python programs
python programs
012345
01234
0123
012
01
0
3(c)
PROGRAM:
row=int(input("enter number of rows:"))
k=0
for i in range(1,(row+1)):
print("*"*(2*i-1))
print()
OUTPUT:
*
***
*****
*******
*********
4(a):-
Program:-
tuple_library1 = ("deptbooks", "deptjournals", "univquestpqpers")
tuple_library2 = ("competitiveexamsguide", "ebooks", "ejournals")
print("Indexing position of library1:", tuple_library1[1])
print("Number of components in library tuple1:", len(tuple_library1))
print("Number of components in library tuple2:", len(tuple_library2))
print("Concatenation of two library tuples:", tuple_library1 +
tuple_library2)
print("Repetition of tuple_library1:", tuple_library1 * 2)
print("Membership operator of tuple_library1:", "ebooks" in
tuple_library1)
print("Membership operator of tuple_library2:", "ebooks" in
tuple_library2)
print("Slicing of tuple_library2:", tuple_library2[0:2])
Output:-
Indexing position of library1: deptjournals
Number of components in library tuple1: 3
Number of components in library tuple2: 3
Concatenation of two library tuples: ('deptbooks', 'deptjournals',
'univquestpqpers', 'competitiveexamsguide', 'ebooks', 'ejournals')
Repetition of tuple_library1: ('deptbooks', 'deptjournals',
'univquestpqpers', 'deptbooks', 'deptjournals', 'univquestpqpers')
Membership operator of tuple_library1: False
Membership operator of tuple_library2: True
Slicing of tuple_library2: ('competitiveexamsguide', 'ebooks')
4(b):-
Program
tuple_building1 = ("bricks", "sand", "cement")
tuple_building2 = ("tiles", "paint", "wood")
print("Indexing position of building1:", tuple_building1[1])
print("Number of components in building tuple1:",
len(tuple_building1))
print("Number of components in building tuple2:",
len(tuple_building2))
print("Concatenation of two building tuples:", tuple_building1 +
tuple_building2)
print("Repetition of tuple_building1:", tuple_building1 * 2)
print("Membership operator of tuple_building1:", "paint" in
tuple_building1)
print("Membership operator of tuple_building2:", "paint" in
tuple_building2)
print("Slicing of tuple_building1:", tuple_building1[0:2])
Output:-
Number of components in building tuple1: 3
Number of components in building tuple2: 3
Concatenation of two building tuples: ('bricks', 'sand', 'cement', 'tiles',
'paint', 'wood')
Repetition of tuple_building1: ('bricks', 'sand', 'cement', 'bricks', 'sand',
'cement')
Membership operator of tuple_building1: False
Membership operator of tuple_building2: True
Slicing of tuple_building1: ('bricks', 'sand')
4(c):-
Program:-
list_car1 = ["steering", "wheels", "brake", "engine", "seats"]
list_car2 = ["accelerator", "clutch", "gear", "horn", "indicator",
"battery"]
print("Indexing position of 2 in car list1:", list_car1[2])
print("Number of components in car list1:", len(list_car1))
print("Number of components in car list2:", len(list_car2))
print("Concatenation of two car lists:", list_car1 + list_car2)
print("Repetition of car list1:", list_car1 * 2)
print("Membership operator of car list1:", "battery" in list_car1)
print("Membership operator of car list2:", "battery" in list_car2)
print("Slicing of car list1:", list_car1[0:3])
Output:-
Indexing position of 2 in car list1: brake
Number of components in car list1: 5
Number of components in car list2: 6
Concatenation of two car lists: ['steering', 'wheels', 'brake', 'engine',
'seats', 'accelerator', 'clutch', 'gear', 'horn', 'indicator', 'battery']
Repetition of car list1: ['steering', 'wheels', 'brake', 'engine', 'seats',
'steering', 'wheels', 'brake', 'engine', 'seats']
Membership operator of car list1: False
Membership operator of car list2: True
Slicing of car list1: ['steering', 'wheels', 'brake']
5(a):-
Program:-
set1_lang = {"c", "c++", "python", "java"}
set2_lang = {"c", "python", ".net", "c#"}
print("The set1 languages are", set1_lang)
print("The set2 languages are", set2_lang)
print("Union of two sets:", set1_lang.union(set2_lang))
print("Intersection of two sets:", set1_lang.intersection(set2_lang))
print("Difference of two sets:", set1_lang.difference(set2_lang))
Output:-
The set1 languages are {'c++', 'python', 'c', 'java'}
The set2 languages are {'python', 'c', 'c#', '.net'}
Union of two sets: {'c', '.net', 'python', 'c#', 'java', 'c++'}
Intersection of two sets: {'python', 'c'}
Difference of two sets: {'c++', 'java'}
5(b):-
Program:-
automobile1={"transmission":"clutch", "body":"steeringsystem",
"axiliary":"seats"}
print("item in the dictionaries:",automobile1)
automobile1["body2"]="wheels"
print("Add element in the dictionaries:",automobile1)
print("Accessing single element in the
dictionaries:",automobile1["body"])
print("Item in the dictionaries or not:","body" in automobile1)
print("item in the dictionaries or not:","head" in automobile1)
Output:-
item in the dictionaries: {'transmission': 'clutch', 'body':
'steeringsystem', 'axiliary': 'seats'}
Add element in the dictionaries: {'transmission': 'clutch', 'body':
'steeringsystem', 'axiliary': 'seats', 'body2': 'wheels'}
Accessing single element in the dictionaries: steeringsystem
Item in the dictionaries or not: True
item in the dictionaries or not: False
6(a):
Program:-
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while n > 1:
fact *= n
n -= 1
return fact
num = int(input("Enter the number: "))
print("Factorial of", num, "is", factorial(num))
Output:-
Enter the number: 5
Factorial of 5 is 120
6(b)
Program:-
def large(arr):
#root element variable
max=arr[0]
for elem in arr:
if(elem>max):
max=elem
return max
list1=[1,4,5,2,6]
result=large(list1)
print(result)
Output:-
6
6(c)
Program:-
def calculate_area(name):
#converting all characters into lower cases
name=name.lower()
#check for the conditions
if(name=="rectangle"):
l=int(input("enter rectangle's length:"))
b=int(input("enter rectangle's breadth:"))
#calculate area of rectangle
r_area=l*b
print(f"the area of rectangle is {r_area}.")
elif(name=="square"):
s=int(input("enter square’s side length:"))
# Initialize Pygame
pygame.init()
os.environ['pygame_hide_support_prompt'] = 'hide'.
speed = [1, 1]
color = (255, 250, 250)
width = 500
height = 300
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Bouncing Ball")
ball = pygame.image.load("ball.png")
rect_boundry = ball.get_rect()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
rect_boundry = rect_boundry.move(speed)
if rect_boundry.left < 0 or rect_boundry.right > width:
speed[0] = -speed[0] # Reverse horizontal direction
if rect_boundry.top < 0 or rect_boundry.bottom > height:
speed[1] = -speed[1] # Reverse vertical direction