Python Essentials: Syntax, Data Types, and Control Structures

Code Comments in Python

Comments are essential for documenting logic and making code readable. In Python, there are two primary ways to annotate your scripts:

  • Single-line comments: Use the # symbol. Anything following it on the same line will be ignored by the interpreter.
  • Multi-line comments: Use triple quotes (""" """ or ''' '''). While technical these are string literals, they are widely used for multi-line documentation.

In most modern IDEs like PyCharm or VS Code, you can toggle comments using the Ctrl + / shortcut.

Fundamental Data Types

Python is dynamically typed, meaning you do not need to declare a variable's type explicitly. Use the type() function to inspect the data type of a value:

def sample_function():
    pass

print(type(500))            # <class 'int'>
print(type("Developer"))    # <class 'str'>
print(type(False))          # <class 'bool'>
print(type(3.14159))        # <class 'float'>
print(type(["A", "B"]))     # <class 'list'>
print(type(sample_function)) # <class 'function'>

Variable Assignment and Execution Flow

Python executes code sequentially from top to bottom. Variables can be reassigned, and the most recent assignment determines the value.

x = 10
x = 20
x = 30
print(x + x)  # Result: 60

When comparing values, Python checks for both equality and type compatibility. You can use casting functions like int(), str(), or float() to convert types for comparisons.

val_a = "5"
val_b = 5

print(val_a == val_b)        # False: String vs Integer
print(int(val_a) == val_b)   # True: Both are now Integers

Mathematical Operations

Python provides standard operators for arithmetic, including floor division and modulus:

m = 10
n = 3

print(m + n)  # Addition: 13
print(m - n)  # Subtraction: 7
print(m * n)  # Multiplication: 30
print(m / n)  # Division: 3.333...
print(m // n) # Floor Division: 3
print(m % n)  # Modulo (Remainder): 1

List Manipulation

Lists are versatile, ordered collections. They are zero-indexed, meaning the first element is at position 0.

items = ["CPU", "GPU", "RAM"]

# Accessing and modifying
print(items[1])        # GPU
items[2] = "SSD"       # Updating an element
print(len(items))      # Length of list: 3

# Adding and removing
items.append("PSU")           # Add to end
items.insert(0, "Case")       # Insert at specific index
removed_item = items.pop()     # Remove last item
items.pop(1)                  # Remove item at index 1

# Slicing
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[2:])            # Elements from index 2 to end: [2, 3, 4, 5]

Conditional Statements

Python uses if, elif, and else for logic. Crucially, Python relies on indentation (whitespace) instead of curly braces to define code blocks.

temperature = 25

if temperature > 30:
    print("It is hot outside.")
elif 15 <= temperature <= 30:
    print("The weather is pleasant.")
else:
    print("It is cold outside.")

Structural Pattern Matching

Introduced in Python 3.10, the match statement provides a more readable alternative to complex if-elif-else chains.

status_code = 404

match status_code:
    case 200:
        print("Success")
    case 400 | 401 | 404:
        print("Client Error")
    case 500:
        print("Server Error")
    case _:
        print("Unknown Status")

You can also use "guards" (if statements within a case) for more specific conditions:

user_age = 22

match user_age:
    case age if age < 13:
        print("Child")
    case age if 13 <= age < 20:
        print("Teenager")
    case _ :
        print("Adult")

Loops and Iteration

The for loop is used to iterate over a sequence (like a list or a range of numbers).

# Iterating through a range
for i in range(5):
    print(f"Iteration: {i}")

# Summing values in a list
data_points = [10, 20, 30, 40]
total = 0
for value in data_points:
    total += value
print(f"Total Sum: {total}")

Tags: python Syntax Data Types Control Flow Lists

Posted on Thu, 03 Sep 2026 16:26:21 +0000 by hismightiness