Table of contents
1. Import statements1.1 What are import statements?
1.2 Why use import statements?
1.3 How to import a module?
2. Block structure and indentation
import
statement.
math
module contains these functions.
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:
import module_name
to import everything in a module.from module_name import item1, item2
to import specific items from a module.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:
:
) after if temperature > 30
tells Python that a new block is starting.print
lines that are indented under the if
are part of the same block. They will only run if temperature > 30
is true.else:
line is not indented as much as the prints above, so Python knows it is at the same level as the if
.print
under else
will run if temperature > 30
is false.Tips for proper indentation:
Common structures that require indentation:
if condition:
# Block of code if condition is true
else:
# Block of code if condition is false
for item in collection:
# Block of code to repeat for each item
or
while condition:
# Block of code to repeat until condition is false
def greet(name):
print("Hello,", name)
# More lines of code in the function can be indented here
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:
:
) to start a new block.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.
Author
Dr. Roger Ianjamasimanana