chapter 1 Flashcards

1
Q

Code compilation

A

source code -> byte code and byte code is platform independent

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

what does a byte code look like

A

*.pyc

sometimes, the *.pyc cannot be written into your system, but the script is still be run

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

PVM

The last step of what is called the Python interpreter

A

python virtual machine that iterates through your byte code instructions one by one.

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

exit an interactive python session

A

ctrl + D

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

When do we have to use print

A

in the text file, we have to use print to show the result

in the interactive window, we don’t need to

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

route the output of a Python script to a file to save it for later use

A

python filename.py > filename

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

import script

A

> > > import script

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

top-level file

A

one which launches the entire program

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

what will import do

A

find the files->compile them to byte code->run the code

So import is an expensive operation

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

How to reload the same module

A

reload(module name)

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

what is the difference between a reload and an import

A

reload must have a parenthesis and import don’t

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

Mostly, what is a module

A

More generally, a module is mostly just a package of variable names, known as a namespace

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

how to use the content in the module

A

First, the module must be imported. Second, the attribute in the file should be used with prefix which is the name of the file

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

How to import a specific attribute from a module

A

from module_name import attribute name

In this case, the use of the attribute is direct and no prefix is needed to specified in the command

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

What is the difference between from filename import attributes
and import wholemodule

A

The import command executes the module and from ~~ copy the attribute names. That is why in the second case, the prefix is not needed

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

How to show the available attributes in the module we defined

A

dir(module_name)

17
Q

What is the largest program structure in python?

A

module

18
Q

modules and namespace

A

the variables in one module is self-contained and independent of other modules. It cannot see other attributes in other modules except it imports other file explicitly.

19
Q

The characteristics of running a module using execfile(‘module.py’)

A

every time we use execfile to run a module, the module is runs anew

20
Q

which environmental variable specifies the searching path of modules

A

PYTHONPATH which initialize the sys.path attribute

21
Q

When you run a module, you have to reload the nested moduels

A

Because if you have import the nested module before, you may need to reload the module again if you run the module which nests other modules

22
Q

Dynamically typed

A

Python tracks the types automatically for us. We don’t need to declare the types explicitly for variables

23
Q

What is the difference between the expression
3.14152
and
print 3.1415
2

A

The result of the first one is 6.283000000000…4 which is not user friendly
the result of the second one is 6.283 which is user friendly

24
Q

how to represent string

A

single quote and double quote work the same

25
Q

check the length of a tring

A

len(‘string’) which is a built-in command

26
Q

ways to access a string

and the interesting way

A
s='Spam'
s[0]='S'
s[1]='p'
interesting way:
s[-1]: the last item in S
We should notice that s[len(S)-1] is the last character in the string
27
Q

A Gotcha

A

S[1:3} gives the content ‘pa’ if S is ‘Spam’ which doesn’t include m.

28
Q

fortran style access to a string

A
S[1:] 'pam'
S[0:3} 'Spa'
S[:3] 'Spa'
***S[:-1] 'Spa'
S[:] 'Spam'
29
Q

string concatenation

A
operator is + like C++
S+'xyz'     'Spamxyz'
operator *
S*8
'SpamSpam~~~~~Spam'
30
Q

Immutability of a string

A

A string cannot be changed in python once they are created

So in order to ‘change’ it, we just create a new one with the same name

31
Q

Is a tuple mutable?

A

No, it is not.

32
Q

substring search operation

A

stringname.find(‘substring’)

33
Q

Replace occurrences of a substring with another

A

stringname.replace(‘substring’,substitute’)

34
Q

is Python case sensitive?

A

Yes, Python is case sensitive

35
Q

Split a string into substrings on a delimiter

A

stringname.split(‘delimiter’)

36
Q

see the callable command for a string

A

dir(stringname) shows all the callable command

help(stringname.command) gives the command detailed help

37
Q

how to check the binary value of a string

A

ord(‘string_name’)