Python Fundamentals #
Compilation of Python code that I’ve found useful.
import sys
print(f"* Python version:\n{sys.version}\n")
* Python version:
3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
Suppress Warnings#
import warnings
warnings.filterwarnings('ignore')
# Or do filter FutureWarnings specifically
warnings.simplefilter(action='ignore', category=FutureWarning)
String Manipulation #
x="J"
print(f'Hello, {x}! How are you?')
print(f'Hello, {x.lower()}! How are you?')
print(f'Hello, {x}!','How are you?', sep="\n")
print(f"{2*2}")
print(f'{3.1415:.2}')
Hello, J! How are you?
Hello, j! How are you?
Hello, J!
How are you?
4
3.1
word = 'Hello'
print(word[0]) # one char of the word
print(word[0:1]) # one char of the word (same as above)
print(word[0:3]) # the first three char
print(word[:3]) # the first three char
print(word[-3:]) # the last three char
print(word[3:]) # all but the three first char
print(word[:-3]) # all but the three last character
print(word[::-1]) # reversed
H
H
Hel
Hel
llo
lo
He
olleH
word = "Hello World"
print(word.count('o')) # counting the character “o” in the givenstring
word.split(' ') # Split on whitespace | ['Hello', 'World']
word.replace("Hello", "Goodbye") # 'Goodbye World'
print()
print(" ".join(word)) # add a whitespace between every char
print(":".join(word)) # add a : between every char
print(' '.join(reversed(word))) # d l r o W o l l e H
print()
print(word.title()) # Hello World
print(word.capitalize()) # Hello world
print(word.swapcase()) # hELLO wORLD
print()
print(word.strip()) # removes from both ends
print(word.lstrip()) # removes leading characters (Left-strip)
print(word.rstrip()) # removes trailing characters (Right-strip)
2
H e l l o W o r l d
H:e:l:l:o: :W:o:r:l:d
d l r o W o l l e H
Hello World
Hello world
hELLO wORLD
Hello World
Hello World
Hello World
word.isalnum() # check if all char are alphanumeric
word.isalpha() # check if all char in the string are alphabetic
word.isdigit() # test if string contains digits
word.istitle() # test if string contains title words
word.isupper() # test if string contains upper case
word.islower() # test if string contains lower case
word.isspace() # test if string contains spaces
word.endswith('d') # test if string endswith a d
word.startswith('H') # test if string startswith H
True
Loops #
# range() function defaults to 0 as a starting value
for n in range(10):
print(n, end=' ') # print all on same line
print()
start = 1
end = 11
inc = 2
for n in range(start, end, inc):
print(n, end=' ') # print all on same line
0 1 2 3 4 5 6 7 8 9
1 3 5 7 9
List comprehension
print([i for i in range(10)])
# Select only even values
print([i for i in range(10) if (i % 2) == 0])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]
# With the continue statement we can stop the current iteration of the loop, and continue with the next:
for n in range(20):
if n % 2 == 0:
continue
print(n, end=' ')
1 3 5 7 9 11 13 15 17 19
# With the break statement we can stop the loop before it has looped through all the items:
# Print Fibonacci sequence for values <= 100
a, b = 0, 1
amax = 100
L = []
while True:
(a, b) = (b, a + b)
if a > amax:
break
L.append(a)
print(L)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
print(*range(5)) # same as print(0, 1, 2, 3, 4)
0 1 2 3 4
enumerate
n = [25, 63, 145, 12]
for i, n in enumerate(n):
print(i, n)
0 25
1 63
2 145
3 12
Data Types #
Dictionaries#
d = {'pi_approx': 3.14159265359,
'e_approx': 2.71828182846,
'c_approx': '299,792,458 m/s',
'h_bar_approx': '6.62607004e-34 m^2 kg / s'}
print('d =', d)
d[0] = 0.0
d[1] = 1.0
d[-1] = -1.0
d['minus_one'] = d[-1]
print("\n", len(d), 'key-value pairs:', d)
# Deletion of keys:
d.pop('minus_one')
# Return key-value
print(d['c_approx'])
# List of dictionaries
data = [{'first':'Guido', 'last':'Van Rossum', 'YOB':1956},
{'first':'Grace', 'last':'Hopper', 'YOB':1906},
{'first':'Alan', 'last':'Turing', 'YOB':1912}]
sorted(data, key=lambda x: x['YOB'])
'''
[{'first': 'Grace', 'last': 'Hopper', 'YOB': 1906},
{'first': 'Alan', 'last': 'Turing', 'YOB': 1912},
{'first': 'Guido', 'last': 'Van Rossum', 'YOB': 1956}]
'''
# Sorting dictionaries
my_dict = {1: 2, 2: 10, "Hello": 1234}
print({key: value for key, value in sorted(my_dict.items(), key=lambda item: item[1])})
# {1: 2, 2: 10, 'Hello': 1234}
# Iterating over dictionaries
D = {k: v for v, k in enumerate('abcdef')}
print(D)
'''
Default Dictionaries
Python's built-in dictionaries require you to check whether a key exists before updating it.
A way around that is to use default dict
'''
from collections import defaultdict
# you need to provide a "factory" function that the dictionary can use to create an initial value when the key does not exist.
d = defaultdict(int)
d['existing-key'] = 5 # Create one key-value pair
d['existing-key'] += 1 # Update
d['new-key'] += 1
print('\n', d)
d = {'pi_approx': 3.14159265359, 'e_approx': 2.71828182846, 'c_approx': '299,792,458 m/s', 'h_bar_approx': '6.62607004e-34 m^2 kg / s'}
8 key-value pairs: {'pi_approx': 3.14159265359, 'e_approx': 2.71828182846, 'c_approx': '299,792,458 m/s', 'h_bar_approx': '6.62607004e-34 m^2 kg / s', 0: 0.0, 1: 1.0, -1: -1.0, 'minus_one': -1.0}
299,792,458 m/s
{1: 2, 2: 10, 'Hello': 1234}
{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5}
defaultdict(<class 'int'>, {'existing-key': 6, 'new-key': 1})
Lists#
list_from_string = list('abcdefg')
print(list_from_string)
range_list = list(range(10))
print(range_list)
# Sorting lists, can also specify a sorting function with key=func
range_list.sort(reverse=True)
print(range_list)
range_list.sort(reverse=False)
print(range_list)
y = [1,2,3]
z = y.copy() # .copy() can be used to clone a list to be distinct references.
y.append(4)
print(z)
g = [i for i in range(10)] # 0 .. 9
from random import shuffle
shuffle(g) # permute randomly
print(g)
# Select only even values
h = [i for i in g if (i % 2) == 0]
print(h)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3]
[5, 1, 3, 8, 4, 9, 0, 6, 7, 2]
[8, 4, 0, 6, 2]
Sets#
# Sets are unordered, unchangeable, unindexed, and duplicates are not allowed.
# Python is free to reorder your values
s = 'quux'
s = set(s)
print(s)
s.add('p')
print(s)
# Removes a random element from a set.
s.pop()
print(s)
s.update({1,2,3})
print(s)
print('x' in s)
print('y' in s)
# Clears a set.
s.clear()
print(s)
A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
print(A, '&', B, '==', A & B) # intersection
print(A, '|', B, '==', A | B) # union
print(A, '-', B, '==', A - B) # difference
print(A, '<', B, '==', A < B) # proper subset (use `<=` for subset, `>` or `>=` for superset, or '==' for equality)
{'x', 'q', 'u'}
{'x', 'p', 'q', 'u'}
{'p', 'q', 'u'}
{1, 'p', 'q', 2, 3, 'u'}
False
False
set()
{1, 2, 3, 4, 5} & {3, 4, 5, 6, 7} == {3, 4, 5}
{1, 2, 3, 4, 5} | {3, 4, 5, 6, 7} == {1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5} - {3, 4, 5, 6, 7} == {1, 2}
{1, 2, 3, 4, 5} < {3, 4, 5, 6, 7} == False
Tuples
#
Ordered, unchangeable, and allow duplicate values.
t = ("apple", "banana", "cherry")
print(t[0])
print(len(t))
apple
3
Dates#
Python strftime cheatsheet: https://strftime.org/
Example: ‘05SEP2014:00:00:00.000’=’%d%b%Y:%H:%M:%S.%f’
from datetime import datetime
date_str = '2020-10-31'
date = datetime.strptime(date_str, '%Y-%m-%d')
print(date)
2020-10-31 00:00:00
date_str = date.strftime("%Y-%m-%d")
print(date_str)
2020-10-31
print("Today:", datetime.now().strftime("%A, %d %b %Y"))
Today: Sunday, 20 Oct 2024
Random#
https://docs.python.org/3/library/random.html
https://www.w3schools.com/python/module_random.asp
import random
# random.randint(start, stop)
print(random.randint(3, 9))
3
Timeit #
import time
t1 = time.time()
print("Hi!")
t2 = time.time()
print(f"The time taken by print statement is {t2 - t1} seconds")
Hi!
The time taken by print statement is 0.001001596450805664 seconds
'''
%timeit - Measure execution time of small code snippets
from random import random
from decimal import Decimal
NUM_TRIALS = 0
print("Native arithmetic:")
A_native = [random() for _ in range(NUM_TRIALS)]
B_native = [random() for _ in range(NUM_TRIALS)]
%timeit [a+b for a, b in zip(A_native, B_native)]
print("\nDecimal package:")
A_decimal = [Decimal(a) for a in A_native]
B_decimal = [Decimal(b) for b in B_native]
%timeit [a+b for a, b in zip(A_decimal, B_decimal)]
'''
'\n%timeit - Measure execution time of small code snippets\n\nfrom random import random\nfrom decimal import Decimal\n\nNUM_TRIALS = 0\n\nprint("Native arithmetic:")\nA_native = [random() for _ in range(NUM_TRIALS)]\nB_native = [random() for _ in range(NUM_TRIALS)]\n%timeit [a+b for a, b in zip(A_native, B_native)]\n\nprint("\nDecimal package:")\nA_decimal = [Decimal(a) for a in A_native]\nB_decimal = [Decimal(b) for b in B_native]\n%timeit [a+b for a, b in zip(A_decimal, B_decimal)]\n'
Misc #
Windows Paths #
path = r'C:\Users\eric_\Documents\Code\ChromeDriver\chromedriver.exe'
Assertions#
def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
KelvinToFahrenheit(1)
-457.6
'''
One line if statement
value_when_true if condition else value_when_false
Note: You cannot use Python if..elif..else block in one line.
Well you kind of can, but you shouldn't because it is hard to read.
'''
a = 1
b = 'Positive' if a > 0 else 'Negative'
print(b)
# Split string and assign variables to each part
helloworld = 'hello world'
hello, world = helloworld.split(' ')
print(hello,world)
Positive
hello world
Pip#
https://pip.pypa.io/en/stable/user_guide/
pip install –upgrade pip
pip install SomePackage –upgrade
pip uninstall SomePackage
pip install sklearn –upgrade
See outdate packages:
pip list –outdated
Automatically upgrade all the outdated packages (that were installed using pip):
pip install $(pip list --outdated | awk '{ print $1 }') --upgrade
Another option
for i in $(pip list --outdated --format=columns |tail -n +3|cut -d" " -f1); do pip install $i --upgrade; done
Regular Expressions#
https://docs.python.org/3/howto/regex.html
Build/test expressions - https://regex101.com/
import re
##################
# METACHARACTERS #
##################
# [ ] - [abc] will match to any of the characters a, b, c. The same as [a-c]. Matches to metacharacters literally
# [^5] will match any character except '5'. '^' only works if its the first character. [5^] will match to either 5 or '^'
# [\s,.] any whitespace, or ',' or '.'
# \ - can be used to escape all metacharacters, \[] would match literally to '[]'
# . - any character except a newline character. alternate mode (re.DOTALL) will match to even newlines
#
#
# \d - any decimal, = [0-9]
# \D - non-digit, = [^0-9]
#
# \s - whitespace, = [ \t\n\r\f\v]
# \S - non-whitespace, = [^ \t\n\r\f\v]
#
# \w - alphanumeric, = [a-zA-Z0-9_]
# \W - non-alphanumeric, = [^a-zA-Z0-9_]
##################
# REPEAT METACHARS #
##################
# * - previous character can be matched zero or more times
# ca*t will match ct, cat, caaaaat
#
# + - will match one or more times
# ca+t will match cat, caaaaat
#
# ? - matches zero or one times.
# home-?brew matches homebrew or home-brew
#
# {m,n} - at least m, at most n repeats. Omitting 'm' will be interpreted as 0. 'n' will be infinity
# a/{1,3}b will match a/b, a//b, and a///b
##################
# | - 'or' operator, A|B
# ^ - beginning lines
# $ - end of a line
# \A - start of string
# \Z - end of string
# \b - beginning or end of a word(sequence of alphanumeric characters)
# \B - only matches when current position is not a word boundary
##################
# COMPILATION FLAGS #
##################
# ACSII, A - makes escapes like \w, \b, \s match only on ASCII characters
# DOTALL, S - make '.' match any character, including newlines
# IGNORECASE, I
# MULTILINE, M - affecting '^', and '$'
pattern_matcher = re.compile ('fox', re.IGNORECASE)
matches = pattern_matcher.search ('The quick brown fox jumps over the lazy dog')
##################
# PATTERN MATCHERS #
##################
# match() - if RE matches at the beginning of the string
# search() - any location where the RE matches
# findall() - all substrings where RE matches, returns list
# finditer() - all substrings where RE matches, returns iterator
print (matches)
print (matches.group ()) # group() - return the string matched by RE
print (matches.start ()) # start() - return the starting position of the match
print (matches.end ()) # end() - return the ending position of the match
print (matches.span ()) # span() - return a tuple containing the (start,end) positions
<re.Match object; span=(16, 19), match='fox'>
fox
16
19
(16, 19)
##################
# LOOKAHEAD ASSERTIONS #
##################
# (?=...) - positive lookahead
# (?!...) - negative lookahead
# (?P<name>...) - named group. Grab the names with match.group(). match.groupdict() returns a dict of groups
# example: (?P<first>[a-zA-Z]+)
# Make the above more readable with a re.VERBOSE pattern
# Named groups <name>
re_names3 = re.compile ('''^
(?P<first>[a-zA-Z]+)
\s
(?P<middle>[a-zA-Z]+\s)?
\s*
(?P<last>[a-zA-Z]+)
$
''',
re.VERBOSE)
print (re_names3.match ('Rich Vuduc').group ('first'))
print (re_names3.match ('Rich S Vuduc').group ('middle'))
print (re_names3.match ('Rich Salamander Vuduc').group ('last'))
Rich
S
Vuduc
s = '(319) 555-1111'
pattern = '''
^\s* # Leading spaces
(?P<areacode>
\d{3}-? # "xxx" or "xxx-"
| \(\d{3}\)\s* # OR "(xxx) "
)
(?P<prefix>\d{3}) # xxx
-? # Dash (optional)
(?P<suffix>\d{4}) # xxxx
\s*$ # Trailing spaces
'''
matcher = re.compile(pattern, re.VERBOSE)
matches = matcher.match(s)
if matches is None:
raise ValueError("'{}' is not in the right format.".format (s))
areacode = re.search('\d{3}', matches.group ('areacode')).group()
prefix = matches.group ('prefix')
suffix = matches.group ('suffix')
(areacode, prefix, suffix)
('404', '555', '1212')