Introduction to Python

Blog 2: Introduction to Python

Control Flow and Conditional Statements

Control flow and conditional statements are fundamental concepts in programming. They allow you to control the flow of your program's execution based on certain conditions. You can achieve this using if-else statements, while and for loops, and nested conditionals. In this blog post, we will explore these concepts in detail and understand how they can be applied to make your programs more purposeful.


What is a conditional?

A conditional is an expression in programming that evaluates to True or False. In Python, the type of variable that stores this value is known as a Boolean. Conditionals can be found in a few statements, including ‘if’ statements, ‘elif’ statements, and ‘else’ statements. For advanced practice, try to guess the output of the nested conditional example at the bottom of this blog.

The ‘if’ statement

Let’s take a look at this code. It first assigns an integer that the user inputs to the ‘num’ variable. In the following line, an ‘if’ statement appears, comparing if the value of ‘num’ is greater than 0. Python can automatically compare the two. If it is TRUE that num > 0, the indented code on the following line will execute. If num is equal to or less than 0, then the next line will not run and Python will skip over it.

In an ‘if’ statement, an argument, like ‘num > 0’, is always required. But what if you want more conditions to get a better understanding of a variable such as ‘num’?


The ‘elif’ statement

In the case that you want to make another comparison, the ‘elif’ statement is your best option. Just like the ‘if’ statement, an argument is required for this comparison to work. Besides the name itself, the ‘elif’ statement is identical to the ‘if’ statement; ‘elif’ statements can be used again and again while an ‘if’ statement must be used as the first comparison in a block.


The ‘else’ statement

This conditional, unlike the ‘if’ and ‘elif’, requires no argument. It is meant typically at the end of a block to ‘catch’ any other possible comparison outcomes. In this case, where we are seeing whether the number is positive, 0, or negative, it could be replaced by:



Loops

There are two main types of loops in Python: ‘for’ and ‘while’. Each one can be used to set a parameter and execute code in their respective blocks a certain amount of times. They can be beneficial whenever you need to search through a variable to find a value or increment a number many times.

The ‘for’ loop

This type of loop often searches a collection of values with an index, which often increments or alters values in the code block.

In this example, the ‘range’ function is used. For basic purposes, it is a function with two inputs.

Range (starting number, ending number [not inclusive!])

The code above will assign each value generated in the range function to ‘number’ and will print with each iteration. It will print: 1, 2, 3, 4, 5; it does not include the upper bound.

The ‘while’ loop

A ‘while’ loop is a control structure in Python that allows you to execute a block of code repeatedly as long as a specified condition is true. Unlike a ‘for’ loop, which iterates through a sequence, a ‘while’ loop continues to run as long as the given condition remains true.

In this example, the while loop continues to execute as long as the number variable is less than or equal to 5. Inside the loop, the current value of ‘number’ is printed, and then ‘number’ is incremented by 1. The loop will continue until ‘number’ becomes 6, at which point the condition number <= 5 is no longer true, and the loop will terminate. When you run this code, the output is: 1, 2, 3, 4, 5


Nested conditional example

In summary, we've journeyed through the basics of Python programming, uncovering essential concepts like ‘if’, ‘else’, and ‘elif’ statements for decision-making. We've grasped the power of loops – ‘while’ for repetitive tasks based on conditions and ‘for’ for iterating through sequences. With these tools, you can control program flow, make informed choices, and automate repetitive tasks. Continue practicing and seeing what types of control flow you can make!