Python Class1
Python Class1
PROGRAMMING
WHAT DO YOU KNOW
ABOUT PROGRAMMING
LANGUAGES?
PROGRAMMING LANGUAGE
Instructions Output
Process your
instructions
Example: 2+3 Example: 5
CPU
PROCESS
Instructions
CPU
Example: 2+3
Output
Main Memory
Example: 2+3
Python is initially developed by
Guido van Rossum.
PYTHON
WHY PYTHON?
• Python.org
FIRST PROGRAM
IDLE Script
EXECUTING SCRIPT
Program: print(z)
x=2
y=3 Output:
z=x+y 5
ERROR
Unable to execute your
instructions
VARIABLES,
EXPRESSIONS AND
STATEMENTS
CONSTANTS
• Fixed values such as numbers, letters, and
strings, are called “constants” because their
value does not change
y 14 1005 …
1006 …
1007 …
1008 …
1009 …
.
.
.
.
…….
VARIABLES
x = 3.9 * x * ( 1 - x )
A variable is a memory location
used to store a value (0.6)
x 0.6
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
The right side is an expression.
Once the expression is evaluated, the result 0.936
is placed in (assigned to) x.
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 x 0.6 0.936
(0.936).
0.936 0.936
x = 3.9 * x * ( 1 - x )
0.064
The right side is an expression.
Once the expression is evaluated, the result 0.2336256
is placed in (assigned to) the variable on left-
side ( i.e. x.)
EXPRESSIONS…
NUMERICAL EXPRESSIONS
Operator Operation
• Because of the lack of mathematical
symbols on computer keyboards - we + Addition
use similar symbols to express the
classic math operations - Subtraction
* Multiplication
• Asterisk is multiplication
/ Division
• Exponentiation (raise to a power) looks
different than in math ** Power
% Remainder
NUMERICAL EXPRESSIONS
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5
>>> print(xx) >>> print(kk)
4 3
>>> yy = 440 * 12 >>> print(4 ** 3)
>>> print(yy) 64
5280 4R3
>>> zz = yy / 1000
>>> print(zz) 5 23
5.28 20
3
ORDER OF EVALUATION
• When we string operators together -
Python must know which one to do first
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
>>> print(x) 1 + 2 ** 3 / 4 * 5
11.0
>>> 1+8/4*5
Parenthesis
Power 1+2*5
Division, Multiplication
Addition, Subtraction 1 + 10
Left to Right
11