PYTHON PROGRAMMING
LAB FILE
LAB-1:
Q1-Write a program in python to print your name and “This is my first
program”.
Code-
print("Hello, I am Varnika.")
print("This is my first program.")
Output-
Q2- Write a program in python to print customer details and input them from
the user.
Code-
Id=input("Customer ID:")
name=input("Customer Name:")
bill=input("Bill Amount:")
print("The customer ID is:",Id)
print("The customer name is:",name)
print("The bill amount is:",bill)
Output-
Q3- Write a program in python to print different data types.
Code-
integer=45
float_value=56.8
character="hello"
print(integer)
print(float_value)
print(character)
Output-
Q4-Write a program in python to swap two numbers using 3rd variable and
without using 3rd variable.
Code-
1. Without 3rd variable:
a,b=2,5
a,b=b,a
print(a)
print(b)
Output-
2. With 3rd variable:
a,b=2,5
c=a
a=b
b=c
print(a)
print(b)
Output-
Q5- Write a program in python to find the greatest of 3 numbers.
Code-
print("Enter 3 numbers:")
x=int(input("1st number:"))
y=int(input("2nd number:"))
z=int(input("3rd number:"))
if x>y&x>z:
print(x,"is the greatest")
elif y>x&y>z:
print(y,"is the greatest")
else:
print(z,"is the greatest")
Output-
Q6- Write a program in python to determine if a number is even or odd.
Code-
x=int(input("Enter a number:"))
if x%2==0:
print("It is even.")
else:
print("It is odd.")
Output-
LAB-2:
Q1-Write a program to determine if an alphabet is vowel or consonant.
Code-
char=input("Enter an alphabet:")
vowels={'a','e','i','o','u'}
if char in vowels:
print(char+" is a vowel.")
else:
print(char+" is a consonant.")
Output-
Q2- Write a program in python to determine if an year is a leap year or not.
Code-
year=int(input("Enter year:"))
if year%4==0 and year%100!=0:
print(year," is a leap year.")
elif year%400==0:
print(year," is a leap year.")
else:
print(year," is not a leap year.")
Output-
Q3-Write a program in python to print the table for even cases only.
Code-
number=int(input("Enter a number:"))
x=2
while x<=10:
print(number*x)
x+=2
Output-
Q4-Write a program in python to find the factorial for a given number.
Code-
num=int(input("Enter a number:"))
fact=1
while num!=0:
fact=fact*num
num-=1
print(fact," is the factorial.")
Output-
Q5-Write a program in python to print the Fibonacci series upto n.
Code-
num=int(input("Enter a number:"))
a=0
b=1
while num!=0:
sum=a
a=b
b=a+sum
print(sum)
num-=1
Output-
Q6-Write a program in python to print the pattern of an equilateral triangle.
Code-
rows=int(input("Enter number of rows: "))
for i in range(0,rows+1):
for j in range(0,rows-i):
print(end=" ")
j=j-1
for k in range(0,i):
print("*",end=" ")
print("\r")
Output-
LAB-3:
Q1- Write a program in python to print a string, substring, length of string,
replace character in string, repeat string 3 times and reverse of a string.
Code-
x=input("Enter any string: ")
y=input("Enter other string: ")
print(x)
print([Link](" "))
print(x+" "+y)
print([Link]("a","e"))
print(y*3)
print(y[::-1])
Output-
Q2- Write a program in python to check if a string is a pallindrome or not.
Code-
string=input("Enter string:")
rev=string[::-1]
if string==rev:
print("The string is a pallindrome.")
else:
print("The string is not a pallindrome.")
Output-
Q3-Write a program in python to concatenate 4 strings and split them using
format specifier.
Code-
s1="Good Morning"
s2="My name is "
s3=",my age is"
s4="and cost of my laptop is"
x=input("Enter your name")
y=input("Enter your age")
z=input("cost of laptop")
statement = f"{s1} {s2}{x}{s3}{y}{s4}{z}"
print(statement)
print([Link]("t"))
Output-
LAB-4:
Q1-Write a program in python to create a list of courses (maths, physics) and
electives(eco,cs),extend the list of courses with electives and if the roll no. of
the student is between 1 to 10, then remove eco else, remove cs.
Code-
courses = ["Maths", "Physics"]
electives = ["eco", "cs"]
[Link](electives)
print("Original list:", courses)
roll = int(input("Enter roll number: "))
if 1 <= roll <= 10:
[Link]("eco")
else:
[Link]("cs")
print("Final list:", courses)
Output-
Q2-Write a program in python to find the number of elements in the list having
value greater than the list average .
Code-
n = int(input("Enter number of elements: "))
numbers = []
for i in range(n):
num = int(input("Enter number: "))
[Link](num)
total = sum(numbers)
average = total / n
print("Average =", average)
count = 0
for num in numbers:
if num > average:
count += 1
print("Numbers greater than average:", count)
Output-
Q3-Write a program in python to print the shortest word in the list.
Code-
n = int(input("Enter number of words: "))
words = []
for i in range(n):
w = input("Enter word: ")
[Link](w)
shortest = words[0]
for word in words:
if len(word) <= len(shortest):
shortest = word
print("Shortest word", shortest)
Output-
Q4- Write a program to convert float to integer and then to string.
Code-
num = float(input("Enter a real number: "))
integer_value = int(num)
string_value = str(integer_value)
print("String of integer:", string_value)
print("Type:", type(string_value))
Output-
Q5-Write a program to print Fibonacci series upto n terms.
Code-
n=int(input("Enter number of terms"))
fibonacci=[]
a=0
b=1
for i in range(n):
[Link](a)
a,b=b,a+b
print("Fibonacci series:",fibonacci)
Output-
LAB-5
Q1- Write a program to store book details in tuples and remove books published
before 2020.
Code-
names=tuple(input("Enter names of books: ").split(" "))
yop=tuple(map(int,input("Enter year of publishing of respective books:
").split(" ")))
price=tuple(map(float,input("Enter price of items in rupees: ").split(" ")))
book_details=[names,yop,price]
print(book_details)
for i in range(0,2):
if book_details[1][i]<=2020:
del book_details[1]
break;
print("\n")
print("Updated List of book details: ")
print(book_details)
Output-