CS-114 Fundamentals of
Programming
LECTURE # 5
INSTRUCTOR: HANIA ASLAM
Recap
Expressions
Values & Types
Variables
Keywords
Roadmap For Today…
Statements
Augmented Assignments
Operators
Comments
Statements
A statement is a unit of code that the Python interpreter can execute.
What we have seen so far..
Print Statement
Assignment Statement
When you type a statement in interactive mode, the interpreter
executes it and displays the result…
Statements
x = 2 Assignment statement
y = x + 2 Assignment with expression
print (y) Print statement
Variable Operator Constant Function
Statements
A script usually contains a sequence of statements.
Multi Line Statements
Line continuation character ( \ ).
a = 12+ 20 + 3 + \
4+5+6+ \
7+8+9 a = ( 12+ 20 + 3 +
4+5+6+
7+8+9)
Writing long expression within parenthesis.
Multiple Statements in Single Line
Multiple statements can be written in a single line using ( ; ).
w = 10 ; print ( w ) ; w= 30
Augmented Assignment
Common to assign a value to a variable based on its original value.
score = score + 1
Variable score appears on both sides of the assignment statement.
Python provides a shorthand notation for this operation:
score += 1
Self-Review Exercise
>>> score = 8
>>> score *= 5+2
>>> score
=> ???? What would be the result?
Method:
1. Evaluate the expression on the right of the = sign to produce a value.
2. Apply the operator attached to the = sign to the variable on the left of
the = and the value that was produced in step 1.
Augmented Statement
Operators
Operators are special symbols in Python that carry out arithmetic or logical computation.
The value that the operator operates on is called the operand.
Assignment Operator
Arithmetic Operators
Comparison Operators
Logical Operators
Arithmetic Operators
Integer Division Vs. Float Division
Sometimes we want only the integer part of a division result:
How many 24-hour days there are in 53 hours?
◦ 53/24 = 2.2 Float Division
◦ 53//24 = 2 Integer Division
In some cases we just need the remainder part of division result
How many days and hours there are in 53 hours?
o Days= 53//24
o Hours=53%24 (modulo operator yields remainder)
o 2 Days and 5 hours
Integer Division Vs. Float Division
Addition, subtraction, multiplication, and exponentiation all do what you expect.
Division results might be different from your expectation:
>>> minute = 59
>>> minute//60
>>> minute
The value of minute is 59, and in conventional arithmetic 59 divided by 60 is 0.98333.
Operations on Strings
The (+) and (*) operators can be applied to Strings as well.
The (+) operator represents concatenation:
>>> a= ‘ Ali ’
>>>b= ‘ Raza ’
>>>print ( a+b )
Output => Ali Raza
Operations on Strings
The (*) operator performs repetition:
>>> a= ‘100’
>>> print(a*3)
Output => 100100100
Order of operations : Precedence
When more than one operator appears in an expression, the order of evaluation
depends on the rules of precedence.
For mathematical operators python follows mathematical convention.
The acronym PEMDAS is a useful way to remember the order of operations.
Precedence
Self- Review Exercise:
1. 14 + 16 * 3 % 2
Result : 14
2. (14 + 16)* 3 % 2
Result : 0
3. (14 + 16)* (3 % 2)
Result: 30
Comments: Describing code
As programs get bigger and more complicated, they get more
difficult to read.
Add notes to your program.
To include a comment in your program use the following syntax:
# This is a comment
Sequential Steps
x=2 Program:
Output:
print x x=2
print x 2
x=x+2 x=x+2 4
print x
print x
When a program is running, it flows from one step to the next.
As programmers, we set up “paths” for the program to follow.
Q&A’s