Important function in python and Miscellaneous

1==1.0 is True but
0.1+0.2==0.3 is False


while(true): continue if ,break ,try,except exception as e \n print(e/customized error)
except executes after a error occurs in try # try  may/may not be printed completely
exception can be specified : except namerror:,except typerror:, valuerror:,zerodivisionerror
raise error-type("error statement")
else: gets executed if try was succcessful and no break statement
finally: gets executed irrespective of error
__name__ == __main__ by default or name of module used
import filename  , filename.funcname , from filename import variablename(or)*
global (used inside function) works on whole variable and can change its value

global- used at top of prog , defined var can be used anywhere
local -used inside a func to use that var in the func only
enumerate(iterable) # add counter to an iterable and returns it.Syntax == for index,item in enumerate(iterable)
list/set/dict comprehension == 
a=[1,2,5,1128938,3198,993,8589]
# b=[]
""" for item in a:
    if item%2==0:
        b.append(item)
"""
b=[i for i in a if i%2==0]
print(b)
dict_name={"a":3,"b":4,"A":5}
print(dict({k.lower():dict_name.get(k.lower(),0)+dict_name.get(k.upper(),0) for k in dict_name.keys()}))

newlist=[i for i in existinglist/range/input  condition]
newlist=[num*i for i in range(a,b)]

print(dict({k.lower():dict_name.get(k.lower(),0)+dict_name.get(k.upper(),0) for k in dict_name.keys()}))
list(b=[i for i in a if i%2==0])

NOTE :set comprehension is similar to list but it produces a generator object which can only be iterated once

virtual environment
lambda arguments:expressions
variable = "separation".join(list) # creates a string with elements of list separated by separation
.join(iterable object),elements should be string
format -
1. f"something {variable name}"
2. "something {index/leave empty}".format(elements)
"%s something " %(value)
map-applies a function to all elements of input_list
map(function,input_list) # function to be written without parenthesis
filter-creates a list of list for which a function returns true
filter(function,input_list) # function to be written without parenthesis
both filter and map need to be manually converted to list
from functools import reduce
val=reduce(function,input_list) # function to be written without parenthesis
reduce-applies a rolling computation to sequential pair of elements

def function_name(*args):
def function_name(**kargs):
used to take variable arguments
args and kwargs word can be replaced with anything else
args is of type tuple and kwargs is dict
dict take 1 argument at a time and kwargs take key value pair
iterable - object which can give iterator iter() or getitem()
iterator - object in which next() is defined (int not iterable)(iterable - list,tuple,string etc)
iteration - the process of iterating
yield - generates a output as an object in memory one value at a time,makes a iterator
def gen(n):
    for i in range(n+1):
        yield i
for i2 in gen(100):
    print(i2)
print(vars(object)) shows a dict with key as attributes and  values as value of arguments
generators - yield,iter 
print("%c" %(ascii code)
math.ceil(2.1)==3
math.floor(2.1)==2

import time
var=time.time()
# the above is generally used to measure the time taken by a program by defining start and end time
 

Comments

Popular posts from this blog

Dictionary and Sets in Python

Insertion Sort in python

Grid Printing Code for Python