Lecture - 6
Lecture - 6
• Built in functions
• int()
• float()
Operators Arithmetic
Comparison
Assignment
Bitwise
Membership
Identity
Arithmetic Operators
Operator Description Example
+ Addition A+B
- Subtraction A–B
* Multiplication A*B
/ Division A/B
% Modulus A%B
** Exponent A ** B
// Floor Division A // B
Arithmetic Operators
Comparison Operators
Operator Description Example
== Equality A == B
!= Not equal to A != B
< Less than A<B
> Greater than A>B
<= Less than equal to A <= B
>= Greater than equal to A >= B
Comparison Operators
Assignment Operators
Operator Description Example
= Assignment A = 10
x = 2 + 4 * 3 - 4 / 5 * 6
Operator Precedence
• In this case, Python follows a rule
x = 1 + 8 / 4 * 5
x = 1 + 2 * 5
x = 1 + 10
x =11
Exercise
1. x = (4 + 2) * 3 - 4 * 5
-2
2. x = 1 + 2 * 4 / 4 * 10
21
Simple Exercise
• Simple Pay calculator
• Hours = 40/week
• Rate = 100/hr
• Pay ????
Program:
Hours = 40
Rate = 100
Pay = Hours * Rate
Conditional Execution
• Normally program flow is sequential.
True
Statements
Conditional Statements in Python
• Python programming language provides following types of
decision-making statements
IF statement
Nested statements
IF-ELSE statement
IF-ELIF statements
If Statement
False
Syntax: Condition
if condition:
True
• statements to be executed;
Statements
Simple Analogy
• Your mobile Phone alarm
print(‘x is a positive
number’)
Print(‘x is a positive number’)
Example Program
x=5
if x == 5 : Equals 5
print('Equals 5')
if x > 4 : Greater than 4
print('Greater than 4') Greater than or Equals 5
if x >= 5 :
print('Greater than or Equals 5') Less than 6
if x < 6 : print('Less than 6') Less than or Equals 5
if x <= 5 :
print('Less than or Equals 5') Not equal 6
if x != 6 :
print('Not equal 6')
Indentation
• Indent defines the scope of the block (Lines of code).
if condition: False
True
• statements to be Condition
executed;
•if condition: False
Statements
• statements to be
executed;
Statements
Example
True
Program x >0
x = 5 False
if x > 0 : True
x < 10
if x < 10:
print(‘x is a positive
False
single digit number’) Print
Statement
Simplified Condition using Logical Operators
• Recall logical operators
AND
OR
• Provides a way to simplify the nested statements. NOT
Program
x =
if 0 < x and x < 10:
print(‘x is a positive single digit number’)
If-Else Statement
• There are two possibilities and condition determines which will be
executed.
if sensor == True:
print(‘Barrier opened’)
else:
print(‘Barrier closed’)
Simple Analogy
• Email login
False
Syntax: Condition
if condition:
True
• statements to be executed; Else block
else :
If block
statements to be executed;
Example Program x=6
Program x> 0
x = 6
if x > 0 : Print(‘Positive’) Print(‘Negative’)
print(‘x is positive’)
else :
print(‘x is negative’)
print(‘Done’)
Print(‘Done’)
Example Program x=6
• Program the determine the number is even or odd
x = 6 x % 2==0
if x % 2 == 0 :
print(‘x is even’)
else : Print(‘Even’) Print(‘Odd’)
print(‘x is odd’)
print(‘Done’)
Print(‘Done’)
Example Program
• Re-write the same program to the get the input from user
Program
• Else is optional.
If-elif Statements
Syntax:
if condition-1:
statements to be executed;
elif condition-2:
statements to be executed;
elif condition-3:
statements to be executed;
else:
statements to be executed;
If-elif Statements
True
Condition Statements
False
True
Condition Statements
False
True
Condition Statements
False
Statements
Example
Program:
if x < 2 :
• print('small')
elif x < 10 :
• print('Medium')
else :
• print('LARGE')
print('All done')
•
Example
Simple Python calculator
Choice Operation
Takes two numbers from user
along with the choice of 1 Addition
operation 2 Subtraction
3 Multiplication
4 Division
Example
Simple Python calculator