Python Fundamentals: Syntax and Data Structures
A Junior Python developer needs a solid grasp of the language's core concepts. Knowing basic syntax is the foundation on which your entire programming career is built.
Variables and Data Types
Python supports a wide range of built-in data types, each with its own unique features:
- int — whole numbers (e.g., 42, -7)
- float — floating-point numbers (3.14, -2.5)
- str — text strings ("Hello", 'Python')
- bool — boolean values (True, False)
- list — ordered, mutable collections [1, 2, 3]
- tuple — immutable collections (1, 2, 3)
- dict — key-value dictionaries {"name": "John"}
- set — collections of unique elements {1, 2, 3}
Conditional Statements and Loops
Controlling the flow of your program is a critical skill:
# Conditional statements
age = 25
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
# Loops
for i in range(5):
print(f"Iteration {i}")
numbers = [1, 2, 3, 4, 5]
while numbers:
print(numbers.pop())
Functions and Parameters
Functions are the building blocks of modular programming. A junior developer should understand different ways to pass parameters:
def calculate_discount(price, discount=0.1, *args, **kwargs):
"""Calculates the discounted price"""
final_price = price * (1 - discount)
if kwargs.get('vip_client'):
final_price *= 0.95
return final_price
# Usage
result = calculate_discount(1000, 0.2, vip_client=True)
Working with Files
Reading and writing files is essential for most real-world tasks:
# Reading a file
with open('data.txt', 'r', encoding='utf-8') as file:
content = file.read()
# Writing to a file
with open('output.txt', 'w', encoding='utf-8') as file:
file.write("Processing result")
Data Structures: Choosing the Right Tool
Knowing when to use each data structure is what separates a good programmer from a beginner.
| Structure | Description | When to Use | Access Complexity |
|---|---|---|---|
| List | Ordered, mutable collection | When order and changes are needed | O(1) by index |
| Tuple | Immutable ordered collection | For constant data | O(1) by index |
| Dict | Key-value pairs | Fast key-based lookups | O(1) average |
| Set | Unique elements | Removing duplicates, membership checks | O(1) average |
Practical Usage Examples
# List for storing operation history
operations_history = []
operations_history.append("File created")
operations_history.append("Edited")
# Tuple for coordinates (immutable)
coordinates = (10.5, 20.3)
# Dictionary for configuration
config = {
"database_url": "localhost:5432",
"debug": True,
"max_connections": 100
}
# Set for unique tags
tags = {"python", "programming", "tutorial"}
Object-Oriented Programming (OOP)
OOP is a programming paradigm that is essential for working on large-scale projects. A Junio