22PLC15b-Python-NOTES - Module2
22PLC15b-Python-NOTES - Module2
22PLC15b-Python-NOTES - Module2
Hint:
>>> 'The ' + spam[1] + ' ate the ' + spam[0] + '.'
'The bat ate the cat.'
2. Explain the following in terms of python lists with suitable examples.
a. Negative Indicies
c. len() function
d. Modifying lists
e. List concatenation
f. List replication
g. del() function
i. Multiple assignment
Hint:
5. What are Methods? How are they different from functions? Exaplain with a suitable
example.
Hint:
Hint:
• index()
o syntax: list_name.index(list_element)
o returns index of list_element’s first appearence
o Eg:
• append()
• insert()
• remove()
• sort()
7. Explain mutable and immutable data types using tuples and lists.
Hint:
• A list value is a mutable data type: It can have values added, removed, or changed.
Eg:
Hint:
• The tuple data type is almost identical to the list data type, except in two ways.
o tuples are typed with parentheses, ( and ), instead of square brackets, [ and ].
o tuples, like strings, are immutable. Tuples cannot have their values modified, appended, or
removed.
Eg:
o If only one value is in a tuple, this must be indicated by placing a trailing comma after the value
inside the parentheses.
Eg:
>>> type(('hello',))
<class ‘tuple’>
>>> type(('hello'))
<class ‘string’>
Hint:
Hint:
• When a list is assigned to a variable, it actually assigns a list reference to the variable.
• A reference is a value that points to some bit of data, and a list reference is a value that points to a list.
• A reference is an address used by the python interpreter to access the variables from the memory.
>>>spam = [0, 1, 2, 3, 4, 5]
>>>cheese = spam
>>>cheese[1] = 'Hello!'
>>>print('spam =',spam)
>>>print('cheese =',cheese)
Output:
• Python uses references whenever variables must store values of mutable data types, such as
lists or dictionaries.
• For values of immutable data types such as strings, integers, or tuples, Python variables will
store the value itself
• To avoid
Hint:
def eggs(someParameter):
someParameter.append('Hello')
spam = [1, 2, 3]
print('spam before eggs:',spam)
eggs(spam)
print('spam after eggs:',spam)
output:
• when eggs() is called, a return value is not used to assign a new value to spam. Instead, it modifies the
list in place, directly.
• Even though spam and someParameter contain separate references, they both refer to the same list.
Hint:
Eg:
import copy
output:
Hint:
bread[3][2]=44
print("Changing bread[3][2]=44...now..")
print('jam is:',jam)
print('bread is:',bread)
Output:
Hint:
Ex:
myCat['size']
15. Explain the following methods for lists: keys(), values(), items()
• There are three dictionary methods that will return list-like values of the dictionary’s keys, values, or
both keys and values: keys(), values(), and items().
Ex:
Ex:
Output:
16. Explain the usage of dictionary methods with for loop using suitable examples.
Hint:
Ex:
for v in myCat.keys():
print(v)
Output:
size
color
disposition
Ex:
for v in myCat.items():
print(v)
Output:
('size', 'fat')
('color', 'gray')
('disposition', 'loud')
Ex:
for v in myCat.values():
print(v)
Output
fat
gray
loud
17. Explain the usage of in and not in operators for dictionaries with examples.
Hint:
18. Explain the usage of get() method for dictionaries with a suitable example.
Hint:
Output:
Hint:
• Setdefault is used to set a value for a key only if that key does not already have a value.
• i.e. if previously a key exists and has a value then it will not be modified.
Ex:
• Else if a key is not present then it may be assigned a default value using this method.
Ex:
Hint:
import pprint
{' ': 7,
'!': 2,
'I': 1,
'a': 2,
'c': 1,
'e': 5,
'g': 1,
'h': 2,
'i': 1,
'l': 2,
'm': 1,
'n': 2,
'o': 3,
'p': 1,
'r': 1,
's': 1,
't': 3,
'v': 1,
'w': 1}