titbits from official tutorial Flashcards

1
Q

how do you get print to treat inputs as raw characters

A

use r e.g. print (r ‘c:\windows\files’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

substring extraction; in str[a:b] what is included and what is exculded?

A

a is included and b is excluded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

name mutable objects in python

A

list, sets, dictionaries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

name some immutable objects in python

A

tuple, string, number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

simplest way to print a sequence of numbers

A

range (start, stop, step)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

by default python source code files are encoded as —-?

A

UTF8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to ensure a newline is NOT entered aftr the print?

A

use keyword end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to ensure a specific character (e.g.a comma) is entered after a print?

A

print(a, end=’,’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

syntax for a lambda function

A

lambda variable: expression involving variable and parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what are 2 conditions under which python does not check for compiled modules

A
  1. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what would you put in __init__.py file in a package

A
  1. the list of symbols imported if import * is used.

2. any other initialization code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is hr equivalent of pritnf(%d) command in python?

A

print(‘Hello {}”.format(variable_name))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are 2 modes in which you can open files in python?

A

text and binary mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are the two formats your data must be in to write to a file?

A

string or bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what method do you use on a file object to get the curent position of the file cursor?

A

fileobject.tell()

17
Q

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

A

“from_wat”
0- from beginning
1- fro current location
2. from end of file; now the offset has to be negative

18
Q

how do you create custom exceptions in python?

A

by deriving from the exception class

19
Q

mantra wile dealing with exceptions

A

baap can handle whatever beta can handle.

20
Q

how would you comprehend list comprehension?

A
by flatenning - unflattened:
for x in data_structure:
        for y in x
             y
[y for x in data_structure for y in x]
21
Q

Is python case sensitive?

A

yes

22
Q

what in built attribute of an object points to it’s class?

A

obj.__class__

23
Q

difference between global and non local keywords (used in front of variable names)

A

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

24
Q

what are some “Treu” quantities in Python

A

non empty string, non zero number, True, -

25
Q

pattern for reading from files

A

f = open(‘filename’, w) –> f.read() or readline() –> f.close()

26
Q

pattern of writing to files

A

f=open(“filename, w) –> f.write()

27
Q

what does // refer to?

A

floor division

28
Q

shorthand notation to `create a sequence out of string (just like slicing a string or list)

A

list[beg:end:step]

beg, beg+1step, beg+2step… untill beg+i*step

29
Q

whats the difference between append and extend for lists

A

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]

30
Q

list.insert(index,object), what is the value of list[index]

A

object

31
Q

what 4 files does pypi need in order to work

A

setup.py, setup.cfg, LICENSE.txt, README.md

32
Q

what command do you run to upload your package to pip python

A

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.

33
Q

how to call the base class constructor from the derived class

A

super(derived_class, self).__init__(value=20)