chapter 1 Flashcards
Code compilation
source code -> byte code and byte code is platform independent
what does a byte code look like
*.pyc
sometimes, the *.pyc cannot be written into your system, but the script is still be run
PVM
The last step of what is called the Python interpreter
python virtual machine that iterates through your byte code instructions one by one.
exit an interactive python session
ctrl + D
When do we have to use print
in the text file, we have to use print to show the result
in the interactive window, we don’t need to
route the output of a Python script to a file to save it for later use
python filename.py > filename
import script
> > > import script
top-level file
one which launches the entire program
what will import do
find the files->compile them to byte code->run the code
So import is an expensive operation
How to reload the same module
reload(module name)
what is the difference between a reload and an import
reload must have a parenthesis and import don’t
Mostly, what is a module
More generally, a module is mostly just a package of variable names, known as a namespace
how to use the content in the module
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 to import a specific attribute from a module
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
What is the difference between from filename import attributes
and import wholemodule
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 to show the available attributes in the module we defined
dir(module_name)
What is the largest program structure in python?
module
modules and namespace
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.
The characteristics of running a module using execfile(‘module.py’)
every time we use execfile to run a module, the module is runs anew
which environmental variable specifies the searching path of modules
PYTHONPATH which initialize the sys.path attribute
When you run a module, you have to reload the nested moduels
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
Dynamically typed
Python tracks the types automatically for us. We don’t need to declare the types explicitly for variables
What is the difference between the expression
3.14152
and
print 3.14152
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
how to represent string
single quote and double quote work the same
check the length of a tring
len(‘string’) which is a built-in command
ways to access a string
and the interesting way
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
A Gotcha
S[1:3} gives the content ‘pa’ if S is ‘Spam’ which doesn’t include m.
fortran style access to a string
S[1:] 'pam' S[0:3} 'Spa' S[:3] 'Spa' ***S[:-1] 'Spa' S[:] 'Spam'
string concatenation
operator is + like C++ S+'xyz' 'Spamxyz' operator * S*8 'SpamSpam~~~~~Spam'
Immutability of a string
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
Is a tuple mutable?
No, it is not.
substring search operation
stringname.find(‘substring’)
Replace occurrences of a substring with another
stringname.replace(‘substring’,substitute’)
is Python case sensitive?
Yes, Python is case sensitive
Split a string into substrings on a delimiter
stringname.split(‘delimiter’)
see the callable command for a string
dir(stringname) shows all the callable command
help(stringname.command) gives the command detailed help
how to check the binary value of a string
ord(‘string_name’)