Master Python Basics: Encoding, Keywords, Variables, and Control Flow Explained
This tutorial introduces Python 3 fundamentals, covering source file encoding, reserved keywords, comment styles, variable assignment, data types, string operations, the ternary operator, conditional branches, and loop constructs with clear examples and visual illustrations.
1. Encoding
By default, Python 3 source files use UTF-8 encoding and all strings are Unicode. You can specify a different encoding by adding a coding comment at the top of the file.
2. Keywords
Python’s reserved words can be listed using the keyword module, which prints all keywords for the current version.
3. Comments
Single‑line comments start with #. Multi‑line comments are enclosed by three single quotes (''' … ''') or three double quotes (""" … """).
4. Variables
Variables do not need explicit declaration; they are created upon first assignment. Python 3 supports int, float, bool, and complex. Multiple assignment is possible, e.g., a, b = 1, 2. A variable can be reassigned to objects of different types. Division (/) always returns a float; integer division uses //. Mixed arithmetic automatically promotes integers to floats.
5. Strings
Strings are enclosed in single or double quotes; special characters are escaped with a backslash. Concatenation uses +, repetition uses *. Triple quotes ('''…''' or """…"") create multi‑line strings. Prefixing with r makes a raw string where backslashes are not escaped. Indexing starts at 0 from the left and -1 from the right; there is no separate character type—single‑character strings serve that purpose. Strings are immutable; assigning to an index raises an error. Slicing uses obj[start:stop] with a half‑open interval, and either index may be omitted.
6. Ternary Operator
7. Branches
The if‑else statement works like in other languages. The if‑elif‑else chain corresponds to if‑else if‑else in C/Java.
8. Loops
whileloops repeat while a condition is true:
while condition:
statementsforloops iterate over a sequence and may include an else clause that runs after normal termination (but not after a break).
To iterate over numbers, use the built‑in range() function, often combined with for loops.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
