FrancoisBotha.io
Back

Python Quick Start Reference

This post is a quick reference and introduction to Python.

Variables

Variable names must start with a letter or an underscore. There are 4 kinds of numbers: integers, floats, complex numbers and Booleans.

Floating point division:

x = 9
y = 4
z = x / y
# = 2.25

Integer division:

z = x // y
# z = 2

Strings:

x = """Starting and ending a string with triple " characters
permits embedded newlines, and the use of " and ' without
backslashes"""

None

-> Like null, i.e. placeholder for some future value

Data Structures

Lists

Similar to arrays. Automatically grows as required. Can mix up date types in it.

x = [1, 2, 3]

Example:

my_list = ["apple", "banana", "cherry", "date"]

for item in my_list:
    print(item)
my_list[0]
# 'apple'

my_list[-1]
# 'date'

my_list[1:3]
# ['banana', 'cherry']

# Make a copy of a list
x = my_list[:]
# x = ['apple', 'banana', 'cherry', 'date']

# Add element at the end
my_list.append('pear')
# x = ['apple', 'banana', 'cherry', 'date', 'pear']

# Remove the first instance of an occurence
my_list.remove('banana')
my_list
['apple', 'cherry', 'date', 'pear']

# List membership
'apple' in my_list
# True

Sorting a list:

my_list.sort()

Can concatenate lists using + operator.

Two dimensional matrices:

m = [[0, 1, 2], [10, 11, 12], [20, 21, 22]]
m[2][2]
# 22

Create a deep copy of a list

original = [[0], 1]
shallow = original[:]
import copy
deep = copy.deepcopy(original)

Other

  • len
  • min
  • max
  • index
  • count
  • sum

Tuples

Similar to lists, but cannot be modified. E.g.

x = ('a', 'b', 'c')

Sets

E.g.

x = set([1, 2, 3, 1, 3, 5])
# x = {1, 2, 3, 5}

Strings

Like arrays of characters, but imutable. Can be concatenated with '+' operator (but join operator is more efficient).

x = "Goodbye\n"
x = x[:-1]
# x = 'Goodbye'

len("Goodbye")
# 7 

" ".join(["join", "puts", "spaces", "between", "elements"])
# 'join puts spaces between elements'

"".join(["Separated", "by", "nothing"])
# 'Separatedbynothing'

Other

  • split
  • strip, lstrip, rstrip
  • find
  • index
  • count
  • startswith, endswith
  • replace
  • isdigit
  • isalpha
  • islower
  • isupper
  • upper
  • lower
  • title
  • repr / str (converts an object to a string)

Can also convert to a list, manipulate and convert back:

text = "Hello World"
wordList = list(text)
wordList.reverse()
text = "".join(wordList)
print(text)
# dlroW olleH

String interpolation:

"%s is the %s of %s" % ("Ambrosia", "food", "the gods")
# 'Ambrosia is the food of the gods'

value = 42
message = f"The answer is {value}"
print(message)
# The answer is 42

Example of reading and cleaning an input file:

with open("moby_01.txt") as infile, open("moby_01_clean.txt", "w") as
outfile:
    for line in infile:
        # make all one case
        cleaned_line = line.lower()
        # remove punctuation
        cleaned_line = cleaned_line.translate(punct)
        # split into words
        words = cleaned_line.split()
        cleaned_words = "\n".join(words)
        # write all words for line
        outfile.write(cleaned_words)

Dictionaries

english_to_french = {'red': 'rouge', 'blue': 'bleu', 'green': 'vert'}
print("red is", english_to_french['red'])
# red is rouge

list(english_to_french.keys())
# ['green', 'blue', 'red']

list(english_to_french.values())
# ['vert', 'bleu', 'rouge']

del english_to_french['green']
# [('blue', 'bleu'), ('red', 'rouge')]

'red' in english_to_french
# True

print(english_to_french.get('chartreuse', 'No translation'))
# No translation

#-> Word count example:
    for word in sample_string.split():
        occurrences[word] = occurrences.get(word, 0) + 1

Control Flow

# -> While Loop:
while condition:
    body

# -> If stament
if condition1:
    body1
elif condition2:
    body2
else
    body3

# -> For loop
for item in sequence:
    body (incl break and continue)