The Programming Cycle for Python
1
Sep 8, 2025 Programming
Run
with some input
Write/Edit
OK?
NO
YES
YES
NO More
Inputs?
2
Sep 8, 2025 Programming
User Program
Filename, preferred extension is py
3
Sep 8, 2025 Programming
IN[1]: Python Shell Prompt
IN[2]:
User Commands
IN[3]: (Statements)
IN[4]: ( )
Outputs
Python Shell is Interactive
4
Sep 8, 2025 Programming
Interacting with Python Programs
• Python program communicates its results to
user using print
• Most useful programs require information
from users
– Name and age for a travel reservation system
• Python 3 uses input to read user input as a
string (str)
5
Sep 8, 2025 Programming
input
• Take as argument a string to print as a prompt
• Returns the user typed value as a string
– details of how to process user string later
IN[1]:
IN[2]: ( )
IN[3]:
Sep 8, 2025 Programming 6
Elements of Python
• A Python program is a sequence of definitions
and commands (statements)
• Commands manipulate objects
• Each object is associated with a Type
• Type:
– A set of values
– A set of operations on these values
• Expressions: An operation (combination of
objects and operators)
7
Sep 8, 2025 Programming
Types in Python
• int
– Bounded integers, e.g. 732 or -5
• float
– Real numbers, e.g. 3.14 or 2.0
• long
– Long integers with unlimited precision
• str
– Strings, e.g. ‘hello’ or ‘C’
8
Sep 8, 2025 Programming
Types in Python
• Scalar
– Indivisible objects that do not have internal
structure
– int (signed integers), float (floating point), bool
(Boolean), NoneType
• NoneType is a special type with a single value
• The value is called None
• Non-Scalar
– Objects having internal structure
– str (strings)
9
Sep 8, 2025 Programming
Example of Types
10
Sep 8, 2025 Programming
Type Conversion (Type Cast)
• Conversion of value of one type to other
• We are used to int float conversion in Math
– Integer 3 is treated as float 3.0 when a real
number is expected
– Float 3.6 is truncated as 3, or rounded off as 4 for
integer contexts
• Type names are used as type converter
functions
11
Sep 8, 2025 Programming
Type Conversion Examples
Note that float to int conversion
is truncation, not rounding off
12
Sep 8, 2025 Programming
Type Conversion and Input
13
Sep 8, 2025 Programming
Operators
• Arithmetic + - * // / % **
• Comparison == != > < >= <=
• Assignment = += -= *= //= /= %= **=
• Logical and or not
• Bitwise & | ^ ~ >> <<
• Membership in not in
• Identity is is not
14
Sep 8, 2025 Programming
Variables
• A name associated with an m
object 64
• Assignment used for binding Acads
m = 64; c
3.1416
c = ‘Acads’;
f = 3.1416; f
• Variables can change their 2.7183
bindings
f = 2.7183;
Sep 8, 2025 Programming
15
Assignment Statement
• A simple assignment statement
Variable = Expression;
• Computes the value (object) of the expression
on the right hand side expression (RHS)
• Associates the name (variable) on the left
hand side (LHS) with the RHS value
• = is known as the assignment operator.
16
Sep 8, 2025 Programming
Multiple Assignments
• Python allows multiple assignments
x, y = 10, 20 Binds x to 10 and y to 20
• Evaluation of multiple assignment statement:
– All the expressions on the RHS of the = are first
evaluated before any binding happens.
– Values of the expressions are bound to the
corresponding variable on the LHS.
x, y = 10, 20 x is bound to 21
x, y = y+1, x+1 and y to 11 at the
end of the program
17
Sep 8, 2025 Programming
Programming using Python
Operators and Expressions
18
09/08/2025 Programming
Binary Operations
Op Meaning Example Remarks
+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4.25 In Python3
9.1/2.0 is 4.55 Real div.
// Integer Division 9//2 is 4
% Remainder 9%2 is 1
19
09/08/2025 Programming
The // operator
• Also referred to as “integer division”
• Result is a whole integer (floor of real
division)
– But the type need not be int
– the integral part of the real division
– rounded towards minus infinity
• Examples
9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0
1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
20
09/08/2025 Programming
The % operator
• The remainder operator % returns the
remainder of the result of dividing its
first operand by its second.
9%4 is 1 (-1)%2 is 1 (-1)//(-2) is 0
9%4.5 is 0.0 1%(-2) is 1 1%0.6 is 0.4
Ideally: x == (x//y)*y + x %y
21
09/08/2025 Programming
Conditional Statements
• In daily routine
– If it is very hot, I will skip
exercise.
– If there is a quiz tomorrow, I will
first study and then sleep.
Otherwise I will sleep now.
– If I have to buy coffee, I will
go left. Else I will go
straight. 22
Sep 8, 2025 Programming
if-else statement
• Compare two integers and print the min.
if x < y: 1. Check if x is less
print (x) than y.
2. If so, print x
else: 3. Otherwise, print y.
print (y)
print (‘is the minimum’)
23
Sep 8, 2025 Programming
Indentation
• Indentation is important in Python
– grouping of statement (block of statements)
– no explicit brackets, e.g. { }, to group statements
x,y = 6,10 Run
x the program
y
if x < y: 6 10
print (x)
else:
print (y) ed Output
i p p
sk the min’)
print (‘is 6 24
Sep 8, 2025 Programming
if statement (no else!)
• General form of the if statement
e
if boolean-expr : tru
fals
e
S1
S1
S2
• Execution of if statement S2
– First the expression is evaluated.
– If it evaluates to a true value, then S1 is
executed and then control moves to the S2.
– If expression evaluates to false, then control
moves to the S2 directly.
25
Sep 8, 2025 Programming
if-else statement
• General form of the if-else statement
if boolean-expr : rt u
e
fa
S1
ls
e
else: S1 S2
S2
S3 S3
• Execution of if-else statement
– First the expression is evaluated.
– If it evaluates to a true value, then S1 is executed and
then control moves to S3.
– If expression evaluates to false, then S2 is executed
and then control moves to S3.
– S1/S2 can be blocks of statements! 26
Sep 8, 2025 Programming
Nested if, if-else
if a <= b:
if a <= c:
…
else:
…
else:
if b <= c) :
…
else:
… 27
Sep 8, 2025 Programming
Elif
• A special kind of nesting is the chain of if-
else-if-else-… statements
• Can be written elegantly using if-elif-..-else
if cond1: if cond1:
s1 s1
else: elif cond2:
if cond2: s2
s2 elif cond3:
else: s3
if cond3: elif …
s3 else
else: last-block-of-stmt
…
28
Sep 8, 2025 Programming
Summary of if, if-else
• if-else, nested if's, elif.
• Multiple ways to solve a problem
–issues of readability,
maintainability
–and efficiency
29
Sep 8, 2025 Programming
Class Quiz
• What is the value of expression:
(5<2) and (3/0 > 1)
a) Run time crash/error
b) I don’t know / I don’t care
c) False
The correct answer is
False
d) True
30
Sep 8, 2025 Programming
Short-circuit Evaluation
• Do not evaluate the second operand of binary
short-circuit logical operator if the result can be
deduced from the first operand
– Also applies to nested logical operators
true false false true
not( (2>5) and (3/0 > 1) ) or (4/0 < 2)
Evaluates to true
31
Sep 8, 2025 Programming
3 Factors for Expr Evaluation
• Precedence
– Applied to two different class of operators
– + and *, - and *, and and or, …
• Associativity
– Applied to operators of same class
– * and *, + and -, * and /, …
• Order
– Precedence and associativity identify the operands for
each operator
– Not which operand is evaluated first
– Python evaluates expressions from left to right
– While evaluating an assignment, the right-hand side is32
Sep 8, 2025 Programming
evaluated before the left-hand side.
Class Quiz
• What is the output of the following program:
y = 0.1*3
if y != 0.3:
print ('Launch a Missile')
else:
print ("Let's have peace")
Launch a Missile
33
Sep 8, 2025 Programming
Caution about Using Floats
• Representation of real numbers in a computer
can not be exact
– Computers have limited memory to store data
– Between any two distinct real numbers, there are
infinitely many real numbers.
• On a typical machine running Python, there are
53 bits of precision available for a Python float
34
Sep 8, 2025 Programming
Caution about Using Floats
• The value stored internally for the decimal
number 0.1 is the binary fraction
0.00011001100110011001100110011001100110011001100110011010
• Equivalent to decimal value
0.1000000000000000055511151231257827021181583404541015625
• Approximation is similar to decimal
approximation 1/3 = 0.333333333...
• No matter how many digits you use, you have
an approximation
35
Sep 8, 2025 Programming
Comparing Floats
• Because of the approximations, comparison of
floats is not exact.
• Solution?
• Instead of
x == y
use
abs(x-y) <= epsilon
where epsilon is a suitably chosen small value
36
Sep 8, 2025 Programming