Enumerate in Python

Enumerate is used in Python when we want to get the index of an element along with the element. For ex: a = [5, 6, 7, 8]for i, item in enumerate(a):    print(i, a) The output will be: 0 51 62 73 8  

Read more...

Dictionaries in Python

Dictionaries in Python can be created by using curly braces as below: phone_nos = {} This is an empty dictionary. We can add an item like below: phone_nos[‘sreedhar’] = 123456 phone_nos[‘sam’] = 1781767 A dictionary contains keys and values. In the above example, ‘sreedhar’ and ‘sam’ are keys and 123456 and 1781767 are values. We…

Read more...

Lists in Python

Array or List in Python is a useful data structure to store and access data. A list is defined as below: mylist = [] An item is added to the list as below: mylist.append(“hi”) We can get the length of the list using len: list_len = len(myList) To loop through the entire using a for…

Read more...

Defining a function in python

We should define a function when we need to do something repeatedly – something that can’t be done in a while loop or a for loop. Below is a sample function which prints a line on standard output. def print_something():    print(“Hi there”) print_something() Save the code in a file func.py and run as > python…

Read more...

Define and print a string

There are many ways to print a string. Let us say we are in a Python interpreter session. We define a string and then print the same in four different methods. > name = “Sreedhar” > print(f”My name is {name}”) My name is Sreedhar > print(“My name is {}”.format(name)) My name is Sreedhar > print(“My…

Read more...