Unit 03 Advanced python for class 10Ai ,11 ai, 12 ai
Operator
Q1. Write a program to take three integer from the user and display in their sum.
Ans :-
Num1 = int(input(“Enter the first number : ”)) Output :
Num2 = int(input(“Enter the first number : ”) ) Enter the first number:78
Num3 = int(input(“Enter the first number : ”) ) Enter the first number:90
Sum=num1+num2+num3 Enter the first number:56
Print(“The sum of three numbers =” , sum) The sum of three numbers =224
Q2. Write a program to take length and breath of a rectangle from the user and display its
area and perimeter .
Ans :-
L=float(input(“enter the length of the rectangle:”)) Output :
L=float(input(“enter the breath of the rectangle:”)) Enter the length of the rectangle:14.5
Area=l*b enter the breath of the rectangle :6.5
Perimeter=2*(l+b) Area of the rectangle = 94.25
Print() Perimeter of the rectangle = 42.0
Print(“Area of the rectangle =”,area)
Print(“perimeter of the rectangle =”, perimeter)
Conditional statement :- make a new file from shell then type code
after save file press F5 for run
( IF ELSE STATEMENT ):- The if-else statement in Python is a fundamental
control flow construct that allows a program to execute different blocks of code based on
whether a given condition is true or false.
EXAMPLE
Q1. Write a program that display a message based on the age entered by the user .
Condition Message
>=18 You are an adult and have the right to cast vote
<18 You are not an adult and cannot cast a vote
Output :
Ans
Enter your age :14
Age=int(input(“enter your age :”))
You are not an adult and cannot cast a vote
If age>=18:
Print( “You are not an adult and have the right to cast a vote “)
Else :
Print(“ you are not an adult and cannot cast a vote “)
(IF … ELSE ….. ELSE STATEMENT .) Python, if, el…if, and else statements are
used for conditional execution, allowing your program to make decisions and execute
different blocks of code based on whether certain conditions are true or false.
Example
Q1. Write a program to keyword catches anything which isn't caught by the preceding
conditions.
a = 200
b = 33
if b > a: Output :
print("b is greater than a") A is greater than b
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Q2.Write a program to keyword is a logical operator, and is used to combine
conditional statements
Ans a = 200 Output :
b = 33 Both condition are true
c = 500
if a > b and c > a:
print("Both conditions are True")
Python Loops
Loop :- Looping means repeating something over and over until a particular condition is
satisfied. A for loop in Python is a control flow statement that is used to repeatedly execute a
group of statements as long as the condition is satisfied. Such a type of statement is also
known as an iterative statement.
Example :- write a program to number in sequence
Ans for n in range(5):
if n == 2:
continue
print(n)
# Output:
# 0
# 1
# 3
# 4
While loop :- A while loop repeatedly executes a block of code as long as a
given condition is true.
Example :- write a programme to repeat numbers ?
Ans
# Basic while loop Output :
count = 0
1,2,3,4,5,6,7,8 two time repeat
while count < 5:
print(count)
count += 1 # Increment count in each iteration
# while loop with a condition based on user input
user_input = ""
while user_input != "quit":
user_input = input("Enter 'quit' to exit: ")
print(f"You entered: {user_input}")
2. write a table of 8 in loop function
Ans
num=int(input('Enter the number whose table is to be displayed : '))
for i in range (1,11):
print(num,'*',i,'=',num*i) Output : whom you want to see any
number table
Eg .8 table