Python Fundamentals: Syntax and Core Concepts

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):

  1. Exponentiation, unary plus/minus
  2. Multiplication, division, modulo, floor division
  3. Addition, subtraction
  4. Bit shift
  5. Bitwise AND
  6. Bitwise XOR
  7. Bitwise OR
  8. Comparison operators
  9. Assignment
  10. 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 immediately
  • continue: 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:

  1. UTF → decode → Unicode
  2. 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 encodign
  • b: binary mode, no encoding required

Common modes:

  • r: read
  • w: write (overwrites existing)
  • a: append

Reading methods:

  • read(): entire file
  • readline(): one line
  • readlines(): list of lines

Writing methods:

  • write(): string
  • writelines(): list of strings

Pointer manipulation:

  • seek(offset, whence)
    • whence=0: start of file
    • whence=1: current position
    • whence=2: end of file
  • tell(): current position
  • truncate(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

Tags: python Syntax Variables Operators Control Flow

Posted on Wed, 30 Sep 2026 16:20:53 +0000 by huszi001