Server Info & Course Info
$ python3
>>> print("Hello World!")
Hello World!
>>> exit()
Features:
$ nano hello.py # Create file using nano
$ python3 hello.py
Features:
# Correct
if True:
print("This is properly indented")
# Error
if True:
print("This will cause IndentationError")
# Single-line comment
'''
Multi-line comment
Spanning multiple lines
'''
def calculate():
"""Docstring comments for functions"""
# Use semicolon ";". Valid but not recommended
x=1; y=2; print(x+y)
# Preferred
x = 1
y = 2
print(x + y)
| False | None | True | and | as |
| assert | async | await | 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 |
Important: These cannot be used as variable/function names!
>>> print("Hello World!")
Hello World!
### Full version #####
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def main():
print("Hello World!")
if __name__ == "__main__":
main()
#### Ok to run #######
print('Hello World!')
Execution Steps:
nano hello.pyCtrl+O → Press “enter”: Ctrl+Xpython3 hello.pyprin("Missing t") # NameError
def test():
print("Missing indent")
"5" + 3 # str + int
# Think of variables as labeled boxes in warehouse (memory in the computer)
box_label = "item_count" # Variable name
box_content = 17 # Stored value
x = 10 # Create object 10, bind to x
x = "cat" # Create new object, rebind x
# Number('123'), underline ('_'), characters ('abcABC')
# No number at the beginning.
# Valid but discouraged examples
_privateVar = 1
MAX_LIMIT = 100
tempVar2 = 3.14
# Invalid examples (with error simulation)
3attempt = 5 # SyntaxError
special-chars = "text" # SyntaxError
PEP8: Python Enhancement Proposal 8
# Good vs Better
studentname → student_name
tmpf → temporary_file
calcVal → calculate_value
dynamic_var = 100 # int
print(type(dynamic_var)) # <class 'int'>
dynamic_var = "Python" # str
print(type(dynamic_var)) # <class 'str'>
dynamic_var = [1, 2, 3] # list
print(type(dynamic_var)) # <class 'list'>
// C++ static typing (comparison)
// type_error.cpp
#include <iostream>
int main() {
int number = 5; // Type locked
number = "text";
std::cout << "The number is: " << number << std::endl;
}
// g++ type_error -o type_error
# Python dynamic typing
number = 5 # int
number = "text" # Valid
print(number)
binary_num = 0b1010 # 10 (binary representation)
octal_num = 0o177 # 127 (octal)
hex_num = 0x1af # 431 (hex)
number = 33 # 33 (decimal)
# Demonstration of precision issues
print(0.1 + 0.2 == 0.3) # False
print(f"{0.1 + 0.2:.17f}") # 0.30000000000000004, approximately equal to 0.3
# Mitigation strategies
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2')) # Exact 0.3
s1 = 'Single quotes'
s2 = "Double quotes"
s3 = '''Triple quotes preserve
line breaks and "both" quote types'''
# String to number
age = int("25")
pi = float("3.1415")
# Number to string
price = str(9.99)
# Truthy conversions
bool(0) # False
bool(1) # True
bool("") # False
bool("a") # True
bool([]) # False
bool([1]) # True
3 + 5.0 # int promoted to float
True + 10 # bool treated as int(1)
"ID:" + str(100)# Required explicit conversion
a = 256
b = 256
print(id(a) == id(b)) # True (small int caching)
x = 257
y = 257
print(id(x) == id(y)) # False (non-cached)
list1 = [1,2]
list2 = [1,2]
print(id(list1) == id(list2)) # Always False
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 7 - 2 |
5 |
* |
Multiplication | 3 * 4 |
12 |
/ |
True Division | 10 / 3 |
3.333… |
// |
Floor Division | 10 // 3 |
3 |
% |
Modulus | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
a = 7
b = 5
print(a > b) # True
print(a == b+2)# True
print('Apple' < 'Banana') # True (A(65) < B(66))
print('cat' > 'CAT') # True (lowercase > uppercase)
print('10' < '2') # True (character '1'(49) vs '2'(50))
| X | Y | X and Y | X or Y | not X |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
def check():
print("Called!")
return False
print(True or check()) # Short-circuit, no output
print(False and check())# Short-circuit, no output
You need to know:
”()” parentheses are not operators but have the highest priority
| Priority | Operators |
|---|---|
| 1 | ** Exponentiation |
| 2 | +x, -x |
| 3 | *, /, //, % |
| 4 | +, - |
| 5 | Comparisons (<, >, etc) |
| 6 | not |
| 7 | and |
| 8 | or |
Full list:
| Priority | Operators |
|---|---|
| 1 | ** |
| 2 | +x, -x, ~x |
| 3 | *, /, //, % |
| 4 | +, - |
| 5 | <<, >> |
| 6 | & |
| 7 | ^ |
| 8 | | |
| 9 | Comparisons (in, <, >, etc) |
| 10 | not |
| 11 | and |
| 12 | or |
x = y = z = 0 # All variables point to same 0
print(id(x), id(y)) # Same memory address
counter = 5
counter += 3 # Equivalent to counter = counter + 3
counter **= 2 # 8^2 = 64
list1 = list2 = []
list1.append(5)
print(list2) # [5] (both reference same list)
result = 5 + 3 * 2 ** 2 // (4 % 3) - 1
# Step-by-step evaluation:
# 1. 4%3 = 1
# 2. 2**2 = 4
# 3. 3*4 = 12
# 4. 12//1 = 12
# 5. 5+12 = 17
# 6. 17-1 = 16
“Ask Kimi/ChatGPT to explain why 0.1 + 0.2 != 0.3 in Python and how to properly compare floats.”
a = "python"
b = "python"
print(a is b) # Output: True (when string meets interning conditions)
print(id(a))
print(id(b))
a = "a"*10000
b = "a"*10000
print(id(a))
print(id(b))
0 to len(str)-1-1 (last char) to -len(str)text = "programming"
print(text[3:7]) # 'gram'
print(text[::-1]) # 'gnimmargorp' (reverse)
print(text[::2]) # 'pormig' (step=2)
str[start:end] (end not included)str[start:] # till endstr[:end] # from beginningstr[::step] # step controlcsv = "apple,banana,cherry"
items = csv.split(",") # ['apple', 'banana', 'cherry']
new_str = "-".join(items) # "apple-banana-cherry"
s = " hello world \t\n"
print(s.strip()) # "hello world"
print(s.lstrip()) # "hello world \t\n" "\n": line break; "\t": tab
print(s.rstrip()) # " hello world"
s = "Python Programming"
print(s.find('thon')) # 2 (returns -1 if not found)
print(s.index('thon')) # 2 (raises ValueError if not found)
print("Hello".lower()) # 'hello'
print("world".upper()) # 'WORLD'
print("title case".title()) # 'Title Case'
name = "Alice"
print("Hello, %s! You have %d messages." % (name, 5))
print("{} + {} = {}".format(2, 3, 2+3))
print("{1} appears before {0}".format("apple", "banana"))
price = 19.99
print(f"Total: ${price * 1.1:.2f}")
print(f"Binary: {42:b}") # '101010'
| Method | Readability | Performance | Features |
|---|---|---|---|
| %-formatting | Low | Medium | Basic types only |
| str.format | Medium | Medium | Positional/Keyword |
| f-strings | High | Best | Expressions, inline |
name = 'John'
number = 5
print("Hello, "+name+"! You have "+ str(number) +" messages.")
\n Newline\t Tab\\ Backslash\" Double quote
r"" is raw string.
path = r"C:\new_folder\test.txt"
regex = r"\d+\.\d+"
print(path) # Shows literal backslashes
print(regex)
long_text = """First line
Second line
Third line"""