Useful operators

range():

for num in range(starting_value,ending_value,increment)
ending value is not considered inside the num
range generates the number instead of storing it in a list..

Enumerate

What it does is it returns an index counter with the data value in a loop .
for eg

word='abcde'
for index,letter in enumerate(word):
print(index)
print(letter)

zip (): just like a zipper it will zip both of the list and pair up the items

mylist1=[1,2,3]
mylist2=['a','b','c']
for item in zip(mylist1,mylist2):
print(item)

#it will only be able to zip pair till the shortest list , afterwards it will ignore it .

In operator : to quickly check if the element is present or not.Works in dictionary

min and max functions:

random library

Importing from a library

  1. shuffle (list , tuple ,etc ): it does not return any thing
  2. randint(lowerrange,upperrange) : returns a random interger.

input function always takes a string as a input , so we need typecasting

List comprehension

quickly creating a list , for example if you keep on appending you list..

mystring='hello'
mylist=[]
for letter in mystring:
mylist.append(letter)
print(mylist)

OR

mylist=[letter for letter in mystring]

You can also perform certain operations

mylist=[x fo x in range(0,11) if x%2==0]

How to use if and else statement

results=[x if x%2==0 else ‘ODD’ for x in range(0,11)]

Nested loop:

mylist=[x*y for x in [2,4,6] for y in[10,100,1000]

Leave a comment