Important built-in functions and keywords in Python
Knowing in detail about built-in functions and keywords in Python
Range
Suppose we want to print the nos. 1–10 present on the list. We would create a list and iterate over it as follows.
>>> list1 = [1,2,3,4,5,6,7,8,9,10]>>> for i in list1:
print(i)1
2
3
4
5
6
7
8
9
10
An easier way to do it is by using ‘range’. Range takes the arguments: start, stop, step just in the case of slicing. It starts from “start” and goes up to “stop” but not including it. The step is used in case there is a jump needed.
>>> for i in range(1,11):
print (i)1
2
3
4
5
6
7
8
9
10>>> for i in range (1,11,2):
print (i)1
3
5
7
9
If we need to make a list using range, the following is done.
>>> list(range(2,22,2))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Enumerate
>>> count = 0
>>> list2 = [11,22,33]>>> for i in list2:
print("Number is {}, at index {}".format(i,count))
count+=1Number is 11, at index 0
Number is 22, at index 1
Number is 33, at index 2
This code can be written in a simpler way using ‘enumerate’
>>> for i in enumerate(list2):
print(i)(0, 11)
(1, 22)
(2, 33)
So ‘enumerate’ keeps track of the index on its own and gives the output as tuples. We can unpack these in the code itself.
>>> for index,num in enumerate(list2):
print(index)
print(num)0
11
1
22
2
33
Zip
It zips together many lists.
>>> list4 = [1,2,3]
>>> list5 = ['a','b','c']>>> for i in zip(list4,list5):
print(i)(1, 'a')
(2, 'b')
(3, 'c')>>> list6 = [4,5,6]>>> for i in zip(list4,list5,list6):
print(i)(1, 'a', 4)
(2, 'b', 5)
(3, 'c', 6)
Now let’s add an uneven no. of elements and see the zipping. We can observe that the zipping happens as long as the number of elements is matched. That is it will zip as far as the shortest list goes.
>>> list4 = [1,2,3,4,5,6,7]
>>> list5 = ['a','b','c']
>>> list6 = [44,55,66]>>> for i in zip(list4,list5,list6):
print(i)(1, 'a', 44)
(2, 'b', 55)
(3, 'c', 66)
It can be cast into a list too.
>>> list(zip(list4,list5,list6))
[(1, 'a', 44), (2, 'b', 55), (3, 'c', 66)]
In
It helps to check if an element is in an object.
>>> 22 in [11,22,33]
True>>> 'b' in 'python'
False>>> 'k1' in {'k1':'a'}
True>>> dict1 = {'k1':1, 'k2':2}
>>> 2 in dict1.values()
True>>> 2 in dict1.keys()
False
Not in
>>> 22 not in [11,22,33]
False
Min and Max
It helps to return the minimum and maximum value in a list.
>>> list7 = [2,43,55,77,122]
>>> min(list7)
2>>> max(list7)
122
Random library
There is a built-in library in python called random. This library has many functions. We need to import the library to be able to use it.
# random is the libraray, shuffle is one of its functions
>>> from random import shuffle
>>> list7
[2, 43, 55, 77, 122]
shuffle doesn’t return anything and is an in-place function.
>>> shuffle(list7)
>>> list7
[55, 122, 2, 43, 77]
Now let’s try to grab random integers using ‘randint’ function. The randint take two arguments: start and end range
>>> from random import randint
>>> randint(0,50)
16>>> n1 = randint(2,5)
>>> n1
4
Taking user input
The ‘input’ function helps in taking user input
>>> m1 = input('Enter a name: ')
>>> Enter a name: mini
>>> m1
'mini'>>> n1 = input('Enter a number: ')
>>> Enter a number: 34
>>> n1
'34'
As observed the output is in the form of a string. But when we need to input numbers, it cannot be in string format but integer format. So we do typecasting.
>>> int(n1)
34>>> n1 = int(input('Enter a number: '))
>>> Enter a number: 45
>>> n1
45
Refer to the notebook here.
Beginner-level books to refer to learn Python:
Advance-level books to refer to learn Python:
Reach out to me: LinkedIn
Check out my other work: GitHub