Categories of Programming Languages
Machine Language: High execution efficiency but very low development productivity.
Assembly Language: Used in operating systems or performance-critical applications like game cheats; offers moderate development speed with relatively high execution performance.
High-Level Languages:
- Interpreted languages: Faster to develop but slower at runtime (e.g., Python).
- Compiled languages: Slower development cycle but faster execution (e.g., C++).
Network Bottleneck Effect: In network-dependent applications, latency dominates over local execution time. Thus, interpreted language are preferred for web apps, while compiled languages remain essential for system-level software like OS kernels.
Running Python Programs
- Interactive Mode (e.g., Jupyter Notebook): Executes code line-by-line, ideal for experimentation.
- Script Mode (e.g., PyCharm): Runs entire scripts via comand line (
python script.py) or IDE execution.
Essential PyCharm Shortcuts
Ctrl + C: Copy entire line if nothing is selected.Ctrl + X: Cut selected content (or full line by default).Ctrl + D: Duplicate current line or selection.Ctrl + Y: Delete current line.Ctrl + /: Toggle comment for selected lines.Ctrl + F: Find text; supports regex and batch replace.Ctrl + Shift + R: Global search and replace across project.Shift + F10: Run last executed script.Ctrl + Shift + F10: Run current file.Ctrl + Alt + L: Reformat code to conform to style guidelines.
Customize shortcuts via File → Settings → Keymap.
Variables and Constants
Programming involves writing instructions that manipulate variables—entities that store changing states from the real world.
A variable consists of:
- Name: Identifier following naming rules.
- Assignment operator (
=): Binds value to name. - Value: Actual data stored.
Naming Rules:
- Must be descriptive.
- Composed of letters, digits, underscores; cannot start with a digit.
- Cannot use Python keywords (e.g.,
if,for).
Two common naming conventions:
- snake_case (preferred in Python)
- camelCase
Constants are conventionally written in UPPER_SNAKE_CASE. Though Python doesn’t enforce immutability, this signals intent.
Python Memory Management and Variable Inspection
When a variable is assigned, Python allocates memory for its value. The interpreter manages memory automatically using:
- Reference Counting: Tracks how many names refer to a value. When count drops to zero, memory is reclaimed.
- Garbage Collector: Handles cyclic references beyond reference counting.
- Small Integer Caching: Integers from -5 to 256 are pre-allocated and reused. PyCharm may extend this range during debugging.
Example:
# x = 100
# y = x # Reference count for 100 becomes 2
# del x # Reference count drops to 1
# del y # Reference count 0 → memory freed (unless in small int pool)
Inspect variables using:
value = 42
print(value) # Output value
print(id(value)) # Memory address
print(type(value))# Data type
Data Types
Python categorizes values into types that dictate operations and behavior.
Integer (int)
Use: Ages, IDs, counts.
Definition: age = 25 or age = int("25")
Operations: +, -, *, /, % (modulo), // (floor division), ** (exponentiation).
Floating-Point (float)
Use: Heights, salaries, measurements.
Definition: price = 19.99 or price = float("19.99")
Type Conversion:
num = float(5) # → 5.0
truncated = int(3.9) # → 3 (no rounding)
rounded = round(3.9) # → 4.0
Supports same arithmetic and comparison operators as integers.
String (str)
Use: Names, labels, textual data.
Definition: Single ('text'), double ("text"), or triple quotes ('''multiline''').
Operations:
first = "Alex"
last = "Chen"
full = first + " " + last # Concatenation
repeated = "Hi! " * 3 # → "Hi! Hi! Hi! "
Strings cannot be added to numbers directly.
Comments
- Single-line:
# This is a comment— also disables code during debugging. - Multi-line: Use triple quotes (
'''or"""), though primarily intended for docstrings.
Toggle comments on multiple lines with Ctrl + /.