- This event has passed.
Python Tuple & List
April 30, 2024 @ 8:00 am - May 4, 2024 @ 5:00 pm
What is Tuple?
- A tuple is a collection that is ordered and unchangeable. Tuples are written with round brackets. It is a collection of objects which ordered and immutable. Allow duplicate values.
How to create Tuple?
- #Empty Tuple
- empty_tuple = ()
- print (empty_tuple)
- #Single Element
- To write a tuple containing a single value you have to include a comma, even though there is only one value
- x= (50,)
- #mutiple element
- x= (“apple”, “54.23″, “23″, “apple”, “India”)
- print (this tuple)
READ OR ACCESS
- To access values in the tuple, use the square brackets. For multiple elements use slicing.
- X = (10,20,30)
- X[0]
- X[0:3] #Range of Indexes
- You can use positive and negative index both.
How to update Tuple?
- Tuples are unchangeable, that you cannot change, add, or remove items once the tuple is created.
- tup1 = (12, 34.56)
- # Following action is not valid for tuples
- tup1[0] = 100 #throw Error
How to Delete Tuple?
- Tuples are immutable, so you cannot remove items from it.
- del a[0] #throw error
- #you can delete entire tuple
- del a
——————————————————————————————————————————————————————————
What is List?
- List items are ordered, changeable, and allow duplicate values.It is created by placing all the items (python objects) inside square brackets [], separated by commas.Python objects may be of different types (integer, float, string etc.)
How to create List?
- Lists in Python can be created by just placing the sequence inside the square brackets[].
- colors = [‘red’, ‘blue’, ‘green’]
- To ensure List are ordered you can check below statements
- a = [1,2,”Peter”,4.50,”Ricky”,5,6]
- b = [1,2,5,”Peter”,4.50,”Ricky”,6]
- a ==b
- A list with a single object is sometimes referred to as a singleton list.
READ OR ACCESS
- List items are indexed and you can access them by referring to the index number that nay be positive or negative
- Positive index start with zero ( First Element)
- Negative index start with -1 ( Last Element)
- You can also use slicing to select multiple element in the list. [:] and [start:stop:step]
How to update List?
- refer the index number
- x= [“apple”, 56.32, 23]
x[0] = “blackcurrant“ - #you can also mention range of index
- x[1:3] = [99.99, “watermelon”]
- In case you specify more elements during update then it will insert the remaining value
- x[1:2] = [99.99, “watermelon”]
- In case you specify less items than you replace , then you will lose particular index value
INSERT NEW ELEMENTS.
- Append
- Concatenation
- Insert
- Extend
APPEND: It will add an item to the end of the list
- x= [“Hello”, “banana”, 23]
- x.append(“orange”)
INSERT :The insert() method inserts an item at the specified index
- X.insert(index,value)
EXTEND: To append elements from another list to the current list, use the extend() method.
- x.extend(y)
How to Delete list?
- del x[index] #For single item deletion
- del x[start : stop] #For Mutiple item deletion
- del x #For list deletion
POP : The pop() method removes the specified index.
- X.pop(index)
- If you will not specify index it will remove last element from the list
REMOVE: The remove() method removes the specified item.
- X.remove(value)
CLEAR: He clear() method empties the list. The list still remains, but it has no content.
- X.clear()
