Fundamentals 15 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Basics: From Code to Functions and Modules Explained

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_buy

2 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_home

3 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

classesfunctionsModulesVariablesLoopsprogramming basicsConditionals
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.