Python reference sheet for quiz
A Python reference sheet compiled from Labs 1–4. It covers every command, function, operator, and control structure mentioned in the slides.
---
# Python Reference Sheet — Labs 1–4
## 1. Program Structure & Syntax
| Item | Syntax / Rule |
|------|----------------|
| Comments | # comment |
| Indentation | Exactly **4 spaces** per level |
| Main function | def main(): … main() |
| Identifier rules | Start with letter or _, then letters/digits_; case-sensitive; cannot be a keyword |
| Keywords | False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield |
---
## 2. Data Types & Conversion
| Type | Examples |
|------|----------|
| int | 10, 0, -9999 |
| float | 1.1, 0.0, -3333.33, -3.33333e3 |
| str | "hello", 'hello', "", '' |
| bool | True, False |
| complex | complex(2, 3) |
| Function | Purpose | Example |
|----------|---------|---------|
| int(x) | Convert to integer | int("5") → 5; int(5.9) → 5 |
| float(x) | Convert to float | float("5.5") → 5.5 |
| str(x) | Convert to string | str(5) → "5" |
| type(x) | Return data type | type(10) → <class 'int'> |
| eval(input(...)) | Evaluate user input as Python expression | x = eval(input("Number? ")) |
| ord(c) | Character → numeric code | ord('A') → 65 |
| chr(n) | Numeric code → character | chr(65) → 'A' |
| abs(x) | Absolute value | abs(-5) → 5 |
| round(x, n) | Round to n decimal places | round(3.14159, 2) → 3.14 |
| pow(x, y) | x to the power y | pow(2, 3) → 8 |
| divmod(x, y) | Quotient and remainder | divmod(7, 3) → (2, 1) |
| complex(re, im) | Create complex number | complex(2, 3) |
| c.conjugate() | Complex conjugate | complex(2,3).conjugate() |
**Math module** — requires import math
| Function | Purpose | Example |
|----------|---------|---------|
| math.ceil(x) | Round up | math.ceil(5.45) → 6 |
| math.floor(x) | Round down | math.floor(5.45) → 5 |
---
## 3. Input / Output
| Statement | Description |
|-----------|-------------|
| print("Hello") | Print to screen |
| print("I", "love", "Mickey") | Print multiple items separated by space |
| print("*", end="") | Print without newline |
| print() | Print empty line |
| input("Prompt: ") | Read string from user |
| eval(input("Prompt: ")) | Read and evaluate expression |
| int(input("Prompt: ")) | Read integer |
| float(input("Prompt: ")) | Read float |
---
## 4. Operators
### Arithmetic
| Operator | Meaning | Example |
|----------|---------|---------|
| + | Addition | 5 + 3 → 8 |
| - | Subtraction | 5 - 3 → 2 |
| * | Multiplication | 5 * 3 → 15 |
| / | Division | 5 / 2 → 2.5 |
| // | Floor division | 5 // 2 → 2 |
| % | Modulo (remainder) | 5 % 2 → 1 |
| ** | Exponentiation | 2 ** 3 → 8 |
| -x | Negation | -5 |
| +x | Unary plus | +5 |
### Relational
| Operator | Meaning |
|----------|---------|
| < | Less than |
| <= | Less than or equal |
| == | Equal to |
| >= | Greater than or equal |
| > | Greater than |
| != | Not equal |
### Boolean
| Operator | Meaning |
|----------|---------|
| and | Logical AND |
| or | Logical OR |
| not | Logical NOT |
### Membership
| Operator | Meaning |
|----------|---------|
| in | Value in sequence |
| not in | Value not in sequence |
### Precedence (high → low)
1. **
2. - (negation)
3. *, /, //, %
4. +, -
5. <, <=, ==, >=, >, !=
6. not
7. and
8. or
---
## 5. Strings & Escape Sequences
| Item | Example |
|------|---------|
| String literals | "hello", 'hello', "" |
| Concatenation | "I" + "love" + "you" |
| Indexing | moredata[0] |
| ord('A') | 65 |
| chr(65) | 'A' |
| Escape | Meaning |
|--------|---------|
| \' | Single quote |
| \" | Double quote |
| \t | Tab |
| \n | Newline |
| \\ | Backslash |
| \ooo | Octal character code |
---
## 6. Control Structures
### If / Else / Elif
```python
if condition:
statements
elif condition:
statements
else:
statements
```
### While Loop
```python
while condition:
statements
```
### For Loop
```python
for var in sequence:
statements
```
### Range
```python
range(stop) # 0 to stop-1
range(start, stop) # start to stop-1
range(start, stop, step) # start to stop-1, step
```
### Break & Continue
```python
while True:
if condition:
break # exit loop immediately
if condition:
continue # skip rest of iteration
```
### Nested Loops
```python
for i in range(1, 6):
for j in range(1, 6):
print("*", end="")
print()
```
### Post-Test Loop Simulation
```python
number = -1
while number < 0:
number = eval(input("Enter a positive number: "))
```
### Input Validation
```python
f_or_c = input("Please enter selection: ")
while f_or_c != "F" and f_or_c != "C":
f_or_c = input("Please enter selection again: ")
```
---
## 7. Assignment
| Type | Example |
|------|---------|
| Simple | x = 5 |
| Simultaneous | x, y = y, x |
| Multiple expressions | sum, diff = x+y, x-y |
| Multiple input | x, y = eval(input("Enter two numbers: ")) |
---
## 8. Common Patterns from Labs
### Sum 1 to n
```python
sum = 0
current = 1
n = int(input("Enter a value: "))
while current <= n:
sum = sum + current
current = current + 1
print(sum)
```
### Temperature Conversion
```python
celsius = eval(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit.")
```
### Interactive Average
```python
moredata = "yes"
sum = 0.0
count = 0
while moredata[0] == 'y':
x = int(input("Enter a number >> "))
sum = sum + x
count = count + 1
moredata = input("Do you have more numbers (yes or no)? ")
print("\nThe average of the numbers is", sum / count)
```
### List Input
```python
my_list = eval(input("Enter comma-separated values: "))
```
---
## 9. Keywords List (Python)
```
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
```
---
This sheet includes every Python command/function/operator explicitly shown in Labs 1–4. Good luck on your quiz!