Variables, Expressions, and Statements: Python For Informatics: Exploring Information
Variables, Expressions, and Statements: Python For Informatics: Exploring Information
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”
• Case Sensitive
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
Highest precedence rule to lowest precedence rule:
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
•
TypeError: cannot concatenate 'str'
You can also use int() and and 'int'
float() to convert between >>> ival = int(sval)
>>> type(ival)
strings and integers <type 'int'>
>>> print ival + 1
• You will get an error if the 124
>>> nsv = 'hello bob'
string does not contain >>> niv = int(nsv)
numeric characters Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int()
User Input
• Why comment?
# All done
print bigword, bigcount
String Operations
• Some operators apply to strings
>>> print 'abc' + '123’
> + implies “concatenation”
abc123
> * implies “multiple concatenation” >>> print 'Hi' * 5
HiHiHiHiHi
• Python knows when it is dealing with
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
http://en.wikipedia.org/wiki/Mnemonic
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c = a * b
print x1q3p9afd print c
hours = 35.0
What are these rate = 12.50
bits of code doing? pay = hours * rate
print pay
Exercise
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Summary
• Integer Division
• Type
• Conversion between types
• Reserved words
• User input
• Variables (mnemonic)
• 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.