Session-2 Operators if...Else Loops
Session-2 Operators if...Else Loops
Today’s Topics:
Operators in Python:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Membership Operators
Control-structures
If…else
For
While
Jump Statements
Break
Continue
Pass
Arithmetic Operators
print(5+6)
print(5-6)
print(5*6)
print(5/2)
print(5//2)
print(5%2)
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print(5**2)
Relational Operators
print(4 > 5)
print(4 < 5)
print(4 >= 4)
print(4 <= 4)
print(4 == 4)
print(4 != 4)
Logical Operators
print(1 and 0)
print(1 or 0)
print(not 1)
Bitwise Operators
bitwise and
print(2 & 3)
bitwise or
print(2 | 3)
bitwise xor
print(2 ^ 3)#same same zero
bitwise not
print(~3)#-(n+1)
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
bitwise right shift
print(4 >> 2)
Assignment Operators
# =
a = 2
# a = a % 2
a %= 2
Membership Operators
# in/not in
print("P" in "Pune")
print("P" not in "Pune")
print(4 in [2,3,4,5,6])
print(1 in [2,3,4,5,6])
Example:- Find the sum of digits of a 3 digit number
entered by the user
number = int(input("Enter a three digit number : "))
a = number % 10 # gives last digit of the number
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
number = number//10 # last digit is removed from the
original number
b = number % 10
number = number//10
c = number % 10
print(a + b + c)
Login Program:
email = input("Enter Email : ")
password = input("Enter Password : ")
if email == "teknowell.edutech@gmail.com" and
password == "1234":
print("Welcome")
elif email == "teknowell.edutech@gmail.com" and
password != "1234":
print("Incorrect Password")
password = input("Enter Password again")
if password == "1234":
print("Welcome, Finally!")
else:
print("Incorrect Password again")
else:
print("Incorrect Email")
if menu == '1':
print("\nPIN change")
elif menu == '2':
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print("\nBalance check")
elif menu == '3':
print("\withdrawl")
else:
print("\nExit")
Create a menu driven program for +,-,* and /
Modules in Python:
math
keywords
random
datetime
math
import math
math.sqrt(196)
keyword
import keyword
print(keyword.kwlist)
random
import random
print(random.randint(1,100))
datetime
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
import datetime
print(datetime.datetime.now())
help function
help("modules")
Loops in Python
While Loop
For Loop
While Loop
Example:- Program to print the table of given number
number = int(input("Enter the number : "))
i = 1
while i<11:
print(number, "*", i, "=", number * i)
i += 1
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
counter = 1
while guess != jackpot:
if guess < jackpot:
print("Wrong! guess higher")
else:
print("Wrong! guess lower")
guess = int(input("Guess the number : "))
counter += 1
else:
print("Correct Guess")
print("Attempts", counter)
For loop:
for i in {1,2,3,4,5}:
print(i)
Explanation :
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
To calculate the population for each year with a 10%
increase, you can use a simpler equation based on the
previous year's population. Let's assume the
population of the previous year is represented by
variable x.
The equation can be written as:
Current Year Population = x * 1.1
In this equation, the current year's population is
equal to the previous year's population multiplied by
1.1, representing a 10% increase.
To find the population of the previous year (x), we
can rearrange the equation as follows:
x = Current Year Population / 1.1
Using this simplified equation, if you have the
current year's population (e.g., 10,000), you can
divide it by 1.1 to calculate the population of the
previous year.
This equation allows you to calculate the population
for each year, assuming you know the population of
the current year and want to find the population of
the previous year.
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
result = result + i/fact
print(result)
Nested Loops:
Examples:- unique pairs
for i in range(1, 5):
for j in range(1, 5):
print(i, j)
Patterns:
Example:-
*
**
***
rows = int(input("Enter number of rows : "))
Example:-
1
121
12321
1234321
rows = int(input("Enter number of rows : "))
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end = "")
for k in range(i-1 , 0, -1):
print(k, end = "")
print()
Break:
for i in range(1,10):
if i == 5:
break
print(i)
Example:
lower = int(input("Enter lower range : "))
upper = int(input("Enter upper range : "))
Output:
Enter lower range : 10
Enter upper range : 100
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Continue:
for i in range(1,10):
if i == 5:
continue
print(i)
Pass:
for i in range(1,10):
pass
Project:
balance=5000
response="y"
pin=input("Enter your pin:")
if(pin=="12345"):
print("\n1.Saving\n2.Current\n3.Exit")
while(response=="Y" or response=='y'):
choice=int(input("Enter your choice:"))
if(choice==1):
print("\n1.Deposit\n2.Withdraw\n3.Check
Balance\n4.Exit")
while(response=='Y' or response=='y'):
opr=int(input("Enter Operation:"))
if(opr==1):
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
amount=int(input("Enter amount to
deposit:"))
balance=balance+amount
print("Amount:",balance)
response=input("Do you want to
Continue???")
elif(opr==2):
amount=int(input("Enter amount to
withdraw:"))
if(amount>balance):
print("Insufficent Balance")
response=input("do you want to
continue??")
else:
balance=balance-amount
print("Amount:",balance)
response=input("do you want to
continue??")
else:
print("Invalid Operation")
if(choice==2):
print("\n1.Deposit\n2.Withdraw\n3.Check
Balance\n4.Exit")
while(response=='Y' or response=='y'):
opr=int(input("Enter Operation:"))
if(opr==1):
amount=int(input("Enter amount to
deposit:"))
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
balance=balance+amount
print("Amount:",balance)
response=input("Do you want to
Continue???")
elif(opr==2):
amount=int(input("Enter amount to
withdraw:"))
if(amount>balance):
print("Insufficent Balance")
response=input("do you want to
continue??")
else:
balance=balance-amount
print("Amount:",balance)
response=input("do you want to
continue??")
else:
print("Invalid Operation")
else:
print("Invalid Choice");
else:
print("Quit..............")
else:
print("Invalid Pin")
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Session-2-Task
Problem 1: Write a program that will give you in hand
monthly salary after deduction on CTC - HRA(10%),
DA(5%), PF(3%) and taxes deduction as below:
* Below 5 : 0%
* 5-10 : 10%
* 10-20 : 20%
* aboove 20 : 30%
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Problem 5 - Exercise 12: Display Fibonacci series up
to 10 terms.
5! = 5 × 4 × 3 × 2 × 1 = 120
Output:
120
Example:
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Input:
76542
Output:
24567
**Example 1:**
Input:
30
Output:
276
Problem 9: Write a program that keeps on accepting a
number from the user until the user enters Zero.
Display the sum and average of all the numbers.
UP 5
DOWN 3
LEFT 3
RIGHT 2
!
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
*If the distance is a float, then just print the
nearest integer.*
Example:
Input:
UP 5
DOWN 3
LEFT 3
RIGHT 2
!
Output:
2
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Note: There can be two angles between hands; we need
to print a minimum of two. Also, we need to print the
floor of the final result angle. For example, if the
final angle is 10.61, we need to print 10.
Input:<br>
H = 9 , M = 0<br>
Output:<br>
90<br>
Explanation:<br>
The minimum angle between hour and minute
hand when the time is 9 is 90 degress.
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.