About Variables in Python F E B 1 1 T H

Similar documents
The float type and more on variables FEB 6 TH 2012

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

ENGR 101 Engineering Design Workshop

Jython. secondary. memory

Chapter 2 Writing Simple Programs

Variable and Data Type I

PROGRAMMING FUNDAMENTALS

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Variable and Data Type I

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

CMSC 201 Computer Science I for Majors

Beyond Blocks: Python Session #1

Basic Syntax - First Program 1

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

CSCE 110 Programming I

Python I. Some material adapted from Upenn cmpe391 slides and other sources

CSCE 110: Programming I

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Programming with Python

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Key Differences Between Python and Java

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

An Introduction to Processing

Getting Started with Python

Short, Unique and Mysterious

Introduction to Problem Solving and Programming in Python.

Full file at

Variables, Expressions, and Statements

Variables, expressions and statements

Algorithms and Data Structures

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Beyond programming. CS 199 Computer Science for Beginners Spring 2009 Lois Delcambre Week 5/12/2009 1

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Fundamentals of Programming (Python) Getting Started with Programming

3. Java - Language Constructs I

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Table of Contents EVALUATION COPY

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Introduction to Programming

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

egrapher Language Reference Manual

CSE / ENGR 142 Programming I

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

2.Raspberry PI: Architecture & Hardware Specifications

Introduction to Python, Cplex and Gurobi

Computer Components. Software{ User Programs. Operating System. Hardware

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

And Parallelism. Parallelism in Prolog. OR Parallelism

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Lecture 19. Operators and Abstraction

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Final thoughts on functions F E B 2 5 T H

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Variables. Data Types.

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

VLC : Language Reference Manual

Bits, Words, and Integers

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Iterators & Generators

Datatypes, Variables, and Operations

Functions!!! Why functions? Functions provide good way to design systems!

SOFT 161. Class Meeting 1.6

Table of Contents. Preface... xxi

Programming Language 2 (PL2)

Python Reference (The Right Way) Documentation

Fall 2017 CISC124 9/16/2017

Here n is a variable name. The value of that variable is 176.

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

mith College Computer Science Week 12 CSC111 Spring 2018 Dominique Thiébaut

What Version Number to Install

Beyond Base 10: Non-decimal Based Number Systems

1st and 3rd September 2015

Java Identifiers, Data Types & Variables

CS1 Lecture 3 Jan. 18, 2019

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

Computational Expression

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Python Basics. 1 of 7 9/5/2018, 8:51 AM. txt1 = "ada lovelace, english mathematician and writer" print(txt1)

Python: common syntax

COMP 364: Functions II

COS 140: Foundations of Computer Science

Visual C# Instructor s Manual Table of Contents

Basics of Java Programming

CS1 Lecture 3 Jan. 22, 2018

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Course Title: Python + Django for Web Application

Transcription:

About Variables in Python F E B 1 1 T H

Range of floating point numbers What is the largest floating point number in Python? Unfortunately, there is no sys.maxfloat. Here is an interesting way to find out: prod = 1.0 while prod*1.1!= prod: prev = prod prod = prod*1.1 print prev, prod The output is 1.78371873262e+308 inf

What does this output mean? Python uses an object called inf to represent positive infinity. When 1.78371873262e+308 was multiplied by 1.1 (i.e., increased by 10%), we went beyond the upper limits of type float. This means that the largest floating point number in Python has 308 digits. Notice that the while-loop terminated because inf * 1.1 equals inf.

A better version of this program import math prod = 1.0 while not math.isinf(prod): prev = prod prod = prod*1.1 print prev, prod There is a function called isinf(x) in the math module that tells us if x equals inf.

Sequence types There are seven sequence types in Python: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. Later we will study study strings, lists, and tuples in more detail. There are many powerful built-in operations on sequence types provided by Python. Stay tuned for details.

Variables in Python Variables are sticky notes attached to objects. What happens during the assignment statement? x = 10 A memory cell (made up of 4 bytes) is created and 10 is placed in it. The name x is attached ( stuck ) to this memory cell.

More on variables What happens when x = x + 1 is executed? 1. The object that x is attached to (i.e., 10) is copied into some working area. 2. 1 is added to this object. 3. The new object (i.e., 11) is moved into a (different) memory cell. 4. The name x is now attached to this new memory cell.

Multiple sticky notes at the same location What happens when we execute: x = 5 y = x x = x +1 1. x is a sticky note attached to a memory cell containing 5. 2. Then y is also stuck to this very location. 3. When x = x + 1 is executed, remember the memory cell containing 10 remains unchanged and the sticky note x is moved to the cell with 11. 4. Therefore y continues to have value 10.

The function id(x) id(x) returns the identity of the object x. This is an int (or long) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id value. We will take id(x) to be the address of the memory cell that x is occupying. This is not accurate, but good enough and will help our mental model of how variables work.

Try these code snippets x = 5 x = 5 id(x) y = x x = x + 1 id(x) id(x) id(y) x = x + 1 id(x) id(y)

Variable names Variable names need to start with a letter (upper or lower case) or an underscore (i.e., _ ). Following the first character, any sequence of letters, digits, and underscores is allowed. Python has a small number of keywords, that cannot be used as variable names: and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try

More on variables Case matters. The variables count and Count are different. Do not use lower case el ( l ), upper case oh ( O ), or upper case eye ( I ) as single letter variable names. These are hard to distinguish from numerals 0 and 1 in some fonts. Use meaningful names: e.g., factorbound, myupperlimit, sequencelength, etc. Watch out for spelling errors in variable names.

Scope of a variable In Python there is no explicit variable declaration. In many languages (C, Java, etc.) variables have to be declared before they can be used. In programs in these languages, a variable comes into existence when it gets declared. In Python, a variable comes into existence when it is first assigned a value. The variable lives until the end of the program or until it is explicitly deleted using the del operator (this operator will become useful later). The scope of a variable is the portion of the program that the variable is in existence for.