Difference between revisions of "Python"
(→time) |
|||
Line 20: | Line 20: | ||
− | We try to use modules that are available by default (on linux systems). If not it will be mentioned in | + | We try to use modules that are available by default (on linux systems). If not it will be mentioned in this article. |
+ | Only modules for which we use a very limited number of functions are listed here. More complex modules have there own article | ||
+ | |||
==sys== | ==sys== | ||
Provides a number of system variables | Provides a number of system variables | ||
Line 68: | Line 70: | ||
The CompletedProcess returned has (args, returncode, stdout, stderr) | The CompletedProcess returned has (args, returncode, stdout, stderr) | ||
− | == | + | ==random== |
− | + | Generate random numbers. | |
+ | ;random.random() | ||
+ | :Return a floating point in the range from 0.0 to 1.0 (including both) | ||
− | + | ;random.randint(start,stop) | |
− | + | :Return an integer in the range from start to stop (including both) | |
− | + | :Pick a random element from a list: | |
− | + | <syntaxhighlight lang=python> | |
− | + | list[random.randint(0,len(list)-1)] | |
− | + | list[int(random.random()*len(list))] | |
− | + | </syntaxhighlight> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Variables= | =Variables= |
Revision as of 12:08, 19 April 2019
Generic things:
The Python style guide is available in [PEP 8]
Modules
Modules need to be imported into your program by the import
command.
To add the location of your own modules to the python search path put it in the PYTHONPATH variable.\
- import <module>
- Import everything from the module, address components as <module>.<component>.
- import <module> as <short>
- Calls can have the short name. E.g. numpy is often imported as np
- from <module> import *
- Module components can be called without the module name. Beware of duplicates.
- from <module> import <component>
- Import a specific component from a modules, callable by just the component name.
We try to use modules that are available by default (on linux systems). If not it will be mentioned in this article.
Only modules for which we use a very limited number of functions are listed here. More complex modules have there own article
sys
Provides a number of system variables
- sys.argv
- List of everything on the commandline. sys.argv[0] is the program itself.
- sys.version
- The python version you run
getopt
Module to parse the commandline arguments (sys.argv).
datetime
Date and time functions
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
time
Time functions
- time.sleep(3)
- Sleep for 3 seconds
from time import sleep
sleep(3)
subprocess
Module to execute shell commands
In python2
import subprocess
exitcode = subprocess.call("<any command>")
commandoutput = subprocess.check_output("<any command>")
Use ("command",shell=True) to have the call work like it would on the commandline
In python3
import subprocess
CompletedProcess = subprocess.run("<any command>")
The CompletedProcess returned has (args, returncode, stdout, stderr)
random
Generate random numbers.
- random.random()
- Return a floating point in the range from 0.0 to 1.0 (including both)
- random.randint(start,stop)
- Return an integer in the range from start to stop (including both)
- Pick a random element from a list:
list[random.randint(0,len(list)-1)]
list[int(random.random()*len(list))]
Variables
Everything is an object in python. Objects can be variables and functions.
Variables are always pointers to objects.
a = 2
b = 2
Both a and b point to the same object (the immutable integer '2')
Variables are local by default. If a routine has any assignment to a variable it is local. If you have defined a variable outside a routine and need assignments to it in the routine you have to declare it global explicitly.
a = 'a string'
def main():
global a
print(a)
a = "This would fail with 'local variable 'a' referenced before assignment' if 'a' was not declared as global"
main()
[Geeks for Geeks] has as good page about this.