Variables, Expressions, and Statements Chapter 2 Flashcards
Variables
a named place in the memory where a programmer can
store data and later retrieve the data using the variable “name”
• Programmers get to choose the names of the variables
• You can change the contents of a variable in a later statement.
A variable is a memory location used to store a value (0.6) The right side is an expression. Once the expression is evaluated, the result is placed in (assigned to)the variable on the left side (i.e., x).
x = 12.2 y = 14
Python Variable Name Rules
- Must start with a letter or underscore _
- Must consist of letters and numbers and underscores
- Case Sensitive
• Good: spam eggs spam23 _speed
• Bad: 23spam #sign var.12
• Different: spam Spam SPAM
Reserved Words
You cannot use reserved words as variable names / identifiers
Reserved Words
and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print as with
Sentences or Lines
x = 2
Assignment Statements
• We assign a value to a variable using the assignment statement (=)
• An assignment statement consists of an expression on the
right-hand side and a variable to store the result
Example of an Assignment Statements :
x = 3.9 * x * ( 1 - x )
Numeric Expressions
• Because of the lack of mathematical symbols on computer keyboards - we use “computer-speak” to express the classic math operations • Asterisk is multiplication • Exponentiation (raise to a power) looks different from in math.
\+ Addition - Subtraction * Multiplication / Division ** Power % Remainder
Numeric Expressions
> > > xx = 2
xx = xx + 2
print xx
4
> > > yy = 440 * 12
print yy
5280
> > > zz = yy / 1000
print zz
5
>>> jj = 23 >>> kk = jj % 5 >>> print kk 3 >>> print 4 ** 3 64
Order of Evaluation
• When we string operators together - Python must know which one
to do first
• This is called “operator precedence”
• Which operator “takes precedence” over the others?
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:
> Parenthesis are always respected
> Exponentiation (raise to a power)
> Multiplication, Division, and Remainder
> Addition and Subtraction
> Left to Right
Parenthesis Power Multiplication Addition Left to Right
> > > x = 1 + 2 ** 3 / 4 * 5
print x
11
Operator Precedence
• Remember the rules top to bottom • When writing code - use parenthesis • When writing code - keep mathematical expressions simple enough that they are easy to understand • Break long series of mathematical operations up to make them more clear Parenthesis Power Multiplication Addition Left to Right Exam Question: x = 1 + 2 * 3 - 4 / 5
Python Integer Division is Weird!
This changes in Python 3.0
>>> print 10 / 2 5 >>> print 9 / 2 4 >>> print 99 / 100 0 >>> print 10.0 / 2.0 5.0 >>> print 99.0 / 100.0 0.99
Mixing Integer and Floating
• When you perform an operation where one operand is an integer and the other operand is a floating point, the result is a floating point • The integer is converted to a floating point before the operation
>>> print 99 / 100 0 >>> print 99 / 100.0 0.99 >>> print 99.0 / 100 0.99 >>> print 1 + 2 * 3 / 4.0 - 5 -2.5 >>>
What does “Type” Mean?
• In Python variables, literals and constants have a “type” • Python knows the difference between an integer number and a string • For example “+” means “addition” if something is a number and “concatenate” if something is a string >>> ddd = 1 + 4 >>> print ddd 5 >>> eee = 'hello ' + 'there' >>> print eee hello there
concatenate = put together
Type Matters
• Python knows what “type” everything is • Some operations are prohibited • You cannot “add 1” to a string • We can ask Python what type something is by using the type() function
> > > eee = ‘hello ‘ + ‘there’
eee = eee + 1
Traceback (most recent call last):
File “”, line 1, in
TypeError: cannot concatenate
‘str’ and ‘int’ objects
»> type(eee)
> > > type(‘hello’)
> > > type(1)
> > >