Dr. Roger Ianjamasimanana

Python import statements and block structure

By Dr. Roger Ianjamasimanana

1. Import statements

1.1 What are import statements?

In Python, many useful functions and tools are stored in separate files called modules. Before you can use them in your program, you need to load (or "import") these modules. This is done with the import statement.

1.2 Why use import statements?

Imagine you want to work with mathematical functions like square root or sine, but Python doesn't know these until you tell it where to find them. The math module contains these functions.

1.3 How to import a module?

The basic syntax to import a module in python is:

import module_name

For example, to import the math module, we use the following syntax:

import math

Now, the functions and constants inside the math module are available. For example, to calculate the square root of 9, we do:

Here, math.sqrt() tells Python to use the sqrt function from the math module.

Importing specific parts of a module:
If you only need a few things from a module, you can import them directly. For instance:

from math import sqrt, pi

This command imports only the sqrt function and the constant pi from the math module. Now you can write:

Notice that you don't need to write math.sqrt() anymore because you imported sqrt directly.

Summary of import statements:

  • They allow you to use code that others have written.
  • Use import module_name to import everything in a module.
  • Use from module_name import item1, item2 to import specific items from a module.

2. Block structure and indentation

What is a block of code?
A block of code is a group of statements that work together. In Python, blocks are defined by their indentation (spaces or tabs at the beginning of lines). Blocks are used after certain statements like if, for, while, def, and with.

Why is indentation important?
Unlike some other languages that use brackets {}, Python uses indentation to show which lines of code belong together. This makes the code cleaner and easier to read.

Example with an if statement:

  • The colon (:) after if temperature > 30 tells Python that a new block is starting.
  • The two print lines that are indented under the if are part of the same block. They will only run if temperature > 30 is true.
  • The else: line is not indented as much as the prints above, so Python knows it is at the same level as the if.
  • The indented print under else will run if temperature > 30 is false.

Tips for proper indentation:

  • Always use the same number of spaces for each level of indentation (it is recommended to use 4 spaces).
  • Don’t mix tabs and spaces.

Common structures that require indentation:

  1. If/else statements:
    if condition:
        # Block of code if condition is true
    else:
        # Block of code if condition is false
  2. Loops:
    for item in collection:
        # Block of code to repeat for each item
    or
    while condition:
        # Block of code to repeat until condition is false
  3. Function definitions:
    def greet(name):
        print("Hello,", name)
        # More lines of code in the function can be indented here
  4. The with statement (often used for files):
    with open('file.txt', 'r') as file:
        contents = file.read()
        # Work with contents
    The code indented under with ensures the file is handled correctly.

Example of incorrect indentation:

In the code above, the second print line is not aligned with the first one. This will cause an error because Python expects all code under the if to be equally indented.

Correcting indentation:

Now both lines under the if statement are aligned, and Python knows they belong together.

Understanding nested blocks:
When you have blocks inside other blocks, increase the indentation level each time. For example:

The second if is inside the first one, so its lines are indented further. When the nested block is done, you go back one level of indentation to end it.

Summary of block structure:

  • Use a colon (:) to start a new block.
  • Indent all lines in that block equally.
  • When a block ends, return to a previous indentation level.
  • Proper indentation is not just style—it's required for Python to understand your code.

By understanding and applying these principles of importing and indentation, you'll write clearer and more correct Python code. Always check that your indentation matches the logical structure you intend.

feature-top
Readers’ comment
feature-top
Log in to add a comment
🔐 Access