DEV Community

Cover image for Python for Beginners and Intermediate Developers — A Complete Guide
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on • Edited on

Python for Beginners and Intermediate Developers — A Complete Guide

Python is one of the most popular, versatile, and beginner-friendly programming languages in the world. Created by Guido van Rossum and first released in 1991, Python emphasizes readability, simplicity, and developer productivity. This guide explores Python from architecture and internals to all syntax, constructs, and core concepts, along with examples.


1. Python Architecture

Python is an interpreted, high-level, object-oriented programming language. Understanding its architecture helps you write efficient Python programs.

1.1 Components of Python Architecture

  1. Python Source Code (.py files): The code written by the developer.
  2. Python Interpreter: Converts the source code into bytecode.
  3. Bytecode (.pyc files): Intermediate, platform-independent representation of your code.
  4. Python Virtual Machine (PVM): Executes the bytecode line by line.
  5. Standard Library & Modules: Prebuilt modules and libraries that extend Python’s functionality.

Diagram: Python Architecture

[Python Source Code] --> [Interpreter] --> [Bytecode (.pyc)] --> [Python Virtual Machine] --> [Output]
Enter fullscreen mode Exit fullscreen mode

2. Python Internals

Python internally uses several mechanisms to manage memory, execution, and object handling.

2.1 Memory Management

  • Python uses automatic memory management with Garbage Collection.
  • Objects are stored in heap memory, and references are tracked.
  • Unused objects are cleaned up by Python’s garbage collector.

2.2 Dynamic Typing

  • Python is dynamically typed, which means variable types are determined at runtime.
x = 10       # int
x = "Hello"  # str
Enter fullscreen mode Exit fullscreen mode

2.3 Python Object Model

  • Everything in Python is an object.
  • Each object has:

    • Identity: id(object)
    • Type: type(object)
    • Value: The data it holds
x = 42
print(id(x), type(x), x)
Enter fullscreen mode Exit fullscreen mode

2.4 Execution Flow

  1. Python source code is compiled into bytecode.
  2. Bytecode is executed by Python Virtual Machine (PVM).
  3. Modules and functions are loaded dynamically.

3. Python Syntax and Basics

3.1 Variables and Data Types

Python supports multiple data types:

  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Text Type: str
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview
num = 10        # int
pi = 3.14       # float
name = "Python" # str
flag = True     # bool
Enter fullscreen mode Exit fullscreen mode

3.2 Operators

Python supports:

Arithmetic Operators

+  -  *  /  %  **  //
Enter fullscreen mode Exit fullscreen mode

Comparison Operators

==  !=  >  <  >=  <=
Enter fullscreen mode Exit fullscreen mode

Logical Operators

and  or  not
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

=  +=  -=  *=  /=  //=  %=  **=
Enter fullscreen mode Exit fullscreen mode

Bitwise Operators

&  |  ^  ~  <<  >>
Enter fullscreen mode Exit fullscreen mode

3.3 Control Flow

Conditional Statements

if x > 10:
    print("x is greater than 10")
elif x == 10:
    print("x is 10")
else:
    print("x is less than 10")
Enter fullscreen mode Exit fullscreen mode

Loops

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

# While loop
while x > 0:
    print(x)
    x -= 1
Enter fullscreen mode Exit fullscreen mode

Loop Control Statements

break
continue
pass
Enter fullscreen mode Exit fullscreen mode

3.4 Functions

Functions are defined using def keyword.

def greet(name):
    return f"Hello, {name}"

print(greet("Python"))
Enter fullscreen mode Exit fullscreen mode

Lambda Functions

square = lambda x: x**2
print(square(5))
Enter fullscreen mode Exit fullscreen mode

Variable Scope

  • Local: Inside function
  • Global: Outside function

3.5 Classes and OOP

Python is fully object-oriented.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hello, my name is {self.name}"

p = Person("Alice", 25)
print(p.greet())
Enter fullscreen mode Exit fullscreen mode

OOP Concepts in Python

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Inheritance Example

class Student(Person):
    def __init__(self, name, age, roll_no):
        super().__init__(name, age)
        self.roll_no = roll_no
Enter fullscreen mode Exit fullscreen mode

3.6 Modules and Packages

Python code can be modularized using modules and packages.

# module: math_module.py
def add(a, b):
    return a + b

# main.py
import math_module
print(math_module.add(5, 3))
Enter fullscreen mode Exit fullscreen mode
  • Standard Library: Python ships with hundreds of modules (os, sys, math, random, etc.)
  • Third-Party Modules: Installed via pip

3.7 Exception Handling

Python uses try-except blocks:

try:
    x = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:
    print("Execution complete")
Enter fullscreen mode Exit fullscreen mode

3.8 File Handling

Python supports reading and writing files:

# Writing to a file
with open("file.txt", "w") as f:
    f.write("Hello Python")

# Reading from a file
with open("file.txt", "r") as f:
    print(f.read())
Enter fullscreen mode Exit fullscreen mode

3.9 Advanced Features

  • Decorators
def decorator(func):
    def wrapper():
        print("Before function")
        func()
        print("After function")
    return wrapper

@decorator
def say_hello():
    print("Hello!")
say_hello()
Enter fullscreen mode Exit fullscreen mode
  • Generators
def gen():
    for i in range(5):
        yield i

for x in gen():
    print(x)
Enter fullscreen mode Exit fullscreen mode
  • Context Managers
with open("file.txt", "w") as f:
    f.write("Hello")
Enter fullscreen mode Exit fullscreen mode
  • Iterators
lst = [1, 2, 3]
it = iter(lst)
print(next(it))
Enter fullscreen mode Exit fullscreen mode
  • Type Hinting
def add(a: int, b: int) -> int:
    return a + b
Enter fullscreen mode Exit fullscreen mode
  • Comprehensions
lst = [x**2 for x in range(5)]
Enter fullscreen mode Exit fullscreen mode

4. Python Versions and Implementation

  • CPython: Default, written in C
  • Jython: Python on JVM
  • IronPython: Python on .NET
  • PyPy: High-performance JIT-compiled Python

5. Python Internals

  • Interpreter: CPython converts Python code to bytecode and executes in PVM.
  • Memory Management: Reference counting + Garbage Collector
  • Dynamic Typing: Types resolved at runtime
  • Objects: Everything is an object, including functions and classes.

6. Python Use Cases

  • Web Development: Django, Flask
  • Data Science: NumPy, Pandas, Matplotlib
  • AI/ML: TensorFlow, PyTorch, scikit-learn
  • Automation & Scripting
  • Game Development: Pygame
  • Networking: socket, asyncio

Conclusion

Python is a powerful, easy-to-learn, and versatile language. Its combination of simplicity, dynamic typing, object-oriented design, and extensive libraries makes it ideal for beginners and professionals alike. By understanding its architecture, internals, and full syntax, you can harness Python for virtually any programming task.

Top comments (0)