List and Tuple in Python | Shrddha Khapra #Lec 03
# Lists in python
# a built in data types that can stores set of values
# it can store different type of element(integer,string,float,ect)
marks = [12.3,23.6,45.7,89.9,77.6]
print(marks)
# we can get value at index like string we have already read it
idx = marks[0]
print(idx)
# we can check our list type
print(type(marks)) #list class also be "lists"
# we can print list legenth
print(len(marks))
# it can store different type of element(integer,string,float,ect)
student = ["asad",345,"ali",34.0]
print(student)
std_idx = student[0]
print(std_idx)
# lists are muteable in python we can change in lists and not possible in string
new_std = ["ali",56,89.0]
new_std[0] = "asad"
print(new_std) #here we can made change in list
# List Slicing
# similar to string Slicing
# syntax : list_name[starting_idx : ending_idx] * ending idx is not included
serial = [67,89,809,990,89,90,9]
slicing = serial[0 : 4] #slicing all elements
# print(slicing)
print(serial[ :6]) # in this line i have pass last index it will me all indices
print(serial[0 : ]) # here i have passed starting index it will give me all indicies bcz we have not enter ending index it willconsider all idicis same upper print statment
# we can also use negative idexing here
print(slicing[-4:-1])
# Lits methoeds/functions
lists = [1,5,3,4]
lists.append(9) #add one element at the enf
lists.sort() #sort in assending order
"""lists.sort(reverse = True)""" #sort in desending order
# we can reverse our list (hum apni list ulta saktey starting value end mein aur ending value start mein aa jaye gi)
lists.reverse()
# insert element at any index
lists.insert(1,"asad")
print(lists)
# we can apply assending and dessending metoeds on string
str1 = ["banana","mango","apple","grips"]
"""str1.sort()""" #assending
str1.sort(reverse = True) #desending
print(str1)
listn = [2,1,3,1]
"""listn.remove(1)""" #remove first occurance
listn.pop(2) #remove at index
print(listn)
# we can search python documentation on google
# Tuples in python
# A built in data type let us immutable sequence of values we can't make a change in"tup"
""""
tup = (78,90,76,43,45) #tup[0],tup[1]
tup[0] = 58 # we cannot change because it immutable
print(tup) #it will give you error!
"""
# tup[0] = 58 # we cannot change because it immutable
tup = (12,45,67,89)
print(type(tup)) #its type istuple
# difference b/w list and tuple is list is mutable and tuple is immutable
tup1 = (23,56,78,90)
print(tup1[0:3])
# Tuple methoeds
# return first occurs index
tuple2 = (1,2,3,1,2)
return_idx = tuple2.index(2) #return index of first occurance
print(return_idx)
# count total appearance
tuple3 = (6,7,9,4,6,7,4)
count_occur = tuple3.count(6) #count total occurances
print(count_occur)
#Let's practice
# wap to ask user to enter thier 3 favorite movies name and store them in list
"""
user = str(input("enter your 1st favorite movie name = "))
user2 = str(input("enter your 2nd favorite movie name = "))
user3 = str(input("enter your 3rd favorite movie name = "))
# movies_list = [user,user2,user3]
# movies_list.sort()
# print(movies_list)
movies = []
movies.append(user)
movies.append(user2)
movies.append(user3)
# print(movies)
"
"""
# WAP to check if list contain a palindrome of elements (hint:use copy() metoed)
listn = [1,2,3,2,1]
palind = listn.copy()
palind.reverse()
if(listn == palind):
print("this is palindrome list")
else:
print("this is not palindrome list")
# WAP to count the number of student with A grade in following tuple
"""
grade = ("C","D","A","A","B","B","A")
grade_counter = grade.count("A")
print(grade_counter)
"""
grade = ["C","D","A","A","B","B","A"]
grade.sort()
print(grade)
Comments
Post a Comment