0% found this document useful (0 votes)
19 views19 pages

Tuple

Python tuple simple notes.

Uploaded by

hadihussan573
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

Tuple

Python tuple simple notes.

Uploaded by

hadihussan573
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

മലയാളത്തിൽ

▪ List is a sequence of values called items or elements.


▪ The elements can be of any data type.
▪ The list is a most versatile data type available in Python which can be
written as a list of comma- separated values (items) between square
brackets.
▪ List are mutable, meaning, their elements can be changed.
▪ In Python programming, a list is created by placing all the items
(elements) inside a square bracket [ ], separated by commas.
▪ It can have any number of items and they may be of different types
(integer, float, string etc.).
▪ Method -1 without constructor
▪ # empty list
▪ my_list = []
Method-2 using list constructor
▪ # list of integers
# empty list my_list = list()
▪ my_list = [1, 2, 3] # list of integers
▪ # list with mixed datatypes my_list = list([1, 2, 3])
▪ my_list = [1, "Hello", 3.4]
▪ # nested list
▪ my_list = [“welcome", [8, 4, 6]]
▪ Index operator [] is used to access an item in a list.
▪ Index starts from 0
▪ marks=[90,80,50,70,60]
▪ print(marks[0])
▪ Output: 90
▪ Nested list:
▪ my_list = [“welcome", [8, 4, 6]]
▪ print(my_list[1][0])
▪ Output: 8
▪ Python allows negative indexing for its sequences.
▪ The index of -1 refers to the last item, -2 to the second last item and so on

▪ my_list = ['p','r','o','b','e']

▪ # Output: e
▪ print(my_list[-1])

▪ # Output: p
▪ print(my_list[-5])
▪ To change elements use = operator with index[]
▪ >>> marks=[90,60,80]
▪ >>> print(marks)
▪ [90, 60, 80]
▪ >>> marks[1]=100
▪ >>> print(marks) [90, 100, 80]
▪ A new element or item can be added to an existing list using append() method.
▪ This method adds the item to the end of the list
▪ >>> marks=[90,60,80]
▪ >>> marks.append(50)
▪ >>> print(marks)
▪ >>> [90, 60, 80, 50] #output
▪ To insert one item at a desired location use the method insert()
▪ >>list.insert(index,element)
▪ >>> marks.insert(2,40)
▪ >>> print(marks)
▪ [90, 60, 40, 80, 50]
▪ extend Method: This method works like concatenation.
▪ It takes a list as an argument and adds it to the end of another list.
▪ >>> list1 = [‘x’,’y’,’z’]
▪ >>> list2 = [1,2,3]
▪ >>> list1.extend(list2)
▪ >>> print list1
▪ [‘x’, ‘y’, ‘z’, 1, 2, 3] # Output
>>>
▪ >>> marks.extend([60,80,70])
▪ >>> print(marks)
▪ [90, 60, 40,80, 50, 60, 80, 70]
▪ Python provides many ways in which the elements in a list can be deleted
1.Pop(): pop() method is used to remove an item at the given index.
▪ The pop operator deletes the element on the provided index and stores that
element in a variable for further use
▪ >>> list = [10,20,30,40]
▪ >>> a = list.pop(2)
▪ >>> print(list)
▪ [10,20,40] # Output
▪ >>> print (a)
▪ 30 # Output
2. del Operator :The del operator deletes the value on the provided index, but it
does not store the value for further use.
▪ It can even delete the list entirely.
>>> list = [‘w’,‘x’,’y’,’z’]
>>> del (list[1])
>>> print(list)
[‘w’, ‘y’, ‘z’] # Output
▪ >>> del list
▪ >>>print (list)
▪ Name Error: name ‘list' is not defined
▪ clear() method to empty a list.
▪ list.clear()
▪ print(list)
▪ [] #output
3. remove Operator
▪ We use the remove operator if we know the item that we want to remove or
delete from the list (but not the index).
>>> list = [10,20,30,40]
>>> list.remove(10)
>>> print(list)
[20,30,40] # Output
▪ In order to delete more than one value from a list, del operator with slicing is
used.
>>> list = [1,2,3,4,5,6,7,8]
>>> del list[1:3]
>>> print (list)
[1,4,5,6,7,8] # Output
▪ Slicing [::] (i.e) list[start:stop:step]
▪ Concatenation = +
▪ Repetition= *
▪ Membership = in
▪ 1. Concatenation
▪ The concatenation operator works in lists in the same way it does in a
string. This operator concatenates two lists. This is done by the + operator
in Python
▪ >>> list1 = [10,20,30,40]
▪ >>> list2 = [50,60,70]
▪ >>> list3 = list1 + list2
▪ >>> print (list3)
▪ [10,20,30,40,50,60,70] # Output
▪ 2. Repetition
▪ The repetition operator works as suggested by its name; it repeats the list
for a given number of times.
▪ Repetition is performed by the * operator.
▪ >>> list1 = [1,2,3]
▪ >>> list1 * 4
▪ [1,2,3,1,2,3,1,2,3,1,2,3] # Output
▪ >>> [2] * 6
▪ [2,2,2,2,2,2] # Output
▪ 3. In Operator
▪ The In operator tells the user whether the given string exists in the list or not.
▪ It gives a Boolean output, i.e., True or False.
▪ If the given input exists in the list, it gives True as output, otherwise, False
▪ >>> list = [‘Hello’, ‘Python’, ‘Program’]
▪ >>> ‘Hello’ in list
▪ True # Output
▪ >>> list = [10,20,30,40]
▪ >>> 10 in list
▪ True # Output
▪ >>> 50 in list
▪ False # Output
4.Slicing
▪ List can be sliced in the same way as strings
▪ list[start:stop:step]
▪ list[n:m] produces a slice with elements from index n to index m-1
▪ list=[2,3,4,5,6]
▪ print(list[1:4])
▪ [3,4,5]
▪ print(list[-4:-1])
▪ [3,4,5]

You might also like