Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Programming Lab File Sem - 2

It contain both input and output of 10 python program.

Uploaded by

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

Programming Lab File Sem - 2

It contain both input and output of 10 python program.

Uploaded by

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

A

Lab File Of

Programming In Python Lab


(ICT 160)
Submitted By

SRIDA AGARWAL
Enrollment No. (03716101424)
Submitted To

Ms. Ritu Ma’am


University School of Chemical Technology
Guru Gobind Singh Indraprastha University (GGSIPU)
In
Partial fulfillment of the requirements for the award of the degree of

Bachelor Of Technology
In

Chemical Engineering

Guru Gobind Singh Indraprastha University


Sector- 16C, Dwarka, New Delhi – 110078 (INDIA)
April , 2025
Practical – 1

AIM - Write a program to generate the Fibonacci series.

n = int(input("Enter the number of terms in the Fibonacci series: "))

a, b = 0, 1
count = 0

if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print("Fibonacci series up to 1 term: ")
print(a)
else:
print("Fibonacci series up to n terms: ")
while count < n :
print(a, end=" ")
a, b = b, a + b
count += 1

OUTPUT -
Practical – 2

AIM - Write a function to check the input value is Armstrong.

n = int(input("enter the number: "))


i=0
j=0
for i in str(n) :
j=j+1
print ("The number of digits is :",j)
sum = 0
for i in str(n):
n=int(n)
i=int(i)
sum=sum+(i**j)
if n==sum :
print("The number is Armstrong.")
else :
print ("The number is not Armstrong.")

OUTPUT –
Practical – 3

AIM - Write the function for Palindrome.

num = int(input("Enter a number: "))


original = num
reversed_num = 0

while num > 0:


digit = num % 10
reversed_num = reversed_num * 10 + digit
num = num // 10

if original == reversed_num:
print(f"{original} is a palindrome.")
else:
print(f"{original} is not a palindrome.")

OUTPUT –
Practical – 4

AIM - Write a recursive function to print the factorial for a given number.

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

num = int(input("Enter a number to find its factorial: "))

if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f"The factorial of {num} is: {factorial(num)}")

OUTPUT –
Practical – 5

AIM - Define a function that computes the length of a given list or string.

def mystrlen():
str = input("Enter a string: ")
print ("Length of the input string is: ", len(str))
mystrlen()

OUTPUT -
Practical – 6

AIM - Write a program that takes two lists and returns True if they have at least
one common member.

def common(list1,list2):
result = False
for x in list1:
for y in list2:
if x == y:
result = True
return result
print(common([1,2,3,4,5],[5,6,7,8,9]))
print(common([1,2,3,4,5],[6,7,8,9]))

OUTPUT –
Practical – 7

AIM - Write a Python program to print a specified list after removing the 0th,
2nd, 4th and 5th elements.

lists = ['Apple','Banana','Mango','Orange','Pineapple','Strawberry','Grapes']
lists= [x for (i,x) in enumerate(lists) if i not in (0,2,4,5)]
print (lists)

OUTPUT -
Practical – 8

AIM - Write a Python program to clone or copy a list.

original_list = [15, 3, 25, 21, 16]


new_list = list(original_list)
print(original_list)
print(new_list)

OUTPUT –
Practical – 9

AIM - Write a Python script to sort (ascending and descending) a dictionary by


value.

import operator
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
print('Original dictionary : ',d)
sorted_d = sorted(d.items(), key=operator.itemgetter(0))
print('Dictionary in ascending order by value : ',sorted_d)
sorted_d = dict( sorted(d.items(), key=operator.itemgetter(0),reverse=True))
print('Dictionary in descending order by value : ',sorted_d)

OUTPUT –
Practical – 10

AIM - Write a Python script to concatenate following dictionaries to create a


new one. dic1={1:10, 2:20} dic2={3:30, 4:40} dic3={5:50,6:60}.

dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3): dic4.update(d)
print(dic4)

OUTPUT -
Practical – 11

AIM - Write a Python program to sum all the items in a dictionary.

d={'A':100,'B':540,'C':239}
print("Total sum of values in the dictionary:")
print(sum(d.values()))

OUTPUT –
Practical – 12

AIM – Write a python program for traffic light signals.

light = input("light colour :")


if(light == "red"):
print("Stop")
elif(light == "yellow"):
print("Wait")
elif(light == "green"):
print("Go")
else:
print("Invalid colour")

OUTPUT –
Practical – 13

AIM – Write a python program to get the vowel and space count in a sentence
input by the user.

Text = input("enter a text ")


vowels = "aeiouAEIOU"
space = ' '
vowel_count = 0
space_count = 0
for i in Text:
if i in vowels :
vowel_count +=1
if i in space :
space_count +=1
print("vowel count", vowel_count)
print("space count", space_count)

OUTPUT –
Practical – 14

AIM – Write a python code to see all the arithmetic operators on 2 numbers
input by user.

a = (int(input("Enter first number :")))


b = (int(input("Enter second number :")))

addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
modulus = a % b
floor_division = a // b
exponentiation = a ** b

print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Modulus:", modulus)
print("Floor Division:", floor_division)
print("Exponentiation:", exponentiation)
OUTPUT –
Practical – 15

AIM – Write a python program to get area and perimeter of square and
rectangle input by the user.

print("Square")
side = float(input("Enter the side of the square: "))
square_area = side * side
square_perimeter = 4 * side
print(f"Area of square: {square_area}")
print(f"Perimeter of square: {square_perimeter}")

print("Rectangle")
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
rectangle_area = length * width
rectangle_perimeter = 2 * (length + width)
print(f"Area of rectangle: {rectangle_area}")
print(f"Perimeter of rectangle: {rectangle_perimeter}")
OUTPUT -

You might also like