Python list and list Functions

List

List is just a collection of the things in a ascending, descending or in a random manner.
code👇
grocery= ("harpic","sauce","masala","vim bar","deo")
print(grocery)
('harpic', 'sauce', 'masala', 'vim bar', 'deo')

grocery= ("harpic","sauce","masala","vim bar","deo",56)
print(grocery)

numbers= (2, 3, 5, 6, 11)
('harpic', 'sauce', 'masala', 'vim bar', 'deo', 56)

grocery= ("harpic","sauce","masala","vim bar","deo",56)
print(grocery[5])
56

To accessing the index numbers of the variable:
code👇
numbers= [2, 3, 5, 6, 11]
print(numbers[2])
5

numbers= [2, 12, 56, 9, 6, 11]
numbers.sort()
print(numbers)
[2, 6, 9, 11, 12, 56]

numbers= [2, 12, 56, 9, 6, 11]
numbers.reverse()
print(numbers)
[11, 6, 9, 56, 12, 2]

some more functions:-
numbers= [2, 12, 56, 9, 6, 11]
print(len(numbers))
print(max(numbers))
print(min(numbers))
print(numbers)
6
56
2
[2, 12, 56, 9, 6, 11]
to adding the elements from the last number of indexes in the variables used by "append" function
code👇
numbers= [2, 12, 56, 9, 6, 11]
numbers.append(71)
numbers.append(150)
numbers.append(320)
print(numbers)
[2, 12, 56, 9, 6, 11, 71, 150, 320]

here's a function ".insert" it ggive opurtunity to append or add a new number given by your own index position.
code👇
numbers= [2, 12, 56, 9, 6, 11]
numbers.insert(1, 850)
print(numbers)
[2, 850, 12, 56, 9, 6, 11]

numbers= [2, 12, 56, 9, 6, 11]
numbers.remove(9)
print(numbers)
[2, 12, 56, 6, 11]

Tuple

Tupple is a fuNction which generally used for change the index value of the variable.
there is two types of tuple function-

Mutable-  "Can change" [LIST]
Immutable-  "Can't change" [TUP]
"(.....)" parenthesis
"[.....]" Square braces
code👇
numbers= [2, 12, 56, 9, 6, 11]
numbers[1] = 98
print(numbers)
[2, 98, 56, 9, 6, 11]

Here 12 changed into the 98.
code:
tp = (1, 2, 3)
print(tp)
(1, 2, 3)
    
    Tuple values can't be changed because its always be a immutable.
To swap the values of two different variables.
code:
a = 1
b = 2
temp = a
a = b
b = temp
print(a, b)
2 1

To know more about functions of lists click here👉 https://www.programiz.com/python-programming/methods/list

Comments

Popular Posts