Python Basics Summary
Python Interpreter and Interactive Mode
Interpreter executes code line by line. Interactive mode allows quick testing by typing commands directly into
the Python shell.
Data Types
Basic: int, float, str, bool
Collection: list, tuple, dict, set
Statements
Instructions Python executes. Example:
x = 5 (assignment)
print(x) (function call)
if x > 0: (control flow)
Expressions
Code that evaluates to a value, e.g., 3 + 4 * 2 returns 11.
Boolean Values and Operators
Values: True, False
Operators: and, or, not, ==, !=, <, >, <=, >=
Strings
Sequence of characters. Immutable.
s = 'Python'
s[0] = 'P', s.upper() = 'PYTHON'
Arrays of Numbers
Python Basics Summary
Use array or numpy module. Example:
import array
a = array.array('i', [1,2,3])
Lists
Ordered, mutable collection.
lst = [1, 2, 3]
lst.append(4)
Tuples
Ordered, immutable collection.
tup = (1, 2, 3)
Dictionaries
Unordered key-value pairs.
d = {'name': 'Alice', 'age': 25}
Functions
Reusable block of code.
def greet(name):
return 'Hello ' + name
File Reading and Writing
Read:
with open('file.txt', 'r') as f:
content = f.read()
Write:
with open('file.txt', 'w') as f:
Python Basics Summary
f.write('Hello')