Programming Structures

1. Control Flow

Python includes basic control flow structures such as conditionals and loops.

1.1 Conditional Statements

Conditional statements control the flow of execution based on conditions.

Exercise ☕📝

Only sword or arrow can defeat the beast.

1.2 Loops

Loops are used to iterate over a sequence of items.

1.2.1 For Loop

1.2.2 While Loop

Exercise ☕📝

Write a program that prints the first 10 numbers in the Fibonacci sequence. Recall that the Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers. Therefore, the first 10 numbers in the Fibonacci sequence are 0 1 1 2 3 5 8 13 21 34.

2 Functions

Functions are reusable blocks of code that perform a specific task. We write functions to avoid repetition and improve code readability.

Exercise ☕📝

Write a function cal_interest(principal, rate, years) that takes in the principal amount, annual interest rate, and number of years, and returns the total amount of interest. Assume annual compounding.

3 Exception Handling

Exception handling ensures robust code execution.