Python 3 Language Notes - mikedorais/python_learning GitHub Wiki
😃
https://app.pluralsight.com/library/courses/python-fundamentals/
import module
from module import function
from module import function as alias
help()
help(object)
str(object) # converts to string
len(string) # length of string
# specifying other numbers in other bases:
10.5 # decimal no preceding 0
0b0011001 # binary - preface with 0b
0o0712254 # octal - preface with 0o
0x7FF # hexadecimal - prefice with 0x
int(object) # convert to int
int(object, base)
# python floats
# * IEEE-754 precision (64-bit)
# * 53 bits of binary precision
# * 15 to 16 bits of decimal precision
# Any literal number containing a decimal or e suffix
3.125
3e8
1.616ee-35
float(int or str) # convert to float
float("nan")
float("inf")
float("-inf")
None (null) # Never printed in REPL
a = None
a is None # => True
# bool type either True or False
boo(object) # convert to bool
# 0, empty string, empty list, empty tuple, and None are falsey all other truthy
# bool("False") and bool("True") are both true.
# Relational operators: == != < > <= >=
if expr:
# do indented stuff here
else:
# do indented stuff here
if expr:
# do indented stuff here
elif expr:
# do indented stuff here
else:
# do indented stuff here
while expr:
# do indented stuff here
# break breaks out of innermost loop
# ^C (control-C) gets out of an endless loop
# augmented assignment operator
a += b
a -= b
a *= b
# math operators
2 + 2 # => 4
3 - 1 # => 2
5 * 2 # => 10
8 / 4 # => 2
51 % 10 # => 1 modulus
12 // 7 # => 2 integer division`
response = input() # returns the input from the user via <stdin>
# tuple unpacking can be done in for loop
>>> t = [('one, 'two', 'three'), (1, 2, 3), ("a", "b", "C")]
>>> for first, second, third in t:
do_something(first, second, third)
# unique identifier
>>> id(x)
>>> x is b # x refers to the same object as b
# list slice
>>> x = [32, 42, 52, 92, 102]
>>> x[:2]
[32, 42]
>>> x[1:]
[42, 52, 92, 102]
>>> x[2:4]
[52, 92]
>>> x[:] # shallow copy of list
"""mypyfile.py"""
import sys
# Define functions above (e.g. do_stuff())
def main(arg1): # the main function can be named anything. Just call it by name below
print("Gee! I was passed arg1 of {} but I don't know what to do with it!".format(arg1)
do_stuff()
if __name__ == '__main__':
if len(sys.argv) < 2:
# command line parameter not supplied. print error or use default
arg1_to_use = "default argument value"
else:
# sys.argv[0] is the python script name
# sys.argv[1] is the first parameter passed to the script
arg1_to_use = sys.argv[1]
main(arg1_to_use)
$ python3 mypyfile.py
$ python3 mypyfile.py parameter1
>>> import mypyfile
Note: Advanced command line argument parsing: standard library argparse docopt
At top of file (below shebang) the doc string starts with 3 double quotes and ends with 3 double quotes and can span multiple lines. Can follow Google's Style guide:
- Example Google Style Python Docstrings
- Google Python Comments Style Guide from Google Python Style Guide
#!/user/bin/env python3
"""Does something very useful.
Usage:
python3 mypythonscript.py <arg1namedesc> <arg2namedesc>
"""
def myfunction(arg1, arg2)
""""Computes something useful.
Args:
arg1: The first thing needed to compute
something useful.
arg2: The second thing needed to compute
something useful.
Returns:
Something useful derived from the arguments.
"""
Issue converting back to datetime from a string that is ISO format like from datetime.isoformat() when using the built-in python conversion functions to convert from string to datetime using formats. The following shows a common way of dealing with this with the datetutil package. Beware that it may not be perfect for all possible ISO date-time strings. Research it and test it if you depend on it. Time zones are tricky.
$ pip install python-dateutil
> datetimeval = datetime.datetime(2017, 4, 3, 18, 55, 19, 385727)
> dateutil.parser.parse(datetimeval.isoformat()) # converts to ISO format then back to the same date
datetime.datetime(2017, 4, 3, 18, 55, 19, 385727)