Python Basics: Variables, Control Flow, Data Structures, and Object‑Oriented Programming
This tutorial introduces Python fundamentals, covering variables, data types, control flow statements, loops, lists, dictionaries, and object‑oriented programming concepts such as classes, inheritance, encapsulation, getters/setters, and demonstrates code examples for each topic.
Python is a high‑level programming language designed for code readability, allowing developers to express ideas with minimal syntax; it is widely used in data science, web development, and machine learning.
Variables are created by simple assignment, for example one = 1, two = 2, some_number = 10000. Python supports integers, booleans, strings, floats, and other data types.
Control flow uses if, elif, and else statements to execute code based on conditions, e.g.
if True:
print("Hello Python If")
if 2 > 1:
print("2 is greater than 1")
else:
print("1 is not greater than 2")Loops enable repeated execution. A while loop runs while a condition is true:
num = 1
while num <= 10:
print(num)
num += 1. A for loop iterates over a range or a collection, e.g.
for i in range(1, 11):
print(i).
Lists store ordered collections of items. They are created with square brackets, indexed from zero, and can be extended with append.
my_integers = [1, 2, 3, 4, 5]
bookshelf = []
bookshelf.append("The Effective Engineer")
print(bookshelf[0])Dictionaries hold key‑value pairs. Access values via their keys and iterate over items.
dictionary_tk = {"name": "Leandro", "nickname": "Tk", "nationality": "Brazilian"}
print(dictionary_tk["name"]) # LeandroObject‑oriented programming defines classes as blueprints. The __init__ method initializes attributes, and methods implement behavior. Example of a simple class with getters and setters using @property:
class Vehicle:
def __init__(self, wheels, tank, seats, max_speed):
self.number_of_wheels = wheels
self.type_of_tank = tank
self.seating_capacity = seats
self.maximum_velocity = max_speed
@property
def number_of_wheels(self):
return self._wheels
@number_of_wheels.setter
def number_of_wheels(self, value):
self._wheels = valueEncapsulation hides internal state. Public attributes are accessed directly, while non‑public attributes use a leading underscore and are manipulated through methods.
class Person:
def __init__(self, first_name, email):
self.first_name = first_name
self._email = email
def email(self):
return self._email
def update_email(self, new_email):
self._email = new_emailInheritance allows a subclass to reuse and extend a parent class. An ElectricCar can inherit from Car without redefining shared attributes:
class Car:
def __init__(self, wheels, seats, max_speed):
self.number_of_wheels = wheels
self.seating_capacity = seats
self.maximum_velocity = max_speed
class ElectricCar(Car):
def __init__(self, wheels, seats, max_speed):
super().__init__(wheels, seats, max_speed)The tutorial covers these core Python concepts, providing clear explanations and runnable code snippets to help beginners build a solid foundation in the language.
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.
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.
