-
Notifications
You must be signed in to change notification settings - Fork 0
Python
Python is dynamically typed interpreted object oriented language. Python uses indentation to identify the body of functions, loop and conditions. It is garbage collected.
Uses indentation to denote the body of a loop, function, conditional statements etc. Usually tab space can be set to 4 spaces.
settings for vim,
set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
integer, string, float, complex Boolean - True, False del - to remove the reference of a variable
eg = r"example\n"
r - raw,
""" to span string
literals across multiple lines \
"""
Python strings are immutable
** - exponent
// - Floor
Rest of the operators are same as C.
in, not in - membership operators
is, is not - identity operators
or, and, not - logical operators
if (expression):
statements
elif expression:
statement(s)
else: statement
for iterating_var in sequence:
statement(s)
for i in range(10)
for i in range(5,10)
range generates arithmetic progressions
while expression:
statement(s)
has else statement which can be tagged along with end of loop. executed unless loop is terminated by break
break and continue statements similar to C
def function_name (parameters):
"""function documentation"""
statements
return [expression]
All parameters are passed by reference argument types: required, keyword, default, variable length
Variable length args,
def function_name ([formal_args], *var_args_tuple)
Anonymous functions,
lambda [arg1 [,arg2,.....argn]]:expression
return statement with no arguments is same as return None
local and global variables same as C.
global variable_name - to access global variable
pass - statement is a no op. useful for empty api documentation
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
list[start:stop:step] to slice a list
list [::-1] - reverses the list
Looping for i in list: print i
for q, a in zip(questions, answers):
print (q, a)
zip - pair multiple sequences and loop through,
for i, v in enumerate(['tic', 'tac', 'toe']):
print(i, v)
fetch index and value of a sequence using enumerate
It is a immutable list
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
dict = { jack': 4098, 'jones': 4139}
dict['first'] = "Add the first item"
dict[2] = "This is two"
for k, v in dict.items():
print(k, v)
set = { 'apple', "orange" }
'apple' in set
- find if apple is a member of set.
A module is a file containing python code. modules provide namespacing.
import module1[, module2[,... moduleN] - to import whole module
from module_name import func_name
from module_name import * - lets you access function_names, variables without specifying module_name
Modules are searched first in current directory, then PYTHONPATH and default installation dependent path - /usr/local/lib/python/
Package is a collection of modules specified in init.py . Directory name being the package name.
__all__ = [module1, module2] (in __init__.py)- for supporting from package_name import *
dir(package_name) - outputs all the names defined in a module
globals() - returns global names
locals() - from a function returns local names
reload(module_name) - to execute the top level code of module again.
if __name__ == "__main__":
functions to run when the module is run as a script