Unit 4
Unit 4
Unit- IV
Dr. T. Nikil Prakash
Assistant Professor
Department of Information Technology
St. Joseph’s College (Autonomous)
Tiruchirappalli-02
Lists
• Python Lists are just like dynamically sized arrays, declared in other
languages (vector in C++ and ArrayList in Java).
• In simple language, a list is a collection of things, enclosed in [ ] and
separated by commas.
• The list is a sequence data type which is used to store the collection of
data.
• Tuples and String are other types of sequence data types.
• Example
• Var = ["Geeks", "for", "Geeks"]
• print(Var)
• Output:
• syntax
• s=list[start:stop:step]
• Example :
• num_list=[1,2,3,4,5,6,7,8,9,10]
• print(“num_list is:”,num_list)
• print(“first elemnent in the list is”,num_list[0])
• print(“num_list[2:5]=”,num_list[2:5])
Output:
• print(“num_list[::2]=”,num_list[::2]) num_list is:
• print(“num_list[1::3]=”,num_list[1::3]) [1,2,3,4,5,6,7,8,9,10]
first elemnent in the list
is 1
num_list[2:5]= [3,4,5]
num_list[::2]= [1,3,5,7,9]
num_list[1::3]= [2,5,8]
Updating values in the lists:
• once created, one or more elements of a list can be easily
updated by giving the slice on the left-hand side of the
assignment operator.
• You can also append new values in the list and remove existing
values from the list using the append( ) method and del
statement respectively.
• Example:
• num_list= [1,2,3,4,5,6,7,8,9,10]
• print(“list is:”,num_list)
• num_list[5]=100
• print(“List after updation is:”,num_list)
• num_list.append(200)
• print(“List after appending a value is: “,num_list)
• del num_list[3]
• print(“List after deleting a value is:”,num_list)
Output:
list is: [1,2,3,4,5,6,7,8,9,10]
List after updation is: [1,2,3,4,5,100,7,8,9,10]
List after appending a value is: [1,2,3,4,5,100,7,8,9,10,200]
List after deleting a value is: [1,2,3,5,100,7,8,9,10,200]
Python List Operations
• The concatenation (+) and repetition (*) operators work in the same
way as they were working with the strings.
• The different operations of list are
• Repetition
• Concatenation
• Length
• Iteration
• Membership
1. Repetition
• The redundancy administrator empowers the rundown components to
be rehashed on different occasions.
• Code
• # repetition of list
• # declaring the list
• list1 = [12, 14, 16, 18, 20]
• # repetition operator *
• l = list1 * 2
• print(l)
• Output:
• [12, 14, 16, 18, 20, 12, 14, 16, 18, 20]
2. Concatenation
• It concatenates the list mentioned on either side of the operator.
• Code
• Code
9
4. Iteration
• The for loop is used to iterate over the list elements.
• Code
• Code
tion
Python List methods
• Python List Methods are the built-in methods in lists used to
perform operations on Python lists/arrays.
• Below, we’ve explained all the Python list methods you can use
with Python lists, for example, append(), copy(), insert(), and
more.
Python List methods
Method Description
Used for adding elements to the end of the
append()
List.
copy() It returns a shallow copy of a list
This method is used for removing all items
clear()
from the list.
count() These methods count the elements.
Adds each element of an iterable to the end
extend()
of the List
Returns the lowest index where the element
index()
appears.
Python List methods
Syntax: Tup1=(val1,val2,….)
• Example :
• 1) Tup1=(1,2,3,4,5,6,7,8,9,10)
• print(“Tup[3:6]=”,Tup1[3:6]) Output:
• print(“Tup[:8]=”,Tup1[:4]) Tup[3:6]=(4,5,6)
Tup[:8]=(1,2,3,4)
• print(“Tup[4:]=”,Tup1[4:])
Tup[4:]=5,6,7,8,9,10)
• print(“Tup[:]=”,Tup1[:]) Tup[:]=(1,2,3,4,5,6,7,8,9,10)
Updating tuples:
• Tuples are immutable objects so we cannot update the values but
we can just extract the values from a tuple to form another tuple.
• Example:
• 1) Tup1=(1,2,3,4,5)
• Tup2=(6,7,8,9,10)
• Tup3=Tup1+Tup2
• print(Tup3)
Output:
(1,2,3,4,5,6,7,8,9,10)
Deleting elements of a tuple
• Deleting a single element in a tuple is not possible as we know a tuple
is an immutable object.
• Hence there is another option to delete a single element of a tuple i.e..,
you can create a new tuple that has all elements in your tuple except the
ones you don’t want.
• Example:
• 1) Tup1=(1,2,3,4,5)
Output:
Traceback (most recent call last):
• del Tup1[3] File "test.py", line 9, in <module>
• print Tup1 del Tup1[3]
Type error: „tuple‟ object doesn‟t support item
deletion
Nested Tuples in Python
• A nested tuple is a Python tuple that has been placed inside of
another tuple.
• Let's have a look at the following 8-element tuple.
• tuple = (12, 23, 36, 20, 51, 40, (200, 240, 100))
• This last element, which consists of three items enclosed in
parenthesis, is known as a nested tuple since it is contained
inside another tuple.
• The name of the main tuple with the index value, tuple[index], can
be used to obtain the nested tuple, and we can access each item of
the nested tuple by using tuple[index-1][index-2].
Example of a Nested Tuple
• # Python program to create a nested tuple
• # Creating a nested tuple of one element only
• employee = ((10, "Itika", 13000),)
• print(employee)
Output
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
• Empty Dictionary:
• {}
Output
accessing a dictionary
• Accessing Values
• The main and common way to access dictionary values is via indexing.
• However, this approach is dangerous because if we try to access the
value of a key that doesn’t exist, then Python will raise a KeyError
exception.
• access_Value_missing_key
Accessing Keys
• The Python dictionary just has one method that is solely used to
return keys from a dictionary.
• #1) d.keys()
• Just like d.value(), this returned object can be converted into a set,
tuple, list, or directly iterated through, based on need.
• Accessing Key-Value Pairs
• At times it is necessary to access dictionary key-value pairs.
• if we want to compute the dictionary values, and create a new
dictionary with the new values but maintaining the keys.
• The Python dictionary has a method that is solely used for this purpose.
• #1) d.item()
• dict_items([(value1,key1),(value1,key1),...,(valueN,keyN)])
Update A Dictionary
• We can change the value of a key in a dictionary.
• The most common way is via indexing.
d.update(b) - output
Delete From A Dictionary