Language Categories
Compiled Languages
Source code is translated into machine language, producing executable files (e.g., *.obj) that run independently.
Advantages: Execution speed, efficiency, no dependancy on source code post-compilation.
Disadvantages: Modification requires recompilation, poor cross-platform support, different binaries needed per OS.
Examples: C, C++.
Interpreted Languages
Execution occurs through an interpreter which translates source code into CPU instructions.
Advantages: Cross-platform compatibility, dynamic adjustments during runtime.
Disadvantages: Lower eexcution performance, cannot generate standalone executables, source visibility.
Examples: Python, Java, PHP.
Variables
Rules:
- Composed of letters, digits, or underscores
- First character cannot be a digit
- Reserved keywords cannot be used
Naming conventions: camelCase, snake_case
Interactive Input/Output
print, input
In Python 2: print 'text'
In Python 3: print('text')
Comments
Use # for single-line comments
Data Types
Numbers: int, float
Strings: single quotes, double quotes, triple quotes for multi-line text
Boolean: True, False
String operations:
- Concatenation:
'hello' + 'world' - Repetition:
'hi' * 3
Operators
Arithmetic: +, -, *, /, %, **, //
Comparison: ==, !=, >, <, >=, <=
Assignment: =, +=, -=, *=, /=, %=, **=, //=
Bitwise: &, |, ~, ^, <<, >>
Logical: and, or, not
Membership: in, not in
Identity: is, is not
Operator precedence (highest to lowest):
- Exponentiation, unary plus/minus
- Multiplication, division, modulo, floor division
- Addition, subtraction
- Bit shift
- Bitwise AND
- Bitwise XOR
- Bitwise OR
- Comparison operators
- Assignment
- Identity, membership, logical
Parentheses () override default precedence
Control Flow
Single branch:
if condition:
pass
Dual branch:
if condition:
pass
else:
pass
Multiple branches:
if condition1:
pass
elif condition2:
pass
else:
pass
Loops:
while condition:
pass
Loop control:
break: exits loop immediatelycontinue: skips current iteration
Alternative clause:
while condition:
pass
else:
pass
Character Encoding
Python 3 default file encoding: UTF-8 Default string encoding: Unicode
Python 2 default file encoding: ASCII Default string encoding: ASCII
Set encoding via comment at top of file:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Encoding process:
- UTF → decode → Unicode
- Unicode → encode → GBK/UTF-8
Memory stores Unicode; disk/network use GBK/UTF-8
In Python 2: str = bytes (for binary data) In Python 3: str = Unicode
Avoid manual encoding/decoding; always backup before conversion
File Operations
A file is an abstraction provided by the OS for disk access.
Benefits: Simplifies complex disk I/O through OS abstraction.
Usage:
f = open('file.txt')
f.read()
f.close()
Context manager:
with open('file.txt') as f:
pass
Reading issues:
# Error when reading UTF-8 file with default encoding
with open('file.txt') as f:
print(f.read())
# Solution
with open('file.txt', encoding='utf-8') as f:
print(f.read())
File modes:
t: text mode (default), requires encodignb: binary mode, no encoding required
Common modes:
r: readw: write (overwrites existing)a: append
Reading methods:
read(): entire filereadline(): one linereadlines(): list of lines
Writing methods:
write(): stringwritelines(): list of strings
Pointer manipulation:
seek(offset, whence)whence=0: start of filewhence=1: current positionwhence=2: end of file
tell(): current positiontruncate(size): truncate file at size
File modification strategies:
Strategy 1: Load all data into memory, modify, then overwrite
Strategy 2: Read from source, write to temporary file, replace source
Pros/cons:
- Strategy 1: Low memory usage, high memory consumption
- Strategy 2: High memory efficiency, temporary file management