Fundamentals 18 min read

Master Python Basics: Variables, Loops, Data Structures, and OOP Explained

This tutorial introduces Python by defining the language, explaining why it’s popular, and walking through core fundamentals such as variables, conditional statements, loops, lists, dictionaries, classes, encapsulation, getters/setters, and inheritance with clear code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Basics: Variables, Loops, Data Structures, and OOP Explained

What is Python?

According to Guido van Rossum, Python is a high‑level programming language designed for readability and concise syntax, allowing programmers to express ideas with few lines of code.

Why learn Python?

It enables elegant coding, is easy to write, and is applicable to data science, web development, and machine learning; companies like Quora, Pinterest and Spotify use it for backend development.

Python Basics

1. Variables

Variables store values. Example:

one = 1
two = 2
some_number = 10000

Python also supports booleans, strings, floats, and other data types.

2. Control Flow – Conditional Statements

Use if, elif, else to execute code based on boolean expressions.

if True:
    print("Hello Python If")
if 2 > 1:
    print("2 is greater than 1")
if 1 > 2:
    print("1 is greater than 2")
else:
    print("1 is not greater than 2")
if 1 > 2:
    print("1 is greater than 2")
elif 2 > 1:
    print("1 is not greater than 2")
else:
    print("1 is equal to 2")

3. Loops and Iteration

While loops repeat while a condition is true:

num = 1
while num <= 10:
    print(num)
    num += 1

For loops iterate over ranges or collections:

for i in range(1, 11):
    print(i)

Iterating over a list:

bookshelf = ["The Effective Engineer", "The 4 Hour Work Week", "Zero to One", "Lean Startup", "Hooked"]
for book in bookshelf:
    print(book)

Iterating over a dictionary:

dictionary = {"some_key": "some_value"}
for key in dictionary:
    print(f"{key} --> {dictionary[key]}")
for key, value in dictionary.items():
    print(f"{key} --> {value}")

4. Lists (Arrays)

Lists store ordered collections:

my_integers = [1, 2, 3, 4, 5]
print(my_integers[0])  # 1

Lists can also hold strings, such as a list of names.

5. Dictionaries (Key‑Value)

Dictionaries map keys to values:

dictionary_example = {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
}
print(dictionary_example["key1"])

Adding a new key/value pair is straightforward.

6. Classes and Objects

Classes are blueprints for objects. Example vehicle class:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity

Instantiate and access attributes:

tesla_model_s = Vehicle(4, 'electric', 5, 250)
print(tesla_model_s.number_of_wheels)  # 4

Getters and setters can be defined with @property and @<em>attribute</em>.setter.

7. Encapsulation

Encapsulation hides internal state; public methods expose behavior while non‑public members (prefixed with an underscore) are considered internal.

8. Inheritance

Subclassing allows a class to inherit attributes and methods from a parent:

class Car:
    def __init__(self, number_of_wheels, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity

class ElectricCar(Car):
    pass

Instances of ElectricCar have the same properties as Car.

Overall, the article walks through Python fundamentals, from basic syntax to object‑oriented concepts, providing runnable code examples for each topic.

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.

programmingData StructuresOOPVariablesLoopsbasics
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.