Understanding Basic Python Programming Concepts: Code, Conditionals, Loops, Variables, Functions, Modules, and Classes
This article introduces fundamental Python programming concepts by using everyday analogies, covering what code is, conditional statements, loops, variables, functions with parameters, modules, and classes, and provides clear example code snippets to illustrate each topic for beginners.
Introduction
Programming has become a common skill, and many people ask what programming actually means. This tutorial uses vivid analogies to explain core programming concepts, using Python as the example language.
What Is Code?
Code is a "language" that a computer can understand. A computer follows explicit instructions without making its own decisions, so code is the precise way we communicate commands to it.
Programming = Algorithms + Data Structures
Python is a simple language with few keywords, making it ideal for illustrating these concepts.
1. Conditionals (Judgment)
Conditional statements let the computer choose different actions based on conditions. Common keywords include if , else , elif , and , or , < , > , = , <= , >= , == .
<code>if 水果店打折:
买西瓜
else:
不买了</code>More complex nesting can handle multiple scenarios, such as negotiating discounts with a shop owner.
<code>if 水果店打折:
买西瓜
else:
与老板讲价
if 讲价成功:
买西瓜
else:
不吃了。</code>The logical operators and and or affect the evaluation order, which can be controlled with parentheses.
2. Loops
Loop keywords in Python are for…in… , while , break , and continue . Loops allow the computer to repeat actions for multiple items, such as checking several fruit stores for discounts.
<code>for 水果店 in (鲜丰水果、四季水果和路边摊):
if 水果店 打折:
买西瓜
break
else:
什么都不做
带西瓜回家</code>The break statement ends the loop once a suitable store is found.
3. Variables
Variables act as the computer's memory cells, storing values that can change over time. Assignment uses = , while comparison uses == . The special value None represents an empty or undefined variable.
<code>之前所有水果店的最大折扣 = None
之前给最大折扣的水果店 = None
for 水果店 in (鲜丰水果、四季水果和路边摊):
当前水果店折扣 = 老板给的折扣
if 当前水果店折扣 < 之前所有水果店的最大折扣:
之前所有水果店的最大折扣 = 当前水果店折扣
之前给最大折扣的水果店 = 水果店</code>4. Functions
Functions group related code into a reusable block, improving readability and maintainability. They are defined with def function_name(): and called by writing the function name followed by parentheses.
<code>def 买西瓜_函数():
for 水果店 in (鲜丰水果、四季水果和路边摊):
if 水果店 打折:
买西瓜
else:
与老板讲价
if 打折成功 and 打折力度<=5:
买两个西瓜
elif 打折成功 and 打折力度 > 5:
买一个西瓜
else:
不吃了。
买西瓜_函数()
休息一天。</code>Function Parameters
Parameters allow a function to operate on different data without duplicating code. By passing the fruit name as an argument, the same function can buy watermelons, pomelos, or any other fruit.
<code>def 买水果函数(水果名):
for 水果店 in (鲜丰水果、四季水果和路边摊):
if 水果店 打折:
买[水果名]
else:
与老板讲价
if 打折成功 and 打折力度<=5:
买两个[水果名]
elif 打折成功 and 打折力度 > 5:
买一个[水果名]
else:
不吃了。
买水果函数(西瓜)
买水果函数(柚子)</code>5. Modules
Modules are separate .py files that group related functions and classes. They can be imported using different styles:
<code># Method 1
import a
a.buy_xigua()
a.buy_youzi()
# Method 2
from a import *
buy_xigua()
buy_youzi()
# Method 3
from a import buy_xigua, buy_youzi
buy_xigua()
buy_youzi()</code>6. Classes and Objects
Classes encapsulate both state (variables) and behavior (methods) into a single blueprint. Objects are instances of a class created at runtime, allowing multiple independent entities with the same structure.
For example, a "person who buys watermelons" can be modeled as a class with attributes like name, balance, and methods like buy_watermelon() .
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.