Reserved Words
•
Yo u can not use reserved words as variable names / identifiers
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
Sentences or Lines
x = 2
x = x + 2
print x
Variable Operator
Constant Reserved Word
Assignment Statement
Assignment with expression
Print statement
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
x = 3.9 * x * ( 1 - x )
x = 3.9 * x * ( 1 - x )
0.6
x
Left side is an expression. Once
expression is evaluated, the result
is placed in (assigned to) x.
0.6
0.6
0.4
0.93
A variable is a memory location
used to store a value (0.6).
x = 3.9 * x * ( 1 - x )
0.6 0.93
x
Right side is an expression. Once
expression is evaluated, the result
is placed in (assigned to) the
variable on the left side (i.e. x).
0.93
A variable is a memory location
used to store a value. The value
stored in a variable can be updated
by replacing the old value (0.6)
with a new value (0.93).
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.
Operator Operation
+ 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
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
% Remainder
5 23
4 R 3
20
3
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
Parenthesis
Power
Multiplication
Addition
Left to Right
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print x
11
>>>
1 + 2 ** 3 / 4 * 5
1 + 8 / 4 * 5
1 + 2 * 5
1 + 10
11
Không có nhận xét nào:
Đăng nhận xét