OPS445 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.
First Quiz
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 1 | Col 2 |
|---|---|
| < | > |
| <= | >= |
| == | != |
Logical Operators
andornot
Math Operators
| Col 1 | Col 2 |
|---|---|
| + | - |
| * | ** |
| / | // |
| += | -= |
| *= | /= |
| % |
Boolean Keywords
TrueFalseNone
Second Quiz
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
Tuples
x = (y, )
x = (y, z)
Sets
x = {x, y}
x.add(x)
Combinations
| Method 1 | Method 2 |
|---|---|
x & y | x.intersection(y) |
x - y | x.difference(y) |
x ^ y | x.symmetric_difference(y) |
x | y | x.union(y) |
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()
Third Quiz
File Operations
f = open(filename, operation_flag)
File Object Methods
readwritereadlinereadlinesclose
Exception Handling
try:
...
except (error1, error2):
...
except error3:
...
except:
...
Common Exceptions
NameErrorSyntaxErrorIndexErrorValueErrorFileNotFoundErrorKeyError
Fourth Quiz
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
Classes
class Name:
def __init__(self, x, y):
self.x = x
self.y = y
def method(self):
...
n1 = Name(x, y)
Magic Methods
class Name:
def __eq__(self, other):
return self == other
Equality Operators
__eq____ne____gt____lt____ge____le__
Math Operators
__add____sub____mul____truediv____mod____floordiv____pow__
Other Dunders
__len____str____repr__
Fifth Quiz
Ansible Ad-Hoc
ansible remote_machine_id [-i inventory] [--private-key id_rsa] [-u remote_user] -a 'date'
Playbooks
- hosts:
user:
become:
vars:
tasks:
- name:
yum: name= state=
- name:
service:
name:
state:
- name:
copy:
dest:
src:
owner:
group:
mode:
- name:
user:
name:
comment:
group:
append: