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

Python

Important python material

Uploaded by

bewosi4132
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)
28 views

Python

Important python material

Uploaded by

bewosi4132
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/ 24

ACTIVITY

CONDITIONS:
gender =input("Enter gender:Male/Female")
score = int(input("Enter the score"))
reg = input("Enter region : Local/Non-local")
if gender == "Male":
if score >= 60:
if reg == "Local":
print("Eligible")
else:
print("Not Eligible,they are not from that region")
else:
print("Not Eligible, they didn't score an average of 60")
else:
print("Not Eligible,the person is not male")
LOOPS: n=n//10
//Sum of N natural numbers: print(sum)
n=int(input("enter the number")) //Armstrong number or not
sum=0 n=eval(input("enter n"))
while(n>0): org=n
sum+=n sum=0
n-=1 while(n>0):
print(sum) a=n%10
//Factorial of a numbers: sum=sum+a*a*a
n=eval(input("enter n")) n=n//10
i=1 if(sum==org):
fact=1 print("The given number is armstrong
no")
while(i<=n):
else:
fact=fact*i
print("The given number is not Armstrong
i=i+1
no")
print(fact)
//Sum of digits of a number:
Palindrome or not
n=eval(input("enter n"))
n=eval(input("enter n"))
sum=0
org=n
while(n>0):
sum=0
a=n%10
while(n>0):
sum=sum+a
a=n%10
n = n // 10
sum=sum*10+a
print(sum)
n=n//10
//Reverse the given number:
if(sum==org):
n=eval(input("enter n"))
print("The given number is palindrome
sum=0 no")

while(n>0): else:

a=n%10 print("The given number is not


palindrome no")
sum=sum*10+a
STRINGS:
LIST:
>>>Reading the elements at the runtime print(square)
and saving them to the list.
seq=[]
for i in range(10):
>>>Converting String to list
ele=input()
alpha='Pranay'
seq.append(ele)
x=[i for i in alpha]
print(seq)
print(x)

>>>Looping in list
>>>Looping the list with comprehension
list1=[22,33,44]
list1=[1,2,3,4,5]
for i in list1
list2=[6,7,8,9,10]
print(i)
pair_up=[(x,y) for x,y in zip(list1,list2)]

>>>List Comprehension
>>>nested list
syntax:list=[expression variable variable in
list1=['aa','b',[2.5,66]]
seq]
print(list1)
list1[2]
square=[i**2 for i in range(1,10)]
marks_list=[]
n=int(input("Enter no.of subjects")) #5
for i in range(n): #i=0,1,2,3,4
marks=int(input(f"Enter marks of sub{i+1}"))
marks_list.append(marks)
print(marks_list)
while(True):
print(" 1.Find Max \n 2.Find Min \n 3.Find Avg \n 4.Exit")
option=int(input("Enter your option"))
if(option==1):
max=marks_list[0]
for i in range(len(marks_list)):
if(marks_list[i]>max):
max=marks_list[i]
print("Max value",max)
elif(option==2):
min=marks_list[0]
for i in range(len(marks_list)):
if(marks_list[i]<min):
min=marks_list[i]
print("Min value",min)
elif(option==3):
sum=0
for i in marks_list:
sum=sum+i
print("Average",sum/n)
elif(option==4):
exit(0)
else:
print("Enter valid option")
2)
mat=[[1,2,3],[4,5,6],[7,8,9]]
dia_sum=0
for i in range(len(mat)): #3 i=0,1,2
r_sum = 0
col_sum = 0
for j in range(len(mat[i])): #3 j=0,1,2
r_sum=r_sum+mat[i][j]
col_sum=col_sum+mat[j][i]
if(i==j):
dia_sum=dia_sum+mat[i][j]
print(f"Sum of row{i+1}",r_sum)
print(f"Sum of col{i+1} {col_sum}")
print(f"Dia sum",dia_sum)
TUPLE:
Multiple Assignment or Unpacking of a,b,c,d=map(int,input("Enter values with
tuple: spacing").split())
1)x,y,z=(1,2,3) print(a,b,c,d)
print(x)
print(y) l1=list(map(int,input("Enter the
values").split()))
print(z)
print(l1)
print(type(l1))
Swapping using tuple assignment:
a=20
tup1=tuple(map(float,input("Enter values
b=30
with spacing").split()))
(a,b)=(b,a)
print(tup1)
print("Value after swapping", a,b)
print(type(tup1))

Multiple Values accepted:


a=("python",10, True,"java")
print(type(a))
for i in a:
print(i)
print("--------------------")
for j in range(len(a)):
print(a[j])

-->Split and their functions:


a,b,c,d=input("Enter values with
spacing").split()
print(a,b,c,d)
SET:

l1 = list(map(int, input("Enter multiple elements with space: ").split()))


l2 = list(map(int, input("Enter multiple elements with space: ").split()))
l3 = list(map(int, input("Enter multiple elements with space: ").split()))
set1 = set(l1)
set2 = set(l2)
set3 = set(l3)
# first perform intersection on set1 & set2 r1 = set1.intersection(set2)
# perform intersection of res_set1 & set3 r2 = r1.intersection(set3)
# converting to list
end_list = list(r2)
print(end_list)
s1 = set(input("Enter multiple elements with space: ").split())
s2 = set(input("Enter multiple elements with space: ").split())
result = []
for i in s1:
if i in s2:
result.append(i)
print(result)
DICTIONARY
products={}
for i in range(3):
name=input(f"Enter name of product{i+1}")
price=int(input(f"Enter price of product{i+1}"))
products[name]=price
print(products)
max=0
for i in products.keys():
if(products[i]>max):
max=products[i]
max_product_name=i
print(max_product_name)
print(max)
MyStr1 =input("Enter String: ")
Mydict = {}
for str in MyStr1.split(" "):
Mydict[str] = Mydict.get(str, 0) + 1
print(Mydict)
players={}
n=int(input("Enter no.of players"))
for i in range(n):
name=input(f"Enter name of player{i+1}")
runs=int(input(f"Enter runs of player{i+1}"))
players[name]=runs
print(players)
for i in players.keys():
print(i)
player_name=input("Enter player name to check score")
runs=players.get(player_name,-1)
if(runs==-1):
print("player not found")
else:
print("Player found with runs",runs)
1.Creating dictionary
'''dict1={"key1":100,
"key2":True,
"key3":"Python",
"key4":16.60}
print(dict1)
print(type(dict1))
print(len(dict1))'''

2.Adding elements into a empty dictionary


'''dict={}
dict1["key1"]="python"
dict1["key2"]="java"
print(dict1)'''

3.Assinging multiple values to a single key


'''dict1={}
dict1["key1"]="python"
dict1["key2"]="java"
dict1["key3"]="java",20,89.6
print(dict1)'''

4.Duplicate elements
'''dict1={}
dict1["key1"]="python"
dict1["key2"]="java"
dict1["key3"]="java",20,89.6
dict1["key2"]="css"
print(dict1)'''
5.Extracting values with the help of keys.
'''dict={"key1":100,
"key2":True,
"key3":"Python",
"key4":16.60}
print(dict["key4"])'''

6.Retriving the values using get() method


'''dict={"key1":100,
"key2":True,
"key3":"Python",
"key4":16.60}
print(dict.get("key2"))'''

7.check the existence


'''dict={"key1":100,
"key2":True,
"key3":"Python",
"key4":16.60}
if "key5" in dict:
print("key is present in dict")'''

8.Reassigning the value of key


'''dict={"key1":100,
"key2":True,
"key3":"Python",
"key4":16.60}
dict["key3"]="java"
print(dict)'''
9.Assinging the whole dictionary as value to the key
'''dict={"key1":100,
"key2":True,
"key3":"Python",
"key4":16.60}
dict["key3"]={"apple":20,"banana":30}
print(dict)'''

10.Update
'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
dict1.update({"key2":500})
print(dict1)'''

11.Removing items by using pop()


'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
dict1.pop("key3")
print(dict1)'''

12.Removing the last item by using popitem()


'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
dict1.popitem()
print(dict1)'''

13.Deleting the whole dictionary by using del keyword.


'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
del dict1'''

14.Acessing keys , values and items from the dictionary by using built-in methods

'''dict={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
print(dict.keys())
print(dict.values())
print(dict.items())'''

15.Iterating a loop over all the keys


'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
for i in dict1.keys():
print(i)'''

16.Iterating a loop over all the values


'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
for i in dict1.values():
print(i)'''

17.Iterating a loop over all the items


'''dict1={
"key1":100,
"key2":20.7,
"key3":True,
"key4":"python"
}
for i in dict1.items():
print(i)'''

You might also like