Fundamentals 5 min read

Eight Core Python Programming Concepts with Example Code

This article presents eight essential Python programming topics—basic syntax, control flow, functions, modules and packages, exception handling, file handling, iterators and generators, and object‑oriented programming—each illustrated with clear, runnable code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Eight Core Python Programming Concepts with Example Code

We will explore eight core Python programming concepts and provide multiple example codes.

Part 1: Basic Syntax

# integers, floats and strings
x = 5
y = 3.14
name = "John"

# lists, tuples, dictionaries and sets
my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
my_dict = {'name': 'John', 'age': 20}
my_set = {1, 2, 3}

Part 2: Control Flow

# conditional statements
if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is zero")

# for loop
for i in range(5):
    print(i)

# while loop
while x > 0:
    print(x)
    x -= 1

Part 3: Functions

# define function
def add(a, b):
    return a + b

# call function
result = add(3, 4)
print(result)

Part 4: Modules and Packages

# import module
import math

# use function from module
sqrt_value = math.sqrt(25)
print(sqrt_value)

# create custom module and package
# mymodule.py
def greet():
    print("Hello, from mymodule!")

# main.py
import mymodule
mymodule.greet()

Part 5: Exception Handling

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Error: Division by zero")

Part 6: File Handling

# open file and read content
file = open('myfile.txt', 'r')
content = file.read()
print(content)
file.close()

# write to file
file = open('myfile.txt', 'w')
file.write("Hello, World!")
file.close()

Part 7: Iterators and Generators

# iterator
my_list = [1, 2, 3]
iter_obj = iter(my_list)
print(next(iter_obj))

# generator
def square_generator(n):
    for i in range(n):
        yield i**2

for num in square_generator(5):
    print(num)

Part 8: Object‑Oriented Programming

# class and object
class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print(f"Hello, {self.name}!")

person = Person("John")
person.greet()

These code examples cover the basic concepts and usage for each section. You can expand and modify them as needed to further explore Python's features and capabilities.

Long‑press or scan the QR code below to receive a free Python public course and hundreds of gigabytes of learning materials, including e‑books, tutorials, project sources, and more.

QR code
QR code

Recommended reading:

20 lines of Python code to compress hundreds of images without loss

Google cuts its entire Python team! PyTorch founder’s angry reaction

Friendly Python: Encapsulation and Reuse

Useful Python Search Tool – Whoosh

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.

PythonprogrammingTutorialfundamentals
Python Programming Learning Circle
Written by

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.

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.