Python Overview
Control Structures and Functions
Building on our understanding of Python’s basic data types and operations, we’ll now explore how to control program flow and create reusable code blocks. These structures allow us to write more sophisticated programs that can make decisions, repeat operations, and organize code efficiently.
Functions
Functions are reusable blocks of code that can be executed multiple times from different parts of your program. They help in organizing code, making it more readable, and reducing redundancy. Functions can take input arguments and return output values.
A function in Python is defined using the def keyword followed by the name of the function, which is usually descriptive and indicates what the function does. The parameters inside the parentheses indicate what data the function expects to receive. The -> symbol is used to specify the return type of the function.
Here’s an example:
Functions can be called by specifying the name of the function followed by parentheses containing the arguments. The arguments passed to the function should match the number and type of parameters defined in the function. Here’s an example:
Loops
Loops are used to execute a block of code repeatedly. There are two main types of loops in Python: for loops and while loops.
A for loop in Python is used to iterate over a sequence (such as a list or string) and execute a block of code for each item in the sequence. Here’s an example:
A while loop in Python is used to execute a block of code while a certain condition is met. The loop continues as long as the condition is true. Here’s an example:
Conditional Statements
Conditional statements are used to control the flow of your program based on conditions. The main conditional statements in Python are if, else, and elif.
An if statement in Python is used to execute a block of code if a certain condition is met. Here’s an example:
An else statement in Python is used to execute a block of code if the condition in an if statement is not met. Here’s an example:
An elif statement in Python is used to execute a block of code if the condition in an if statement is not met but under an extra condition. Here’s an example:
Exercises
The following exercises will help you practice using functions with conditional logic.
Create a function that converts temperatures between Fahrenheit and Celsius scales. This exercise demonstrates how to define and use functions with conditional logic to perform different types of conversions based on user input.
The conversion formulas are: - Celsius to Fahrenheit: \(F = (C \times 9/5) + 32\) - Fahrenheit to Celsius: \(C = (F - 32) \times 5/9\)
Time estimate: 15-20 minutes
Use an if-else statement to check the scale parameter. Depending on whether it’s ‘C’ or ‘F’, apply the appropriate conversion formula. Remember to return both the converted temperature value and the new scale designation (either ‘F’ or ‘C’).
Create a function that checks whether a given number is prime. This exercise demonstrates the use of loops, conditional statements, and early return to solve a common mathematical problem.
A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.
Time estimate: 15-20 minutes
First, check if the number is less than 2 (not prime). Then, use a loop to check if the number is divisible by any integer from 2 to the square root of the number. If you find a divisor, the number is not prime. If no divisors are found, the number is prime.