Python (1)
Python (1)
List objects require more memory Tuple objects require less memory
Ex: Ex:
Import sys Import sys
l = [10,20,30,40,50,60,70,80,90] t = (10,20,30,40,50,60,70,80,90)
print (‘The size of List:’, sys.getsize (l)) print (‘The size of tuple:’, sys.getsize (t))
O/P: The size of List: 136 O/P: The size of tuple: 120
If the content is not fixed and keep on If content is fixed and never changes then we should go for
changing then it is recommended to go for tuple objects.
list objects. Ex: To store YouTube Ex: Allowed input values for vendor machine.
comments Ex: 2
Ex: 2 t1 = (10,20,30,40,50,60,70,80,90)
l1 = [10,20,30,40,50,60,70,80,90] t2 = (10,20,30,40,50,60,70,80,90)
l2 = [10,20,30,40,50,60,70,80,90] print(id(t1))
print(id(l1)) print(id(t2))
print(id(l2)) O/P:
O/P: 2274228629056
2274230408512 2274228629056
2274230411520
List objects won’t be reusable tuple objects are reusable
Performance of list is low, i.e. operations on Performance of tuple is high, i.e. operations on tuple
list object require more time object require less time
Comprehension concept is applicable for Comprehension concept is not applicable for tuple
list
List objects are unhashable type and hence tuple objects are hash able type and hence we can store in
we cannot store in set and in Dict keys set and in Dict keys
Unpacking is possible in list but packing is Both packing and unpacking is possible in tuple
not possible
SET FROZENSET
Set is mutable Frozenset is immutable
Ex: Ex:
S = {10,20,30,40} s = {10,20,30,40}
S.add (50) fs = frozenset(s)
Print(S) # {40,10,50,20,30} fs.add (50)
Print(fs) # Attribute Error: ‘frozen set’ Object has no
attribute ‘add’
We can add new elements to the set as it is As Frozen set is immutable, add, remove such type of
mutable terminology is not applicable
Q.14 What is the difference between list & Dict?
Ans:
LIST DICT
list is a comma group separated individual Dict is a group of commas separated key – value pairs
objects within square bracket within Curley braces
Example: l = [10,20,30,40] Example: D = {‘A’: ‘Apple’, ‘B’: ‘Banana’, ‘C’: ‘Carrot’}
In list, Insertion order is preserved. In Dict, all key value will be stored based on hash code of
keys hence Insertion order is not preserved. But from 3.7
version onwards, the Dict functionality internally replaced
with order Dict functionality. Hence 3.7 version onwards in
case of normal Dict also insertion order can be
guaranteed.
In list duplicate objects are allowed. In Dict duplicate keys are not allowed. But values can be
duplicated. If we trying to add new entry (Key-value pair)
with duplicate key then old value will be replaced with
new value.
In list we can access objects by using index, In Dict we can access values by using keys
which is zero based. i.e index of first object
is zero.
Example: d[10]
In list, object need not to be hashable In Dict, all keys must be hashable.
Q.15 In Dict duplicate key is not allowed. But values can be duplicated. If we trying to add new entry
(Key-value pair) with duplicate key what will be happened?
Ans: In Dict duplicate keys are not allowed. But values can be duplicated. If we trying to add new entry
(Key-value pair) with duplicate key then old value will be replaced with new value.
Example:
D = {}
If function with *args argument, then we We can call this function by passing any number of
can call that function by passing any keyword arguments including zero number and with all
number of values including zero number. these keyword arguments, a dictionary will be created.
With all these values, tuple will be created.
Eg: 1 Eg: 1
def f1 (*args): def f1 (**kwargs):
print (args) print (kwargs)
f1 () f1 (name ='Umesh', roll No='100', marks='90')
f1 (10)
f1 (10,20)
f1 (10,20,30)
Eg: 2
def sum (*args):
total = 0
for x in args:
total = total + x
print ('The sum: ', total)
sum ()
sum (10)
sum (10,20)
Ans: NumPy is a library for Python that adds support for large, multi-dimensional arrays and matrices,
along with a large collection of high-level mathematical functions to operate on these arrays.
Pandas is a high-level data manipulation tool that is built on the NumPy package. The key data
structure in Pandas is called the DataFrame. DataFrames are incredibly powerful as they allow you to