Posts

Loops in Python: While loop

Image
Many a times we will encounter a a situation where we have iterate over a group of statements until a specific condition is met. The number iterations that the loop should run is not fixed or known. In such cases, while loop is use.  Statements inside a while loop iterate until a given condition is true. Following is a simple example. 

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...