graph TD;
A{Condition 1?} -->|Yes| B[Block 1: Action if Condition 1 is true]
A -->|No| C{Condition 2?}
C -->|Yes| D[Block 2: Action if Condition 2 is true]
C -->|No| E[Block 3: Action if both are false]
Python vs. C
Source: The Data Visualisation Catalogue
Code blocks are a group of statements that are executed together.
In Python, indentation is used to define blocks of code.
Python uses whitespaces (spaces or tabs) at the beginning of a line to determine the indentation level of the line.
The amount of indentation is flexible, but it must be consistent throughout that block.
Generally, four spaces are used for each level of indentation.
Example:
graph TD;
A{Condition 1?} -->|Yes| B[Block 1: Action if Condition 1 is true]
A -->|No| C{Condition 2?}
C -->|Yes| D[Block 2: Action if Condition 2 is true]
C -->|No| E[Block 3: Action if both are false]
The if statement is used for decision-making in Python programming. It tests a condition and executes a block of code only if that condition evaluates to True. If the condition is False, the block of code is skipped.
The if statement can be combined with elif and else clauses to control the flow of execution in the program, allowing for the implementation of more complex logical structures.
Objective: Categorize life stages by age.
Adult
graph TB;
A[Start Loop<br/>for item in iterable:] --> B[Execute Block]
B --> A
A -->|No More Items| C[End Loop]
Loops in Python are used to execute a block of code repeatedly. Python provides two types of loops: for and while.
for LoopsA for loop is used to iterate over a sequence (e.g., a list, tuple, string, or range) and execute a block of code for each item in the sequence.
while LoopsA while loop, on the other hand, continues to execute a block of code as long as a given condition evaluates to True.
break: Immediately exits a loop.continue: Skips the remainder of the loop’s body and immediately proceeds with the next iteration.pass: Acts as a placeholder, allowing for the definition of empty control structures.List Comprehensions in Python are a concise and efficient way to create lists. They allow for the construction of a new list by applying an expression to each item in an iterable, optionally filtering items to include only those that meet a condition.
The basic syntax of a list comprehension is:
where:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Explanation:
range(10): Produces a sequence of numbers: 0, 1, 2, …, 9.x**2 : Computes the square of each number x from the sequence.range(10) and applies the square operation, generating a new list.[4, 16, 36, 64, 100]
Explanation:
range(1, 11): Generates numbers from 1 to 10 (inclusive).if x % 2 == 0: Filters the numbers to include only even values.x**2: Computes the square of each even number.Error handling is a critical aspect of writing robust Python programs. Python provides the try and except blocks to catch and handle exceptions, preventing the program from terminating unexpectedly.
A common error in programming is division by zero, which occurs when a number is divided by zero. Python raises a ZeroDivisionError exception in such cases.
The basic syntax for handling exceptions in Python is:
Write a Python program that calculates the factorial of a given number \(n\).
# Set the number to calculate the factorial
n = 5
# Initialize the result
result = 1
# Loop to calculate the factorial
for i in range(1, n + 1):
result *= i
# Print the result
print(f"Factorial of {n} is {result}")Factorial of 5 is 120
Explanation:
n = 5 sets the number for which we want to calculate the factorial.result = 1 initializes the result variable to 1.for i in range(1, n + 1): iterates from 1 to n, multiplying the result by each integer i in the range.print(f"Factorial of {n} is {result}") prints the calculated factorial.Write a Python program that generates the first 20 numbers in the Fibonacci sequence.
0 and 1.
Example output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181

Explanation:
a = 0 and b = 1 set the first two numbers of the Fibonacci sequence.0 and 1) are printed using print(a, end=" ") and print(b, end=" "), ensuring that the output is on the same line with spaces separating the numbers.next_number = a + b computes the next Fibonacci number by summing the previous two.print(next_number, end=" ").a = b and b = next_number update the sequence for the next iteration.Write a Python program that prints a pattern of asterisks (*) in the shape of a right-angled triangle.
Example output: for \(n = 5\):
*
**
***
****
*****
# Set the height of the pattern
n = 5
# Loop to print each row of the pattern
for i in range(1, n + 1):
print('*' * i)*
**
***
****
*****
Explanation:
n = 5 sets the height of the pattern to 5.i in range(1, n + 1): iterates from 1 to n, representing each row of the pattern.print('*' * i) prints i asterisks on each row, where i is the current row number.