Python Basics
After building several Java AI projects, I noticed that the demand for Python AI application development is broader in the market. Although I did use Python before, most of it was done through vibe coding, so my fundamentals were not solid. I am using this chance to systematically fill in Python basics.
Basic Variables and Syntax Style
I first defined a few variables:
money = 50.1
name = "小明"
age = 18
Compared with Java, the most direct differences are:
- No
; - No main-class entry structure
- No explicit variable type declaration when defining variables
for Loop and String Formatting
for i in range(4):
print(f"{i + 1} hello world")
I used f-string here. Also, print can use comma-separated arguments:
print("money:", money)
My understanding is that this style is more direct and avoids frequent string concatenation as in Java.
Boolean Values and if/else
sig1 = True
sig2 = False
if sig2:
print("sig2 is true")
else:
print("sig2 is false")
I also noticed Python is highly indentation-sensitive. The if/else block is fully defined by indentation levels.
Functions and Type Inspection
def print_type(x):
print(type(x))
print_type(money)
print_type(name)
print_type(age)
At this stage, I mainly used this to get familiar with:
- Function definitions without mandatory return type declarations
- Using
type()to quickly inspect the real runtime type of variables
Type Conversion
a = 123
print("a", type(str(a)))
This verifies numeric-to-string conversion using str(a).
Identifier and Naming Rules
The key rules I memorized today are:
- Identifiers can consist of Chinese/English characters, digits, and underscores
- They cannot start with a digit
- They cannot use Python keywords
- Python is case-sensitive
Variable naming convention:
- Use lowercase letters
- Use underscores for multi-word names (
snake_case)
Invalid naming examples:
1name- Names with special symbols such as
name!,name@,name#, etc.
Stage Summary
- First, remove Java-style boilerplate thinking
- Then, get used to Python indentation and dynamic typing
- Then, master the most common basics: loops, conditions, functions, and type conversion
Next, I will continue with lists/dicts, object-oriented programming, file handling, and common AI development libraries.