Unit 3 Java
Unit 3 Java
Unit 3 Java
B) Repetition
list1 = ['Hello’]
print(list1 * 4)
C) Membership
list1 = ['Red','Green','Blue’]
Print('Green' in list1)
D) Slicing
list1 =['Red','Green','Blue','Cyan', 'Magenta','Yellow','Black’]
print( list1[2:6])
print(list1[0:6:2])
print(list1[::2])
TRAVERSING A LIST
(A) List Traversal Using for Loop :
list1 = ['Red','Green','Blue','Yellow', 'Black’]
for item in list1:
print(item)
Another way of accessing the elements of the list is using range() and len() functions:
for i in range(len(list1)):
print(list1[i])
(B) List Traversal Using while Loop:
list1 = ['Red','Green','Blue','Yellow', 'Black’]
i=0
while i < len(list1):
print(list1[i])
i += 1
LIST METHODS AND BUILT-IN FUNCTIONS
NESTED LISTS
When a list appears as an element of another list, it is called a nested
list.
E.g list1 = [1,2,'a','c',[6,7,8],4,9] #fifth element of list is also a list
print(list1[4])
print(list1[4][1])
To access the element of the nested list of list1, we have to specify two
indices list1[i][j].
The first index i will take us to the desired nested list and second index j
will take us to the desired element in that nested list.
Write a menu driven program to perform various list operations, such
as:
Append an element
Insert an element
Append a list to the given list
Modify an existing element
Delete an existing element from its position
Delete an existing element with a given value
Sort the list in ascending order
Display the list.
Tuples
A tuple is an ordered sequence of elements of different data types, such as
integer, float, string, list or even a tuple.
Elements of a tuple are enclosed in parenthesis (round brackets) and are
separated by commas
E.g : tuple1 = (1,2,3,4,5)
tuple2 =('Economics',87,'Accountancy',89.6)
tuple3 = (10,20,30,[40,50])
tuple4 = (1,2,3,4,5,(10,20))
If there is only a single element in a tuple then the element should be followed by a
comma.
If we assign the value without comma it is treated as integer.
• Tuple is an immutable data type
• However an element of a tuple may be of mutable type, e.g., a list.
tuple2 = (1,2,3,[8,9])
tuple2[3][1] = 10
print(tuple2 )
Accessing Elements in a Tuple
Elements of a tuple can be accessed in the same way as a list or string
using indexing and slicing.
tuple1 = (2,4,6,8,10,12)
print(tuple1[0] )
TUPLE OPERATIONS
1) Concatenation :
tuple1 = (1,3,5,7,9)
tuple2 = (2,4,6,8,10)
print(tuple1 + tuple2 )
Concatenation operator can also be used for extending an existing tuple.
E.g : tuple6 = (1,2,3,4,5)
tuple6 = tuple6 + (6,)
print(tuple6)
tuple6 = tuple6 + (7,8,9)
print(tuple6)
2) Repetition
tuple1 = ('Hello','World’)
Print(tuple1 * 3)
3) Membership
tuple1 = ('Red','Green','Blue’)
print('Green' in tuple1)
print('Green' not in tuple1)
4) Slicing
tuple1 = (10,20,30,40,50,60,70,80)
print(tuple1[2:7])
Print(tuple1[0:len(tuple1)])
TUPLE METHODS AND BUILT-IN
FUNCTIONS
TUPLE ASSIGNMENT
Assignment of tuple is a useful feature in Python.
It allows a tuple of variables on the left side of the assignment operator to be assigned
respective values from a tuple on the right side.
The number of variables on the left should be same as the number of elements in the
tuple.
(num1,num2) = (10,20)
print(num1)
record = ( "Pooja",40,"CS")
(name, rollNo, subject) = record
print(name)
NESTED TUPLES
Q : Write a program to input n numbers from the user. Store these
numbers in a tuple. Print the maximum and minimum number from this
tuple.
DICTIONARIES
The data type dictionary fall under mapping.
It is a mapping between a set of keys and a set of values.
The key-value pair is called an item
E.g : dict3 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
print(dict3)
print(dict3['Ram’])
Dictionaries are Mutable
Adding a new item :
dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
dict1['Meena'] = 78
print(dict1)
Modifying an Existing Item
dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
dict1['Suhel'] = 93.5 #Marks of Suhel changed to93.5
print(dict1)
DICTIONARY OPERATIONS
Membership :
dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
print('Suhel' in dict1)
print('Suhel' not in dict1)
TRAVERSING A DICTIONARY
dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
for key in dict1:
print(key,':',dict1[key])
or
for key,value in dict1.items():
print(key,':',value)
DICTIONARY METHODS AND BUILT-IN
FUNCTIONS
MANIPULATING DICTIONARIES
WAP IN Python to create a dictionary ‘ODD’ of odd numbers between 1 and 10,
where the key is the decimal number and the value is the corresponding number in
words. Perform the following operations on this dictionary:
(a) Display the keys
(b) Display the values
(c) Display the items
(d) Find the length of the dictionary
(e) Check if 7 is present or not
(f) Check if 2 is present or not
(g) Retrieve the value corresponding to the key 9
(h) Delete the item from the dictionary corresponding to the key 9
Write a program IN PYTHON to enter names of employees and their
salaries as input and store them in a dictionary.