Essential Python Concepts for Developers

Overview

Python is a versatile, interpreted, high-level language. Its syntax emphasizes readability and efficient code structure, making it distinct from curly-brace languages like C++. This summary highlights key structural differences and fundamental utilities.

Data Structures

  • Lists: Ordered, mutable collections defined with [].
  • Tuples: Immutable sequences defined with (). While individual elements cannot be modified, the collection itself can be managed.
  • Dictionaries: Hash maps that store data in key: value pairs, such as data = {'name': 'Alice', 'age': 30}.

Control Flow and Loops

Unlike C++, Python uses indentation (whitespace) to define scope and blocks, with colons (:) indicating the start of a block.


# Demonstration of flow control
counter = 0
while counter < 5:
    counter += 1
    if counter % 2 != 0:
        continue
    print(f"Even number: {counter}")

# Iterating through a sequence
fruits = ['apple', 'banana', 'cherry']
for item in fruits:
    print(f"Current fruit: {item}")

Functions and Modules

Python supports modularity via the import statement. Functions are defined using the def keyword and can accept variable-length arguments using the *args syntax.


def display_items(primary, *others):
    print(f"Primary: {primary}")
    for extra in others:
        print(f"Additional: {extra}")

# Lambda functions provide a concise way to create small, anonymous functions
multiply = lambda x, y: x * y
print(f"Calculation result: {multiply(5, 10)}")

File System and Date Handling

The os module is used for interacting with the file system, while the datetime or time modules handle temporal operations.


import os
import datetime

# Example: Listing files in a directory
root_path = './data'
files = [f for f in os.listdir(root_path) if os.path.isfile(os.path.join(root_path, f))]

# Date formatting
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Timestamp: {current_time}")

Object-Oriented Programming (OOP)

Python treats everything as an object. Key concepts include:

  • Classes and Instances: Blueprints for creating objects containing specific data and behaviors.
  • Inheritance: Mechanisms allowing a child class to derive attributes and methods from a parent class.
  • Method Overriding: Replacing a base class method with a specialized implementation in a subclass.

Practical Application: Fetching Images from the Web

Using libraries like requests (an industry standard for HTTP) and re (regular expressions), you can extract resources from web pages effectively.


import requests
import re

def scrape_images(target_url):
    response = requests.get(target_url)
    # Using regex to find image sources
    pattern = r'src="(https?://.*?\.jpg)"'
    image_links = re.findall(pattern, response.text)
    
    for idx, link in enumerate(image_links):
        with open(f"image_{idx}.jpg", 'wb') as file:
            file.write(requests.get(link).content)

# Usage
# scrape_images("https://example.com")

Tags: python automation web-scraping OOP regex

Posted on Wed, 04 Nov 2026 16:41:38 +0000 by regexpert