Understanding Code: Python Basics for Conditions, Loops, Variables, Functions, Modules, and Classes
This tutorial explains the fundamentals of programming with Python, covering what code is, how to use conditional statements, loops, variables, function definitions, module imports, and the basics of classes and objects, illustrated with clear examples and code snippets.
Code is a language that lets humans communicate precise instructions to a computer, which executes commands without making its own decisions.
Python is used as the teaching language. It has a small set of keywords and simple syntax, making it suitable for illustrating basic programming concepts.
1. Conditional Statements (Judgment)
Conditional logic uses if , else , elif (or elseif ) together with comparison operators ( < , > , = , <= , >= , == ) and logical operators and , or . Example:
if 水果店打折:
买西瓜
else:
不买了A more complex decision can be nested:
if 水果店打折:
买西瓜
else:
与老板讲价
if 讲价成功:
买西瓜
else:
不吃了。Logical operators determine how multiple conditions are combined; parentheses can control precedence.
2. Loops
Python loops use for…in… , while , break , and continue . Example of iterating over three fruit stores to find a discount:
for 水果店 in (鲜丰水果、四季水果和路边摊):
if 水果店 打折:
买西瓜
break # exit loop after buying
else:
什么都不做
带西瓜回家The break statement stops the loop once the desired condition is met.
3. Variables
Variables act as memory cells that store values. They can be assigned with = and compared with == . Example of tracking the best discount:
之前所有水果店的最大折扣 = None
之前给最大折扣的水果店 = None
for 水果店 in (鲜丰水果、四季水果和路边摊):
当前水果店折扣 = 老板给的折扣
if 当前水果店折扣 < 之前所有水果店的最大折扣: # 4折比5折力度大
之前所有水果店的最大折扣 = 当前水果店折扣
之前给最大折扣的水果店 = 水果店
去 之前给最大折扣的水果店 买一个西瓜Variable scope follows indentation: a variable is visible from its first appearance down through deeper‑indented blocks.
4. Functions
Functions group reusable code. They are defined with def function_name(): and called by writing the function name followed by parentheses. Example of a watermelon‑buying function:
def 买西瓜_函数():
for 水果店 in (鲜丰水果、四季水果和路边摊):
if 水果店 打折:
买西瓜
else:
与老板讲价
if 打折成功 and 打折力度 <= 5:
买两个西瓜
elif 打折成功 and 打折力度 > 5:
买一个西瓜
else:
不吃了。
买西瓜_函数()
休息一天。
买西瓜_函数()Function parameters allow the same logic to work for different fruits. The following function takes a fruit name as an argument:
def 买水果函数(水果名):
for 水果店 in (鲜丰水果、四季水果和路边摊):
if 水果店 打折:
买[水果名]
else:
与老板讲价
if 打折成功 and 打折力度 <= 5:
买两个[水果名]
elif 打折成功 and 打折力度 > 5:
买一个[水果名]
else:
不吃了。
买水果函数(西瓜) # 买西瓜
买水果函数(柚子) # 买柚子Python also supports default and keyword arguments, e.g.:
def sum(a, b = 0, c = 1):
return a + b + c
func(1, 0, 1)
func(1)
func(1, c=1)5. Modules
Modules are files (e.g., a.py ) that contain functions, classes, or variables. They can be imported using different styles:
import a
a.buy_xigua()
a.buy_youzi()
from a import *
buy_xigua()
buy_youzi()
from a import buy_xigua, buy_youzi
buy_xigua()
buy_youzi()Importing only what is needed keeps programs lightweight and clear.
6. Classes and Objects
Classes define a blueprint with attributes (state) and methods (behavior). Objects are instances created from a class at runtime. They allow grouping related variables and functions together, enabling more complex modeling than functions alone.
For example, a "Person" class could store a name, balance, and the number of watermelons owned, and provide a method to buy a watermelon.
These concepts together form the foundation for writing organized, maintainable Python programs.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.