In Python, by default, all the parameters (arguments) are passed “by reference” to the functions. Thus, if you change the value of the parameter within a function, the change is reflected in the calling function.We can even observe the pass “by value” kind of a behavior whenever we pass the arguments to functions that are of type say numbers, strings, tuples. This is because of the immutable nature of them.
Python Objects:
- Every object holds unique id and it can be obtained by using id() method. Eg: id(obj-name) will return unique id of the given object.
every object can be either mutable or immutable based on the type of data they hold. - Whenever an object is not being used in the code, it gets destroyed automatically garbage collected or destroyed
- contents of objects can be converted into string representation using a method.
zip() function- it will take multiple lists say list1, list2, etc and transform them into a single list of tuples by taking the corresponding elements of the lists that are passed as parameters. Eg:
list1 = ['A','B','C'] and list2 = [10,20,30]. zip(list1, list2) # results in a list of tuples say [('A',10),('B',20),('C',30)]whenever the given lists are of different lengths, zip stops generating tuples when the first list ends.
Dictionary
Dictionaries are unordered collections of keys and value pair. Dictionaries are mutable. Key should be unique for each value. Key must be unique and immutable.
D1={10:"abc",20:"xyz"} print D1 # {10:’abc’,20:’xyz’}
print D1[10] #abc
D1[20] = "ijk"
D1[30] = "cde"
print D1 #{10:’abc’,20:’ijk’,30:’cde’}
del D1[20] del D1[30] #{10:’abc’}Lists
- Operator * is used to repeat a list by specific number of time.
L1=[1,2]
print L1*2 #[1,2, 1, 2]
- It is used to obtain a sub list which is done by specifying the index.
L1 = [1,2,3,4]
print L1[0:3] #[1, 2, 3]
For this purpose assign the value to the index of the list.L1 = [1, 2, 3, 4]
L1[0] = 0
print L1 #[0, 2, 3, 4]
del L1[0]
print L1 #[2, 3, 4]
No comments:
Post a Comment