Master Python Basics: Variables, Control Flow, Data Structures, and OOP Explained

This comprehensive guide walks you through Python fundamentals—from defining variables and using conditional statements to mastering loops, lists, dictionaries, and object‑oriented programming concepts—providing clear examples and code snippets to help beginners build a solid coding foundation.

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

What Is Python?

Python is a high‑level, readable programming language designed for simplicity and versatility, widely used in data science, web development, and machine learning.

Variables

Variables store values and are created with simple assignment:

one = 1
two = 2
some_number = 10000
true_boolean = True
false_boolean = False
my_name = "Leandro Tk"
book_price = 15.80

Control Flow – Conditional Statements

Use if, elif, and else to execute code based on conditions:

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("2 is greater than 1")
else:
    print("1 is equal to 2")

Loops / Iterators

While loop repeats while a condition is true:

num = 1
while num <= 10:
    print(num)
    num += 1
loop_condition = True
while loop_condition:
    print("Loop Condition keeps: %s" % (loop_condition))
    loop_condition = False

For loop iterates over a range or collection:

for i in range(1, 11):
    print(i)
for book in bookshelf:
    print(book)
for key in dictionary:
    print("%s --> %s" % (key, dictionary[key]))
for key, value in dictionary.items():
    print("%s --> %s" % (key, value))

Lists (Array Data Structure)

Lists store ordered collections and support indexing and appending:

my_integers = [1, 2, 3, 4, 5]
print(my_integers[0])  # 1
my_integers = [5, 7, 1, 3, 4]
print(my_integers[0])  # 5
bookshelf = []
bookshelf.append("The Effective Engineer")
bookshelf.append("The 4 Hour Work Week")
print(bookshelf[0])
print(bookshelf[1])

Dictionaries (Key‑Value Data Structure)

Dictionaries map keys to values and allow any hashable type as a key:

dictionary_example = {"key1": "value1", "key2": "value2", "key3": "value3"}
print(dictionary_example["key1"])  # value1
# Adding a new key‑value pair
dictionary_example["age"] = 24
print(dictionary_example)

Classes and Objects

Classes define blueprints for objects. Example of a simple Vehicle class with an initializer and methods:

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
    def make_noise(self):
        print('VRUUUUUUUM')

tesla_model_s = Vehicle(4, 'electric', 5, 250)
print(tesla_model_s.number_of_wheels)  # 4
Vehicle.number_of_wheels = 2  # using property setter
print(tesla_model_s.number_of_wheels)  # 2
tesla_model_s.make_noise()  # VRUUUUUUUM

Encapsulation

Public attributes are accessed directly, while private attributes (prefixed with an underscore) are accessed via getter/setter methods:

class Person:
    def __init__(self, first_name, email):
        self.first_name = first_name
        self._email = email
    def update_email(self, new_email):
        self._email = new_email
    def email(self):
        return self._email

tk = Person('TK', '[email protected]')
print(tk.first_name)  # TK
print(tk.email())      # [email protected]
tk.update_email('[email protected]')
print(tk.email())      # [email protected]

Inheritance

Subclasses inherit attributes and methods from parent classes:

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):
    def __init__(self, number_of_wheels, seating_capacity, maximum_velocity):
        super().__init__(number_of_wheels, seating_capacity, maximum_velocity)

my_electric_car = ElectricCar(4, 5, 250)
print(my_electric_car.number_of_wheels)   # 4
print(my_electric_car.seating_capacity)  # 5
print(my_electric_car.maximum_velocity) # 250
Source: Machine Heart (机器之心) Original article: Learning Python from Zero to Hero
Python tutorial
Python tutorial
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.

ProgrammingObject-OrientedVariablesLoopsbasicsdata-structures
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.