Py4Inf 02 Expressions
Py4Inf 02 Expressions
Statements
Chapter 2
x = 12.2 x 12.2
y = 14
y 14
Variables
• A variable is a named place in the memory where a programmer can
store data and later retrieve the data using the variable “name”
x = 2 Assignment statement
x = x + 2 Assignment with expression
print x Print statement
x = 3.9 * x * ( 1 - x )
A variable is a memory location x 0.6
used to store a value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
x = 3.9 * x * ( 1 - x )
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
1 + 10
11
Operator Precedence
• Remember the rules top to bottom
Exam Question: x = 1 + 2 * 3 - 4 / 5
Python Integer Division is Weird!
>>> print 10 / 2
• Integer division truncates 5
>>> print 9 / 2
• Floating point division produces 4
>>> print 99 / 100
floating point numbers 0
>>> print 10.0 / 2.0
5.0
>>> print 99.0 / 100.0
0.99
Mixing Integer and Floating
• When you perform an >>> print 99 / 100
operation where one operand 0
is an integer and the other >>> print 99 / 100.0
0.99
operand is a floating point, >>> print 99.0 / 100
the result is a floating point 0.99
>>> print 1 + 2 * 3 / 4.0 - 5
• The integer is converted to a -2.5
floating point before the >>>
operation
What does “Type” Mean?
• In Python variables, literals and
constants have a “type”
>>> ddd = 1 + 4
• Python knows the difference >>> print ddd
between an integer number and a 5
>>> eee = 'hello ' + 'there'
string >>> print eee
hello there
• For example “+” means “addition”
if something is a number and
“concatenate” if something is a
string concatenate = put together
Type Matters
• Python knows what “type” >>> eee = 'hello ' + 'there'
>>> eee = eee + 1
everything is Traceback (most recent call last):
File "<stdin>", line 1, in <module>
• Some operations are prohibited TypeError: cannot concatenate 'str' and
'int' objects
>>> type(eee)
• You cannot “add 1” to a string <type 'str'>
>>> type('hello')
<type 'str'>
• We can ask Python what type >>> type(1)
something is by using the type() <type 'int'>
>>>
function
Several Types of Numbers
>>> xx = 1
• Numbers have two main types
>>> type (xx)
<type 'int'>
> Integers are whole numbers:
>>> temp = 98.6
-14, -2, 0, 1, 100, 401233
>>> type(temp)
> Floating Point Numbers have decimal <type 'float'>
>>> type(1)
parts: -2.5 , 0.0, 98.6, 14.0
<type 'int'>
• There are other number types - they are >>> type(1.0)
<type 'float'>
variations on float and integer
>>>
Type Conversions
>>> print float(99) / 100
0.99
• When you put an integer and >>> i = 42
>>> type(i)
floating point in an expression, <type 'int'>
the integer is implicitly >>> f = float(i)
converted to a float >>> print f
42.0
Conversions
>>> type(sval)
<type 'str'>
>>> print sval + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
• Why comment?
# All done
print bigword, bigcount
String Operations
• Some operators apply to strings
>>> print 'abc' +
> + implies “concatenation” '123’
abc123
> * implies “multiple concatenation”
>>> print 'Hi' * 5
• Python knows when it is dealing with HiHiHiHiHi
a string or a number and behaves >>>
appropriately
Mnemonic Variable Names
• Since we programmers are given a choice in how we choose our
variable names, there is a bit of “best practice”
• We name variables to help us remember what we intend to store in
them (“mnemonic” = “memory aid”)
• This can confuse beginning students because well-named variables
often “sound” so good that they must be keywords
Exercise
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Summary
• Integer Division
• Type
• Conversion between types
• Reserved words
• User input
• Variables
• Comments (#)
• Operators
• Operator precedence
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance (
...
www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.