range() function :
We have seen that a lot of times we need to create a series of integers ( like 1,2,3,4 ) , so instead of writing them manually inside a list , we can use the range() function. The range function helps in generating numbers from the starting point till the ending point. It can also take in a step value in case you need to make a jump between numbers. The syntax for the the range() function is as follows:
- range( starting_value , ending_value , step_value)
You'll be using the range() function to generate numbers inside a list or you can use range() function as a condition for loops.
for x in range(0,10):
print(x)
# note that initial value is included while final value isn't
print('\n') # new line
for x in range(10): # initial value = 0
print(x)
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
Let us see an example where we use range() function generate all odd numbers from 1 to 100 and store them inside a list.
my_list = list(range(1,100,2))
# step value = 2
print(my_list)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
enumerate() function :Often in a string or even a list , we want to print the value of the object along with its index position. This is where we use the enumerate() function . The enumerate() function creates tuples containing index position and the actual object value.
my_list = ['a','b','c','d']
print(enumerate(my_list))
for x in enumerate(my_list):
print(x)
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
zip() function :zip() function zips ( combines them in form of tuples) two or more then two lists. Let us take an example to understand this.
my_list_1 = ['a','b','c']
my_list_2 = [1,2,3]
my_list_3 = ['A','B','C']
for x in zip(my_list_1,my_list_2,my_list_3):
print(x)
('a', 1, 'A')
('b', 2, 'B')
('c', 3, 'C')
The most common question at this point would be what if the number of elements in a list are not equal. In that case the zip() function would create tuples equivalent to number of elements in the shortest list.
my_list_1 = ['a','b'] # shortest list
my_list_2 = [1,2,3,4]
my_list_3 = ['A','B','C','D']
for x in zip(my_list_1,my_list_2,my_list_3):
print(x)
('a', 1, 'A')
('b', 2, 'B')
In order to store the result of the zip() function , You'll have to cast the zip() function to a list.
my_list_1 = ['a','b','c']
my_list_2 = [1,2,3]
my_list_3 = ['A','B','C']
my_final_list = list(zip(my_list_1,my_list_2,my_list_3))
print(my_final_list)
[('a', 1, 'A'), ('b', 2, 'B'), ('c', 3, 'C')]
'in' keyword :Often you'll want to know if a element or value is present in certain data structure . The 'in' keyword basically returns a boolean value (True or False) . Let us take an example based on this.
my_list = [1,2,3,4,5]
print( 1 in my_list )
my_dict = {1 : 'a' , 2: 'b' , 3: 'c'}
print('a' in my_dict.keys())
print('a' in my_dict.values())
True
False
True
max() and min() function :max() and min() function in python are some of the most common and frequently used functions .They return the maximum and minimum value respectively of a specific data structure. Let us take an example to understand these functions.
my_list = [1,2,3,4,5]
print(max(my_list))
print(min(my_list))
my_dict = {1:"INDIA",2:"USA",3:"CHINA"}
print(max(my_dict)) # by default it'll compare keys
print(max(my_dict.values()))
print(min(my_dict.values()))
5
1
3
USA
CHINA
sum() function :The sum() function is mostly used on lists and it returns sum of all the elements in it. So instead of iterating over all the elements in the list and calculating sum ( using counter) , you can simply use the sum() function.
my_list = [1,2,3,4,5,6]
print(sum(my_list))
21