Skip to main content

Reference Sheet For Assessments

  • Note: words in (these) indicate a thing you will have to write, the parentheses are not part of the syntax.
  • Note: variable placeholders will use x, y, and so on.

Python

Shebang

#!/usr/bin/env python3

Built-in Functions

  • help()
  • type()
  • print()
  • input()
  • int()
  • str()
  • float()
  • len()
  • round()

Conditions

if (condition):
(action1)
elif (condition2):
(action2)
else:
(action)

Loops

while (condition1):
(action)
if (condition2):
break
for x in y:
(action)
for i in range(x, y):
(action)

Command Line Arguments

import sys

sys.argv

Import Keywords

  • from (module) import (something)
  • import (module) as (alias)

Comparison Operators

Col 1Col 2
<>
<=>=
==!=

Logical Operators

  • and
  • or
  • not

Math Operators

Col 1Col 2
+-
***
///
+=-=
*=/=
%

Boolean Keywords

  • True
  • False
  • None

Functions

def func_name(x='Default value'):
return y

Main Block

if __name__ == "__main__":

Shell Commands

os.system('x')
os.popen('x').read()
p = subprocess.Popen(['x'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
s = p.communicate()
s[0].decode('utf-8')

Lists

x = []
x.append(x)
x.insert(x, y)
x.remove(x)
x.sort()
x.pop()

y in x

Dictionaries

x = {y: z, a: b}
x[y] = z

x.keys()
x.values()
x.items()

Slicing

x = 'PYTHON'
x[:z]
x[y:]
x[y:z]

Strings

  • upper()
  • lower()
  • capitalize()
  • swapcase()
  • title()
  • strip()
  • split()
  • join()
  • center()
  • replace()
  • startswith()

File Operations

f = open(filename, operation_flag)

File Object Methods

  • read
  • write
  • readline
  • readlines
  • close

Exception Handling

try:
...
except (error1, error2):
...
except error3:
...
except:
...

Common Exceptions

  • NameError
  • SyntaxError
  • IndexError
  • ValueError
  • FileNotFoundError
  • KeyError

F-Strings

print(f'Hello World')
x = 'John'
print(f'Hello {x}')

y = 3
print(f'Formatted: {y:02d} minutes.') # 03
print(f'Formatted: {y:.2f} dollars.') # 3.00