21.3. Recap Cheatsheet
21.3.1. Identifiers
>>> variable = 1
>>> CONSTANT = 2
21.3.2. Interpolation
>>> name = 'Alice'
>>> print(f'Hello {name}')
Hello Alice
21.3.3. Operators
Arithmetic:
>>> x = 1 + 2
>>> x = 1 - 2
>>> x = 1 * 2
>>> x = 1 / 2
>>> x = 1 // 2
>>> x = 1 % 2
>>> x = 1 ** 2
Increment:
>>> x += 1
>>> x -= 1
Comparison:
>>> x = 1 == 2
>>> x = 1 != 2
>>> x = 1 < 2
>>> x = 1 <= 2
>>> x = 1 > 2
>>> x = 1 >= 2
21.3.4. Types
>>> x = 1
>>> x = 1.1
>>> x = True
>>> x = False
>>> x = None
>>> x = 'hello'
>>> x = (1, 2, 3)
>>> x = [1, 2, 3]
>>> x = {1, 2, 3}
>>> x = {'a':1, 'b':2, 'c':3}
21.3.5. Unpacking
>>> a, b = 1, 2
>>> a, b = [1, 2]
21.3.6. Getitem
From ordered iterables (list, tuple, str):
>>> data = ['Alice', 'Bob', 'Carol']
>>>
>>> data[0]
'Alice'
>>>
>>> data[-1]
'Carol'
From mappings (dict):
>>> data = {'a':1, 'b':2, 'c':3}
>>>
>>> data['a']
1
>>>
>>> data['b']
2
21.3.7. Slice
>>> data = ['Alice', 'Bob', 'Carol', 'David', 'Eve']
>>>
>>> data[:3]
['Alice', 'Bob', 'Carol']
>>>
>>> data[3:]
['David', 'Eve']
>>>
>>> data[::2]
['Alice', 'Carol', 'Eve']
21.3.8. Conditional
Block statement if, elif, else:
>>> age = 7
>>>
>>> if 0 <= age < 18:
... status = 'junior'
... elif 18 <= age < 30:
... status = 'young'
... elif 30 <= age < 65:
... status = 'adult'
... else:
... status = 'senior'
Ternary operator:
>>> status = 'minor' if age < 18 else 'adult'
21.3.9. While Loop
>>> data = ['Alice', 'Bob', 'Carol']
>>>
>>> i = 0
>>> while i < len(data):
... name = data[i]
... print(name)
... i += 1
Alice
Bob
Carol
21.3.10. For Loop
>>> data = ['Alice', 'Bob', 'Carol']
>>>
>>> for name in data:
... print(name)
Alice
Bob
Carol
21.3.11. Comprehensions
>>> data = ['Alice', 'Bob', 'Carol']
>>>
>>> names = [x.upper() for x in data]
>>> names
['ALICE', 'BOB', 'CAROL']
21.3.12. Files
>>> data = 'Hello World\n'
>>>
>>> with open('/tmp/myfile.txt', mode='wt') as file:
... file.write(data)
12
>>> with open('/tmp/myfile.txt', mode='rt') as file:
... result = file.read()
21.3.13. Functions
>>> def say_hello(firstname, lastname=''):
... return f'Hello {firstname} {lastname}'
21.3.14. Exceptions
>>> try:
... x = 1 / 0
... except ZeroDivisionError:
... print('Cannot divide by zero')
Cannot divide by zero
21.3.15. OOP
>>> class User:
... def __init__(self, firstname, lastname):
... self.firstname = firstname
... self.lastname = lastname
... self.authenticated = False
...
... def login(self, username, password):
... valid_username = username == 'alice'
... valid_password = password == 'secret'
... valid_credentials = valid_username and valid_password
... if not valid_credentials:
... raise PermissionError('Invalid username and/or password')
... print('User logged-in')
... self.authenticated = True
...
... def logout(self):
... self.authenticated = False
... print('User logged-out')
...
... def set_name(self, firstname, lastname):
... if self.authenticated is True:
... self.firstname = firstname
... self.lastname = lastname
... print('User name changed')
... else:
... raise PermissionError('User is not authenticated')
>>> user = User('Alice', 'Apricot')
>>>
>>> vars(user)
{'firstname': 'Alice', 'lastname': 'Apricot', 'authenticated': False}
>>> user.set_name('Bob', 'Blackthorn')
Traceback (most recent call last):
PermissionError: User is not authenticated
>>> user.login('alice', 'secret')
User logged-in
>>>
>>> vars(user)
{'firstname': 'Alice', 'lastname': 'Apricot', 'authenticated': True}
>>> user.set_name('Bob', 'Blackthorn')
User name changed
>>>
>>> vars(user)
{'firstname': 'Bob', 'lastname': 'Blackthorn', 'authenticated': True}
>>> user.logout()
User logged-out
>>>
>>> vars(user)
{'firstname': 'Bob', 'lastname': 'Blackthorn', 'authenticated': False}