Uploaded by gautami.sinha90

GAUTAMI SINHA(EXP-8)

advertisement
11700119099
GAUTAMI SINHA
Experiment No. - 8
Name – Gautami Sinha
Class Roll - CSE2019/001
University Roll-11700119099
Subject Code: PCC-CS393
IT Workshop Lab (Python-3)
SOURCE CODE:
#1)Write a python program to make a list of cubes.
n = int(input("Enter number till which you want cube: "))
cubes = []
for i in range (n+1):
cubes.append(i**3)
print("The cubes are as follows: ", cubes)
print()
OUTPUT:
SOURCE CODE:
#2) Write a python program to find cubes of a list using list comprehension.
print([(x,y) for x in [10,20,30,40] for y in [40,30,20,10] if x!=y])
print()
OUTPUT:
CSE2019/001
11700119099
GAUTAMI SINHA
CSE2019/001
SOURCE CODE:
#3) Write a python program to combine and print elements of two list using list comprehension.
list1 = [10,20,30,40]
list2 = [50,60,70,80]
print([(x,y) for x in list1 for y in list2])
print()
OUTPUT:
SOURCE CODE:
#4) Write a python program to find the sum and mean of elements in a list.
list4 = [1,2,3,4,5,6]
sum = 0
for i in list4:
sum+=i
mean = float(sum/float(len(list4)))
print("Sum: ", sum)
print("Mean: ", mean)
print()
OUTPUT:
SOURCE CODE:
#5) Write a python program to illustrate the use of enumerate() to print an individual item and its
index in the list.
11700119099
GAUTAMI SINHA
CSE2019/001
list5=[10,20,30,40,50]
for index,i in enumerate(list5):
print(i,"is at index ",index)
print()
OUTPUT:
SOURCE CODE:
#6) Write a python program to print the elements in the list using an iterator.
list6 = [10,20,30,40,50]
it=iter(list6)
for i in range(len(list6)):
print("Element at index",i,"is :",next(it))
print()
OUTPUT:
SOURCE CODE:
#7) Write a python program to create a list of numbers divisible by 2 or 4 using filter() function.
def check(x):
if(x%2==0 or x%4==0):
return 1
evens=list(filter(check,range(4,25)))
print(evens)
print()
11700119099
GAUTAMI SINHA
CSE2019/001
OUTPUT:
SOURCE CODE:
#8) Write a python program that adds 2 to every value in the list using map() function .
def add(n):
return n+2
n = [1, 2, 3, 4, 5]
print("Original list: ",n)
added = map(add, n)
print("New list: ",list(added))
print()
OUTPUT:
SOURCE CODE:
#9) Write a python program to pass more than one sequence to the map() function.
def add(a,b):
return a+b
list9_1 = [1,2,3,4,5]
list9_2 = [6,7,8,9,10]
print("Original lists are: ",list9_1," and ",list9_2)
added = map(add,list9_1,list9_2)
print("Added list is: ", list(added))
print()
OUTPUT:
11700119099
GAUTAMI SINHA
CSE2019/001
SOURCE CODE:
#10) Write a python program to calculate the sum of values in a list using the reduce function().
import functools
num_list=[1,2,3,4,5,6,7]
print("Sum of values in list= ")
print(functools.reduce(lambda a,b:a+b,num_list))
print()
OUTPUT:
SOURCE CODE:
#11) Write a program to define a list of countries that are a member of BRICS. Check whether a
country member of BRICS or not.
members = ["brazil", "russia", "india", "china", "south africa"]
country = input("Name the country: ")
if country.lower() in members:
print(country,"is a member of BRICS")
else:
print(country,"is not a member of BRICS")
print()
OUTPUT:
SOURCE CODE:
#12) Write a program that creates a list of words by combining the words in two individual lists.
word1= ["sing", "sketch", "push"]
word2 = ["ing"]
11700119099
GAUTAMI SINHA
print("The words are: ", [(a+b) for a in word1 for b in word2])
print()
CSE2019/001
Download