Docsity

Python Programming Assignments and Solutions - Chaudhary Bansilal University, Cheat Sheet of Mathematics

Partial preview of the text

Download Python Programming Assignments and Solutions - Chaudhary Bansilal University and more Cheat Sheet Mathematics in PDF only on Docsity!

A

Practical File

On

Python

Programming

(Session - 2023-2024)

Submitted To: Submitted By:

Dr. ManjuLata Himanshu Sharma

(Asst. Professor) 220000613007

Deptt. of ICT M.Sc.(C.S.)

rd

Sem.

Department of Information and Communication Technology Chaudhary Bansilal University, Bhiwani (Haryana)

INDEX Sr. No.

Name Of Program Page

No. Remarks 1 Program to sum all the items in a list. 03 2 Program to multiply all the items in the list 04 3 Program to get the largest number in the list 05 4 Program to access the index of a list. 06 5 Program to get the difference between the two lists. 07 6 Program to clone or copy a list. 08 7 Program to remove duplicate from a list. 09 8 Program to check a list is empty or not. 10 9 Program to find the list of word that are longer than n from a given list of words. 11 10 Program to create a function with variable length of arguments 12 11 Write a Python function that takes two list and return True if they have atleast one common number. 13 12 Program to print a specified list after removing the 0th,4th,and 5th elements. 14 13 Program to shuffle and print a specified list. 15 14 Program to create a Simple Program Using Class and Objects 16 15 Program to create a class to find sum and difference of two numbers using inheritance 17 16 Program to generate and print a list of first and last 5 elements where the values are square of number between 1 and 30. 18 17 Program to generate and print a list except for the first 5 elements, where the value are square of number between 1 and

19 18 Program to draw a star using Python Turtle 20 19 Program to draw a color filled Circle using Python Turtle 21 20 Program to create an app with widgets Using Tkinter 22

2. Program to multiply all the items in the list

function def

new_list(list):

Multiply elements one by one

product = 1 for i in list: product = product * i return product list1 = [23, 12, 4, 8] print(list1) print("Product:") print(new_list(list1))

Output:-

[23, 12, 4, 8] Product: 8832

3. Program to get the largest number in the list

#List of numbers listA = [10, 30, 5, 75, 9, 66] #Printing the largest number in a list print("Largest number is : " , max(listA))

Output:-

Largest number is : 75

5. Program to get the difference between the two lists

def Diff(list1, list2): list_dif = [i for i in list1 + list2 if i not in list1 or i not in list2] return list_dif list1 = [20, 10, 30, 35, 15, 40] list2 = [10, 5, 40, 25] list3 = Diff(list1, list2) print(“ Difference : “,list3)

Output:-

Difference: [20, 30, 35, 15, 5, 25]

6. Program to clone or copy a list

def Cloning(list1): list_copy = list1[:] return list_copy list1 = [2, 4 ,6, 8, 16, 10, 12] list2 = Cloning(list1) print("Original List : " , list1) print("After Cloning : ", list2)

Output:-

Original List : [2, 4, 6, 8, 16, 10, 12] After Cloning : [2, 4, 6, 8, 16, 10, 12]

8. Program to check a list is empty or not

list1 = [12, 3, 67] if not list1: print("The list is empty") else: print("The list is not empty")

Output:-

The list is not empty

9. Program to find the list of word that are longer than n from a given

list of

words

def long_words(n, str): word_len = [] txt = str.split(" ") for x in txt: if len(x) > n: word_len.append(x) return word_len print(long_words(3, "Life is a succession of lessons"))

Output:-

['Life', 'succession', 'lessons']

10. Program to create a function with variable length of arguments

def multiply(*args):

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

Output:-

True False

12. Program to print a specified list after removing the 0

th

th

and 5

th

elements

Create a list 'color' with several color strings color =

['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']

Use a list comprehension to create a new list 'color' that includes elements from

the original list

but only if their index (i) is not in the specified indices (0, 4, 5)

color = [x for (i, x) in enumerate(color) if i not in (0, 4, 5)]

Print the modified 'color' list, which excludes elements at indices 0, 4, and 5

print(color)

Output:-

['Green', 'White', 'Black']

13. Program to suffle and print a specified list

from random import shuffle animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Panda"] shuffle(animal) print(“Specified list : “,animal)

bike1.name = "Mountain Bike" print(f"Name: {bike1.name}, Gears: {bike1.gear} ")

Output:-

Name: Mountain Bike, Gears: 11

15. Program to create a class to find sum and difference of two

numbers using inheritance

class First: def input1(self): self.x = int(input("Enter first number:")) class Second: def input2(self): self.y = int(input("Enter second number:"))

class Third(First, Second): def add(self): super().input1() super().input2() self.z = self.x + self.y print("Sum is:", self.z) obj = Third() obj.add()

Output:-

Enter first number: 43 Enter second number: Sum is: 98

16. Program to generate and print a list of first and last 5 elements

where the values are square of number between 1 and 30

Define a function named printValues

def printValues(): # Create an empty list 'A' A = list()

Loop from 1 to 20 (inclusive)

for i in range(1, 21):

Calculate the square of 'i' and append it to the list 'A'

A.append(i**2)

Print the first 5 elements of the list 'A'

print(A[:5])

Print the last 5 elements of the list 'A'

[36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900]