0% found this document useful (0 votes)
3 views23 pages

Session-2 Operators if...Else Loops

The document covers various Python programming concepts including operators (arithmetic, relational, logical, bitwise, assignment, and membership), control structures (if-else, loops), and jump statements (break, continue, pass). It also includes practical examples and exercises for implementing these concepts, such as a login program, a menu-driven program, and tasks involving salary calculations, triangle validation, and Fibonacci series. Additionally, it discusses modules in Python and provides examples of using loops and control statements.

Uploaded by

photo9975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Session-2 Operators if...Else Loops

The document covers various Python programming concepts including operators (arithmetic, relational, logical, bitwise, assignment, and membership), control structures (if-else, loops), and jump statements (break, continue, pass). It also includes practical examples and exercises for implementing these concepts, such as a login program, a menu-driven program, and tasks involving salary calculations, triangle validation, and Fibonacci series. Additionally, it discusses modules in Python and provides examples of using loops and control statements.

Uploaded by

photo9975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Session-2

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)

bitwise left shift


print(5 << 2)

Assignment Operators
# =
a = 2
# a = a % 2
a %= 2

Note: a++, ++a # not allowed in Python


print(a)

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")

Example:- Minimum of three numbers


a = int(input("First num : "))
b = int(input("Second num : "))
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
c = int(input("Third num : ")
if a < b and a < c:
print("Smallest is", a)
elif b < c:
print("Smallest is", b)
else:
print("Smallest is", c)

Example:- wap to program to find out maximum number


between two numbers without using if...else statement
a=int(input("Enter number1:"))
b=int(input("Enter number2:"))
ans=max(a,b)
print("Max number:",ans)

Example:- Menu Driven Program


menu = input("""
Hi! how can I help you.
1. Enter 1 for PIN change
2. Enter 2 for balance check
3. Enter 3 for withdrawl
4. Enter 4 for exit""")

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

Example: Find sum of all digits of a given number


n = int(input("Enter the number : "))
number = n
result = 0
while n != 0:
l = n % 10
result += l
n //= 10
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print("Sum of digits in", number, "is", result)

Keep accepting numbers from users till he/she enters


a 0 and then find the average
result = 0
counter = 0
n = int(input("Enter a number: "))
while n != 0:
result += n
counter += 1
n = int(input("Enter a numeber: "))
print("Average of entered numbers is",
result/counter)

while loop with else:


x = 1
while x < 3:
print(x)
x += 1
else:
print("limit crossed")

Example:- Number Guessing Game


import random
jackpot = random.randint(1, 100)
guess = int(input("Guess the number : "))

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)

Example:- The current population of a town is 10000.


The population of the town is increasing at the rate
of 10% per year. You have to write a program to find
out the population at the end of each of the last 10
years.
curr_pop = 10000
for i in range(10,0,-1):
print(i, round(curr_pop))
curr_pop /= 1.1

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.

Example:- Find sum of below sequence till given n


value
1/1! + 2/2! + 3/3! + ... + n/n!
n = int(input("Enter n : "))
result = 0
fact = 1
for i in range(1, n + 1):
fact = fact * i

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 : "))

for i in range(1, rows + 1):


for j in range(1, i + 1):
print("*", end = "")
print()

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()

Loop Control Statements:


 Break
 Continue
 Pass

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 : "))

for i in range(lower, upper + 1):


for j in range(2, i):
if i % j == 0:
break
else:
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print(i)

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:

> Salary(Lakhs) : Tax(%)

* Below 5 : 0%
* 5-10 : 10%
* 10-20 : 20%
* aboove 20 : 30%

Problem 2: Write a program that take a user input of


three angles and will find out whether it can form a
triangle or not.

Problem 3: Write a program that will take user input


of cost price and selling price and determines
whether its a loss or a profit.

Problem 4: Write a menu-driven program -


1. cm to ft
2. km to miles
3. USD to INR
4. exit

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Problem 5 - Exercise 12: Display Fibonacci series up
to 10 terms.

*Note: The Fibonacci Sequence is a series of numbers.


The next number is found by adding up the two numbers
before it. The first two numbers are 0 and 1. For
example, 0, 1, 1, 2, 3, 5, 8, 13, 21. The next number
in this series above is 13+21 = 34*

Problem 6 - Find the factorial of a given number.

Write a program to use the loop to find the factorial


of a given number.

The factorial (symbol: `!`) means to multiply all


whole numbers from the chosen number down to 1.

For example: calculate the factorial of 5

5! = 5 × 4 × 3 × 2 × 1 = 120

Output:

120

Problem 7 - Reverse a given integer number.

Example:

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Input:
76542

Output:
24567

Problem 8: Take a user input as integer N. Find out


the sum from 1 to N. If any number if divisible by 5,
then skip that number. And if the sum is greater than
300, don't need to calculate the sum further more.
Print the final result. And don't use for loop to
solve this problem.

**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.

Problem 9: Write a program which will find all such


numbers which are divisible by 7 but are not a
multiple of 5, between 2000 and 3200 (both included).
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
The numbers obtained should be printed in a comma-
separated sequence on a single line.

Problem 10: Write a program, which will find all such


numbers between 1000 and 3000 (both included) such
that each digit of the number is an even number. The
numbers obtained should be printed in a space-
separated sequence on a single line.

Problem 11: A robot moves in a plane starting from


the original point (0,0). The robot can move toward
UP, DOWN, LEFT and RIGHT with a given steps.
The trace of robot movement is shown as the
following:

UP 5
DOWN 3
LEFT 3
RIGHT 2
!

> The numbers after the direction are steps.

> `!` means robot stop there.

**Please write a program to compute the distance from


current position after a sequence of movement and
original point.**

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

Problem 12: Write a program to print whether a given


number is a prime number or not

Problem 13: Print all the Armstrong numbers in a


given range.
Range will be provided by the user<br>
Armstrong number is a number that is equal to the sum
of cubes of its digits. For example 0, 1, 153, 370,
371 and 407 are the Armstrong numbers.

Problem 14: Calculate the angle between the hour hand


and minute hand.

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.

Problem 15:Given two rectangles, find if the given


two rectangles overlap or not. A rectangle is denoted
by providing the x and y coordinates of two points:
the left top corner and the right bottom corner of
the rectangle. Two rectangles sharing a side are
considered overlapping. (L1 and R1 are the extreme
points of the first rectangle and L2 and R2 are the
extreme points of the second rectangle).

Note: It may be assumed that the rectangles are


parallel to the coordinate axis.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy