21
loading...
This website collects cookies to deliver better user experience
# List Data Structure
a = ['hello', 1, True, 4.4]
b = [1, 2, 3, 4, 5]
type(a)
# Output: <class 'list'>
type(b)
# Output: <class 'list'>
# Length of the List
s = ['a', 'b', 12, True, 22]
print(len(s))
# Output: 5
# Accessing the list values
s = ['Hello', 4, True, 0.0, 70]
s1 = s[0]
s2 = s[-1]
print('s1:', s1)
print('s2:', s2)
# Output
# s1: Hello
# s2: 70
# Accessing nested list values
list_a = [[1, 2, 3, 4, 5], "Hello World", [True, False]]
a1 = list_a[0]
a2 = list_a[-1]
print('a1:', a1)
print('a2:', a2)
# Output
# a1: [1, 2, 3, 4, 5]
# a2: [True, False]
# Accessing Values of a sublist
a3 = list_a[0][0]
a4 = list_a[-1][-1]
print('a3:', a3)
print('a4:', a4)
# Output
# a3: 1
# a4: False
# List Slicing
fruits = ['Mangoes', 'Bananas', 'Oranges', 'Guava', 'Grapes']
fruit_list1 = fruits[1:3]
fruit_list2 = fruits[1:3:2]
print('fruit_list1:', fruit_list1)
print('fruit_list2:', fruit_list2)
# Output
# fruit_list1: ['Bananas', 'Oranges']
# fruit_list2: ['Bananas']
a = [1,2,3,4,5,6]
a[3] = True
print("After Mutation:", a)
# Output:
# After Mutation: [1, 2, 3, True, 5, 6]
# Using '+' Operator
s = [1, 2, 3, 4, 5]
t = ['Car', 'Bike']
m = s + t
print(m)
# Output
# [1, 2, 3, 4, 5, 'Car', 'Bike']
# Using '*' in Operator
list_1 = [1, 2, 3, 4, 5]
list_2 = list_1 * 2
print('list_2:', list_2)
# Output
# [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
# Using 'in' Operator
list = ['Python', 'Is', 'Awesome']
text = 'Python'
print(text in list)
# Output: True
print('hello' in list)
# Output: False
s = [10, 'fruits', 'Car', 2.0, False]
s.pop()
print(s)
# Output
# [10, 'fruits', 'Car', 2.0]
s.append('new value')
print(s)
# Output
# [10, 'fruits', 'Car', 2.0, 'new value']
# Define a Tuple
tup = ('Lion', 'Bear', 'Monkey')
type(tup)
# Output: <class 'tuple'>
# Define tuple with one value
tup1 = ('Hello')
tup2 = (8)
type(tup1)
# Output: <class 'str'>
type(tup2)
# Output: <class 'int'>
tup3 = ('Hello',)
tup4 = (8,)
type(tup3)
# Output: <class 'tuple'>
type(tup4)
# Output: <class 'tuple'>