Professional Documents
Culture Documents
Variables, Expressions, and Statements
Variables, Expressions, and Statements
Variables, Expressions, and Statements
Statements
Constants
• Fixed values such as numbers, letters, and strings are called
“constants” - because their value does not change
• Numeric constants are as you expect
• String constants use single-quotes (')
>>> print 123
or double-quotes (")
123
>>> print 98.6
98.6
>>> print 'Hello world'
Hello world
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
An expression is any legal combination of symbols (like variables, constants and operators) that
represents a value. In Python, an expression must have at least one operand (variable or constant) and
can have one or more operators. On evaluating an expression, we get a value. Operand is the value on
which operator is applied.
Constant Expressions: One that involves only constants. Example: 8 + 9 – 2
Integral Expressions: One that produces an integer result after evaluating the expression. Example: a =
10
• Floating Point Expressions: One that produces floating point results. Example: a * b / 2
• Relational Expressions: One that returns either true or false value. Example: c = a>b
• Logical Expressions: One that combines two or more relational expressions and returns a value as
True or False. Example: a>b && y! = 0 7
• Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z
Assignment Statements
• We assign a value to a variable using the assignment 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 = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
• Highest precedence rule to lowest precedence rule
• Left to right
>>> x = 1 + 2 ** 3 / 4 * 5 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4*5
>>>
1+2*5
Parenthesis
Power 1 + 10
Multiplication
Addition
Left to Right
11
>>> x = 1 + 2 ** 3 / 4 * 5 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4*5
>>> Note 8/4 goes before 4*5 1+2*5
because of the left-right
rule.
Parenthesis 1 + 10
Power
Multiplication
Addition 11
Left to Right
Operator Precedence Parenthesis
Power
Multiplication
Addition
• Remember the rules top to bottom Left to Right
Exam Question: x = 1 + 2 * 3 - 4 / 5
Python Integer Division is Weird!
>>> print 10 / 2
5
>>> print 9 / 2
• Integer division truncates 4
>>> print 99 / 100
• Floating point division produces 0
>>> print 10.0 / 2.0
floating point numbers
5.0
>>> print 99.0 / 100.0
0.99
This changes in Python 3.0
Mixing Integer and Floating
• When you perform an >>> print 99 / 100
0
operation where one operand
is an integer and the other >>> print 99 / 100.0
operand is a floating point the 0.99
result is a floating point >>> print 99.0 / 100
0.99
• The integer is converted to a >>> print 1 + 2 * 3 / 4.0 - 5
floating point before the -2.5
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
string >>> eee = 'hello ' + 'there'
>>> print eee
• For example “+” means “addition” if hello there
something is a number and
“concatenate” if something is a
string concatenate = put together
Type Matters >>> eee = 'hello ' + 'there'
>>> eee = eee + 1
Traceback (most recent call last):
• Python knows what “type” File "<stdin>", line 1, in <module>
everything is TypeError: cannot concatenate 'str'
and 'int' objects
• Some operations are prohibited >>> type(eee)
<type 'str'>
• You cannot “add 1” to a string >>> 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: -14, -2, 0, >>> temp = 98.6
1, 100, 401233 >>> type(temp)
<type 'float'>
• Floating Point Numbers have decimal
>>> 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
>>> i = 42
>>> type(i)
• When you put an integer and <type 'int'>
>>> f = float(i)
floating point in an expression
the integer is implicitly >>> print f
converted to a float 42.0
>>> type(f)
• You can control this with the <type 'float'>
>>> print 1 + 2 * float(3) / 4 - 5
built in functions int() and float()
-2.5
>>>
String >>> sval = '123'
>>> type(sval)
• 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
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 is this rate = 12.50
code doing? pay = hours * rate
print pay
Exercise
• Reserved words
• Variables (mnemonic)
• Operators
• Operator precedence
• Integer Division
• User input
• Comments (#)