use of list And tuple
# list [ ]
l1=[1,6,3,8,5,9,5]
print(l1)
l1.sort()
print(l1)
print(l1[0:5]) # 0to 5 printed
print(l1[0:7:2]) #alternat 1 place skiped
#l1 = l1.replace("1","7") #this is use only for string not for list if you do so first change to string
print(l1)
l1.reverse()
print(l1)
l1.append(5) #add a new element at last in this list
l1.insert(5,100) # add an element at 6th place as 100 to existing list
#l1.sort()
print(l1)
l1.pop(3) # it remove 3rd index value
print(l1)
l1.remove(8) #it remove element 8 from the list
print(l1)
################################
#creating a tuple using()(parenthesis)
t=(2, 3, 6, 9, 10)
#printing the tuple
print(t[4]) # it provide yo the 5th element of t n+1th element
#you can't change or update the value of t
#t[1]=90 # you will see an error you cant change the value of tuple
#t1 =(2) #wrong way to assign a tuple use always a comma
#t= (2,) # tupple with a single element
print(t)
# tuple methods
print(t.count(2)) #how many time 2 is reoccurs here only 1
t=(2, 3, 6, 9, 10, 2, 3, 2,)
print(t.count(2)) # 2 occurs 3 time
print(t.index(10)) # 10 is in which index place
###################################### list shorting
# program to accept marks of 7 students and display them in a short manner
s1 = int(input("marks of student 1: ")) # all input data here is a string in front of there we define then as int
s2 = int(input("marks of student 2: "))#we can not sort any string first we have to typecast the string
s3 = int(input("marks of student 3: ") )# int and float can only be sorted
s4 = int(input("marks of student 4: "))
s5 = int(input("marks of student 5: "))
s6 = int(input("marks of student 6: "))
s7 = int(input("marks of student 7: "))
marklist=[s1, s2, s3, s4 , s5 , s6, s7]
marklist.sort()
print(marklist)
################################## Example
# write a program to store five animal name in a list entered by the user
from binascii import a2b_hex # Convert between binary and ASCII
a1 = input("Enter animal name : 1: ")
a2 = input("Enter animal name : 2: ")
a3 = input("Enter animal name : 3: ")
a4 = input("Enter animal name : 4: ")
a5 = input("Enter animal name : 5: ")
Myanimallist =[a1, a2, a3, a4, a5]
print(Myanimallist)
######################### example
# in a tuple item cannot be altered
c = (1, 5, 7,33, 44,)
c[1]=34 #item can't be assign or change
#in tuple we store data as an object
#################################### sum 4 numbers
# write a program to sum a list of 4 numbers
a = [2, 5, 8, 12 ]
print(a[0] + a[1] + a[2]+ a[3])
# it can be done by loop or sum function , here we want to use basic elementary function
#or we can use sum for any length list or tuple
print(sum(a)) # both deliver same solution
#Q 5 write a program to count the number of zeros in the following tuple
a = (7, 0, 8, 0, 0, 9, 8, 8,8)
print(a.count(0)) # there are 3 zeros
print(a.count(8))
Comments
Post a Comment