Python chapter 4 List and Tuples

List


# creat a list using []
a = [1,2,3,4,5,6]

# print the List using print() function
print(a)

# Access using index using a[0], a[1], a[2]
print(a[2])

# change the value of list using 
a[0] = 98 
print(a)

# we can create list with different type of items 
c = [43, "onam", 89, 89.3]
print(c)

# list slicing
friends = ["harry", "tom", "jarry",78]
print(friends[0:2])
print(friends[-2:])


List method


l1 = [1,8,7,2,21,15]
print(l1)
# l1.sort() #short the the list
# l1.reverse() # reverse the list
# l1.append(54) #adds 54 at the end of list
# l1.insert(3, 10) # insert 10 at index 3
# l1.pop(2) #remove element at index 2
# l1.remove(21) #remove 21 from list
print(l1)


Tuple



t = (1,2,4,5)
# print(t[0])
# cannot update the value of a tuple 
# t[0] = 34 # error occour 
# t1 = () # empty tuple
# t1 = (1) #wrong way to declear a tuple with single element
# t1 = (1,) # tuple with single element
# print (t1)


t = (1,2,4,5,1,1)
print(t.count(1)) # counting index 1
print(t.index(5)) # find place of index


Comments

Popular posts from this blog

Python chapter 3

Login Page design using Html and CSS only