Python Flashcards
Explain ‘os’ module usages
os.mkdir(aPath) os.listdir(aPath) os.path.isdir(aPath) os.system(command)
Explain ‘sys’ module methods and variables.
sys. argv[] sys.exit() sys.builtin_module_names # Linked C modules
sys. byteorder # Native byte order
sys. check_interval Signal check frequency
sys. exec_prefix #Root directory
sys. executable #Name of executable
sys. exitfunc #Exit function name
sys. modules #Loaded modules
sys. path #Search path
sys. platform #Current platform
sys. stdin, sys.stdout, sys.stderr # File objects for I/O
sys. version_info #Python version info
sys. winver #Version number
Function definition in Python
Python function definition
def myFunction(arg1, arg2=5, arg3=”default’’):
…
return result
Python ‘List’ methods
append(item)
pop(position)
count(item)
remove(item)
extend(list)
reverse()
index(item)
sort()
insert(position, item)
Python ‘String’ methods
capitalize() *
lstrip()
center(width)
partition(sep)
count(sub, start, end)
replace(old, new)
decode()
rfind(sub, start ,end)
encode()
rindex(sub, start, end)
endswith(sub)
rjust(width)
expandtabs()
rpartition(sep)
find(sub, start, end)
rsplit(sep)
index(sub, start, end)
rstrip()
isalnum() *
split(sep)
isalpha() *
splitlines()
isdigit() *
startswith(sub)
islower() *
strip()
isspace() *
swapcase() *
istitle() *
title() *
isupper() *
translate(table)
join()
upper() *
ljust(width)
zfill(width)
lower() *
* are local-dependent for 8 bit scales.
Python ‘File’ methods
close()
readlines(size)
flush()
seek(offset)
fileno()
tell()
isatty()
truncate(size)
next()
write(string)
read(size)
writelines(list)
readline(size)
Python indexes and slices
* Indexes and Slices of a=[0,1,2,3,4,5]
len(a) –> 6
a[0] –> 0
a[5] –> 5
a[-1] –> 5
a[-2] –> 4
a[1:] –> [1,2,3,4,5]
a[:5] –> [0,1,2,3,4]
a[:-2] –> [0,1,2,3]
a[1:3] –> [1,2]
a[1:-1] –> [1,2,3,4]
b=a[:] –> Shallow copy of a
Python String formating
arg_str = “ {0} {1} {3}”.format(arg1, arg2, arg3)
Python loop using List
for pic in pic_list:
…
Python hash (dictionary): Definition
dict = {‘Alice’: ‘2341’, ‘Beth’: ‘9102’, ‘Cecil’: ‘3258’}
Python hash (dictionary): Value access
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]
Python hash (dictionary): Removing elements
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
del dict[‘Name’] # remove entry with key ‘Name’
dict.clear() # remove all entries in dict
del dict # delete entire dictionary
Python hash (dictionary): Methods
- dict.clear() # Removes all elements of dictionary
- dict.copy() # Returns a shallow copy of dictionary dict
- dict.fromkeys() # Create a new dictionary with keys from seq and values set to value.
- dict.get(key, default=None) # For key key, returns value or default if key not in dictionary
- dict.has_key(key) # Returns true if key in dictionary dict, false otherwise
- dict.items() # Returns a list of dict’s (key, value) tuple pairs
- dict.keys() # Returns list of dictionary dict’s keys
- dict.setdefault(key, default=None) # Similar to get(), but will set dict[key]=default if key is not already in dict
- dict.update(dict2) # Adds dictionary dict2’s key-values pairs to
- dict.values() # Returns list of dictionary dict2’s values
Python hash (dictionary): functions on hash data structure
- cmp(dict1, dict2) # Compares elements of both
- str(dict) # Produces a printable string representation of a dictionary
- type(variable) # Returns the type of the passed variable. If passed variable is dictionary then it would return a dictionary type.
Example: Use Python ‘List’ as a stack
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]