Posts

Showing posts from February, 2023

Loops in Python: For Loop

Image
Loops are used when one wants to repeat a set of commands several times (iterations). For example,when you want to apply a set of operations on elements of a list, by choosing one element at a time, you can use a python loop. There are two types of loops: For loop While loop For loop:  For loop is used when we want to run the set of commands for a known or defined number of times. Few of the ways in which we can use the "for loop" is as follows: for i in range(10):     print(i)

Python list

Python_list Python lists ¶ Lists in python are collection of objects. They can include any python objects such as numerals, strings, other lists and dictionaries. The list is defined by square bracket as follows: my_list = [] The above example shows an empty list. Following is an example of a list with numbers: In [4]: my_list = [ 1 , 2 , 4 , 65 , 43 ] my_list Out[4]: [1, 2, 4, 65, 43] Lists can also contain strings. In [7]: list_of_str = [ 'abc' , 'goku' , 'vada_pav' , 'chicken_rice' ] list_of_str Out[7]: ['abc', 'goku', 'vada_pav', 'chicken_rice'] Single list can also contain different types of objects. Following is an example of a list containing number, string, another list and a...