COMPUTER SCIENCE / PY THON
Python Programming Quick Reference and Basics Guide
Core Syntax, Data Structures, and Idiomatic Patterns
1. Variables and Core Data Types
Python utilizes dynamic typing and built-in standard data structures:
• Integers & Floats: x = 42 , pi = 3.14159
• Strings: name = "Alice" , supporting string formatting via f-strings f"Hello {name}" .
• Lists: Ordered, mutable sequences: items = [1, 2, 3] .
• Dictionaries: Key-value mappings: user = {"id": 101, "role": "admin"} .
2. Control Flow & Iteration
Structured execution using clean indentations:
• if condition: ... elif: ... else: ...
• for item in list: - Standard loop structure.
• list_comp = [x * 2 for x in range(10) if x % 2 == 0] - Concise list comprehensions.
3. Functions and Modules
Reusable code block definitions with default parameters and type hints:
• def calculate_area(width: float, height: float = 10.0) -> float: return width * height
• Importing standard library modules: import math , from datetime import datetime .
4. Exception Handling
Handling errors cleanly using standard try-except blocks:
• try: ... except ValueError as e: ... finally: ...
Reference Guide • Prepared for Knowledge Sharing & Technical Documentation