Comprehending the "If" Statement
One of the most important program control structures in Python is the if statement. It is designed to make a decision in your code. An if statement works on the logic that there is a certain code block that will run only if a condition that is explicitly stated is met. This is advantageous as it means you can easily make decisions without hardcoding them into the program, but rather using the data or the context of the situation to make the decision.The regular style of "IF" statement is as follows:
Basic If Statement Syntax
if condition:
//code block to be executed if that condition turns to be True
As an example, you can explain the age restriction to vote where you can say the age must be 18 or 21 and if so they are then allowed to cast their vote.
Voting Age Example
age = 20;
if age > = 18:
print("You are eligible to vote")
The "Else" Statement
This value would alternate depending on whichever way you specified. Basically, while the if statement helps you execute some piece of code when a condition becomes true, then the else statement provides such an option which is an opposite to the code of conditions since it allows you to specify a section of code to be executed when the if statement IS n. On the other hand, the presence of the current case double negatives suggests that they are used when both boolean possibilities are required to be specified, that is, this statement is also true when the condition becomes false.Number Check Example
number = -5
if number > = 0:
print("the number is positive")
else:
print("the number is negative")
Grade Evaluation Example
score = 85
if score > = 90:
print("Grade: A")
elif score > = 80:
print("Grade: B")
elif score > = 70:
print("Grade: C")
else:
print("Grade: F")
The Role of Loops
The first two statements rely on conditions but the latter two do the opposite. In other words, the if and its partner the else allow us to branch depending on conditions while the loops are control structures that allow us to repeat a particular block of code. There are two loops that are generally used when programming in Python i.e. loops and while loops. The functions of the two kinds of loops differ but they are all meant to prevent redundancy in carrying out tasks.The "For" Loop
A for loop in Python's programming can be defined as general iteration over sequences like a string or range. It is a common operation to execute a block of code for each element in case there is a certain condition that requires a sequence of execution. The following is its syntax:For Loop Syntax
for item in sequence:
Code block to execute for each item
For Loop Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
The "While" Loop
As opposed to the for loops however, the while loops don't necessarily have to have a precondition of running a set number of times or iterating over a certain element. A whole condition solely depends on the satisfaction of the `True` condition stipulated by the user.While Loop Syntax
while expression:
code which is carried out provided the expression evaluates to True
While Loop Example
count = 1
while count <= 5:
print(count)
count += 1