Master Python Basics: From Code to Functions and Modules Explained
This tutorial introduces the fundamentals of programming with Python, explaining what code is, how to use conditionals, loops, variables, functions, parameters, modules, and classes, and provides clear examples that illustrate each concept for beginners.
What is code? Code is a language that a computer can understand; unlike humans, a computer follows explicit instructions without ambiguity. Programming languages let us communicate with computers, and understanding this communication is key to reading code.
Programming = Algorithms + Data Structures
Python is a simple language with few keywords, used for all examples below.
1 Conditionals
Conditionals let the computer choose actions based on conditions using keywords like if, else, elif, and, or, and comparison operators.
if fruit_store_discount:
buy_watermelon
else:
do_not_buy
if fruit_store_discount:
buy_watermelon
else:
negotiate_with_owner
if negotiation_successful:
buy_watermelon
else:
do_not_buy
if fruit_store_discount:
buy_watermelon
else:
negotiate_with_owner
if negotiation_successful and discount <= 5:
buy_two_watermelons
elif negotiation_successful and discount > 5:
buy_one_watermelon
else:
do_not_buy2 Loops
Loops repeat actions for multiple items using for, while, break, and continue.
for fruit_store in (Xianfeng, Siji, Street_stall):
if fruit_store_discount:
buy_watermelon
break
else:
do_nothing
bring_watermelon_home3 Variables
Variables store values, acting as the computer’s memory. None represents an empty value. Assignment uses =, while == checks equality.
previous_max_discount = None
previous_best_store = None
for fruit_store in (Xianfeng, Siji, Street_stall):
current_discount = owner_discount
if current_discount < previous_max_discount:
previous_max_discount = current_discount
previous_best_store = fruit_store
buy_from(previous_best_store)Variable Scope
A variable is only accessible within the block where it is first defined and any nested blocks.
previous_max_discount = None
previous_best_store = None
for fruit_store in stores:
current_discount = get_discount()
if current_discount < previous_max_discount:
previous_max_discount = current_discount
previous_best_store = fruit_store
buy_from(previous_best_store)4 Functions
Functions group code into reusable blocks, improving readability and maintainability.
def buy_watermelon():
for fruit_store in stores:
if fruit_store_discount:
buy_watermelon
else:
negotiate_with_owner
if discount_successful:
buy_two_watermelons
elif discount_successful and discount > 5:
buy_one_watermelon
else:
do_not_buy
buy_watermelon()
rest_one_day()
buy_watermelon()4.1 Function Parameters
Parameters allow functions to operate on different data, such as buying various fruits.
def buy_fruit(fruit_name):
for fruit_store in stores:
if fruit_store_discount:
buy[fruit_name]
else:
negotiate_with_owner
if discount_successful:
buy_two[fruit_name]
elif discount_successful and discount > 5:
buy_one[fruit_name]
else:
do_not_buy
buy_fruit('watermelon')
buy_fruit('pomelo')5 Modules
Modules are files that contain functions, classes, and variables, allowing code to be organized and reused across projects.
# method 1
import a
a.buy_watermelon()
a.buy_banana()
# method 2
from a import *
buy_watermelon()
buy_banana()
# method 3
from a import buy_watermelon, buy_banana
buy_watermelon()
buy_banana()6 Classes and Objects
Classes define the blueprint of objects, encapsulating state (attributes) and behavior (methods). Objects are instances of classes created at runtime.
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.
