What is conditional statements in python

Python If . Else

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in «if statements» and loops.

An «if statement» is written by using the if keyword.

Example

In this example we use two variables, a and b , which are used as part of the if statement to test whether b is greater than a . As a is 33 , and b is 200 , we know that 200 is greater than 33, and so we print to screen that «b is greater than a».

Indentation

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Example

If statement, without indentation (will raise an error):

Elif

The elif keyword is Python’s way of saying «if the previous conditions were not true, then try this condition».

Example

In this example a is equal to b , so the first condition is not true, but the elif condition is true, so we print to screen that «a and b are equal».

Else

The else keyword catches anything which isn’t caught by the preceding conditions.

Example

a = 200
b = 33
if b > a:
print(«b is greater than a»)
elif a == b:
print(«a and b are equal»)
else:
print(«a is greater than b»)

In this example a is greater than b , so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that «a is greater than b».

You can also have an else without the elif :

Example

Short Hand If

If you have only one statement to execute, you can put it on the same line as the if statement.

Example

Short Hand If . Else

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

Example

One line if else statement:

This technique is known as Ternary Operators, or Conditional Expressions.

You can also have multiple else statements on the same line:

Example

One line if else statement, with 3 conditions:

And

The and keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b , AND if c is greater than a :

Or

The or keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b , OR if a is greater than c :

Not

The not keyword is a logical operator, and is used to reverse the result of the conditional statement:

Example

Test if a is NOT greater than b :

Nested If

You can have if statements inside if statements, this is called nested if statements.

Example

if x > 10:
print(«Above ten,»)
if x > 20:
print(«and also above 20!»)
else:
print(«but not above 20.»)

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

Источник

How to Use Conditional Statements in Python – Examples of if, else, and elif

Oluseye Jeremiah

Oluseye Jeremiah

How to Use Conditional Statements in Python – Examples of if, else, and elif

Conditional statements are an essential part of programming in Python. They allow you to make decisions based on the values of variables or the result of comparisons.

In this article, we’ll explore how to use if, else, and elif statements in Python, along with some examples of how to use them in practice.

How to Use the if Statement in Python

The if statement allows you to execute a block of code if a certain condition is true. Here’s the basic syntax:

if condition: # code to execute if condition is true 

The condition can be any expression that evaluates to a Boolean value (True or False). If the condition is True, the code block indented below the if statement will be executed. If the condition is False, the code block will be skipped.

Here’s an example of how to use an if statement to check if a number is positive:

num = 5 if num > 0: print("The number is positive.") 

In this example, we use the > operator to compare the value of num to 0. If num is greater than 0, the code block indented below the if statement will be executed, and the message «The number is positive.» will be printed.

How to Use the else Statement in Python

The else statement allows you to execute a different block of code if the if condition is False. Here’s the basic syntax:

if condition: # code to execute if condition is true else: # code to execute if condition is false 

If the condition is True, the code block indented below the if statement will be executed, and the code block indented below the else statement will be skipped.

If the condition is False, the code block indented below the else statement will be executed, and the code block indented below the if statement will be skipped.

Here’s an example of how to use an if-else statement to check if a number is positive or negative:

num = -5 if num > 0: print("The number is positive.") else: print("The number is negative.") 

In this example, we use an if-else statement to check if num is greater than 0. If it is, the message «The number is positive.» is printed. If it is not (that is, num is negative or zero), the message «The number is negative.» is printed.

How to Use the elif Statement in Python

The elif statement allows you to check multiple conditions in sequence, and execute different code blocks depending on which condition is true. Here’s the basic syntax:

if condition1: # code to execute if condition1 is true elif condition2: # code to execute if condition1 is false and condition2 is true elif condition3: # code to execute if condition1 and condition2 are false, and condition3 is true else: # code to execute if all conditions are false 

The elif statement is short for «else if», and can be used multiple times to check additional conditions.

Here’s an example of how to use an if-elif-else statement to check if a number is positive, negative, or zero:

num = 0 if num > 0: print("The number is positive.") elif num  

Use Cases For Conditional Statements

Example 1: Checking if a number is even or odd.

num = 4 if num % 2 == 0: print("The number is even.") else: print("The number is odd.") 

In this example, we use the modulus operator (%) to check if num is evenly divisible by 2.

If the remainder of num divided by 2 is 0, the condition num % 2 == 0 is True, and the code block indented below the if statement will be executed. It will print the message "The number is even."

If the remainder is not 0, the condition is False, and the code block indented below the else statement will be executed, printing the message "The number is odd."

Example 2: Assigning a letter grade based on a numerical score

score = 85 if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F" print("Your grade is:", grade) 

In this example, we use an if-elif-else statement to assign a letter grade based on a numerical score.

The if statement checks if the score is greater than or equal to 90. If it is, the grade is set to "A". If not, the first elif statement checks if the score is greater than or equal to 80. If it is, the grade is set to "B". If not, the second elif statement checks if the score is greater than or equal to 70, and so on. If none of the conditions are met, the else statement assigns the grade "F".

Example 3: Checking if a year is a leap year

year = 2000 if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: print(year, "is a leap year.") else: print(year, "is not a leap year.") else: print(year, "is a leap year.") else: print(year, "is not a leap year.") 

In this example, we use nested if statements to check if a year is a leap year. A year is a leap year if it is divisible by 4, except for years that are divisible by 100 but not divisible by 400.

The outer if statement checks if year is divisible by 4. If it is, the inner if statement checks if it is also divisible by 100. If it is, the innermost if statement checks if it is divisible by 400. If it is, the code block indented below that statement will be executed, printing the message "is a leap year."

If it is not, the code block indented below the else statement inside the inner if statement will be executed, printing the message "is not a leap year.".

If the year is not divisible by 4, the code block indented below the else statement of the outer if statement will be executed, printing the message "is not a leap year."

Example 4: Checking if a string contains a certain character

string = "hello, world" char = "w" if char in string: print("The string contains the character", char) else: print("The string does not contain the character", char) 
The string contains the character w 

In this example, we use the in operator to check if the character char is present in the string string. If it is, the condition char in string is True, and the code block indented below the if statement will be executed, printing the message "The string contains the character" followed by the character itself.

If char is not present in string, the condition is False, and the code block indented below the else statement will be executed, printing the message "The string does not contain the character" followed by the character itself.

Conclusion

Conditional statements (if, else, and elif) are fundamental programming constructs that allow you to control the flow of your program based on conditions that you specify. They provide a way to make decisions in your program and execute different code based on those decisions.

In this article, we have seen several examples of how to use these statements in Python, including checking if a number is even or odd, assigning a letter grade based on a numerical score, checking if a year is a leap year, and checking if a string contains a certain character.

By mastering these statements, you can create more powerful and versatile programs that can handle a wider range of tasks and scenarios.

It is important to keep in mind that proper indentation is crucial when using conditional statements in Python, as it determines which code block is executed based on the condition.

With practice, you will become proficient in using these statements to create more complex and effective Python programs.

Источник

Читайте также:  Java hashmap default capacity
Оцените статью