titbits from official tutorial Flashcards
how do you get print to treat inputs as raw characters
use r e.g. print (r ‘c:\windows\files’)
substring extraction; in str[a:b] what is included and what is exculded?
a is included and b is excluded.
name mutable objects in python
list, sets, dictionaries
name some immutable objects in python
tuple, string, number
simplest way to print a sequence of numbers
range (start, stop, step)
by default python source code files are encoded as —-?
UTF8
how to ensure a newline is NOT entered aftr the print?
use keyword end
how to ensure a specific character (e.g.a comma) is entered after a print?
print(a, end=’,’)
whats the difference between a set of statements enclosed by an else and followed by a for loop and a set of statements followed by a for loop (without the else)
If the loop has a break statement and it was exited through execution of that break as opposed to natural exhaustion of the sequence it was iterating over, then the statement in else block WILL NOT be executed whereas a block not enclosed by else WILL BE executed.
syntax for a lambda function
lambda variable: expression involving variable and parameter
what are 2 conditions under which python does not check for compiled modules
- if the module is run from the python command line
2. if there is no source module, i.e, only the compiled code exists in the current directory
what would you put in __init__.py file in a package
- the list of symbols imported if import * is used.
2. any other initialization code.
what is hr equivalent of pritnf(%d) command in python?
print(‘Hello {}”.format(variable_name))
what are 2 modes in which you can open files in python?
text and binary mode.
what are the two formats your data must be in to write to a file?
string or bytes
what method do you use on a file object to get the curent position of the file cursor?
fileobject.tell()
Moving a file pointer’ cursor to a new location is done via seek which has 2 args - one is offset, what is the other and what values can it take and what do they mean
“from_wat”
0- from beginning
1- fro current location
2. from end of file; now the offset has to be negative
how do you create custom exceptions in python?
by deriving from the exception class
mantra wile dealing with exceptions
baap can handle whatever beta can handle.
how would you comprehend list comprehension?
by flatenning - unflattened: for x in data_structure: for y in x y [y for x in data_structure for y in x]
Is python case sensitive?
yes
what in built attribute of an object points to it’s class?
obj.__class__
difference between global and non local keywords (used in front of variable names)
global - used to indicate that a particular variable lives in the global namespace and should be rebound there.
nonlocal - used to indicate that a particular variable lives in the enclosing namespace and must be rebound there
what are some “Treu” quantities in Python
non empty string, non zero number, True, -
pattern for reading from files
f = open(‘filename’, w) –> f.read() or readline() –> f.close()
pattern of writing to files
f=open(“filename, w) –> f.write()
what does // refer to?
floor division
shorthand notation to `create a sequence out of string (just like slicing a string or list)
list[beg:end:step]
beg, beg+1step, beg+2step… untill beg+i*step
whats the difference between append and extend for lists
a = [1,2,3] b =[5,6]
a. append(b); now a willbe [1,2,3, [5,6]]
a. extend(b); now a will be [1,2,3,5,6]
list.insert(index,object), what is the value of list[index]
object
what 4 files does pypi need in order to work
setup.py, setup.cfg, LICENSE.txt, README.md
what command do you run to upload your package to pip python
python setup.py register sdist upload
regsiter name on pypi
sdist - create a source distribution in the name of the tarfile in the top level of your source code
upload -command to uplaod.
how to call the base class constructor from the derived class
super(derived_class, self).__init__(value=20)