How to Use if/else Expressions in Python
Now that we're acquainted with conditional operators and know how to use them, we can branch our code based on certain conditions. In Python, this is achieved with the following structure:
if condition:
execute this if the condition is true
else:
execute this if the condition is false
In the example above, the condition can be either a conditional statement or a boolean value. For example, it might look like a != 4 or b > 100. The commands beneath if and else are instructions to be executed depending on whether the condition is met. These could be print() functions, variable assignments, arithmetic operations, string manipulations, and more.
Let's walk through an example: Suppose you have a string and you want to determine if it's long. We'll define a string as 'long' if it contains more than 20 characters.
1234567891011121314151617# Assign some variable test = "small string" # Conditional statement if len(test) > 20: print("This string is long!") else: print("Nothing special") # Check on different string test = "This string is very-very and very large" # Conditional statement if len(test) > 20: print("This string is long!") else: print("Nothing special")
Note
Remember, the code blocks under the
ifandelsestatements should have consistent indentation (e.g., 2 spaces, 4 spaces, etc.).
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Kysy minulta kysymyksiä tästä aiheesta
Tiivistä tämä luku
Näytä käytännön esimerkkejä
Awesome!
Completion rate improved to 1.64
How to Use if/else Expressions in Python
Pyyhkäise näyttääksesi valikon
Now that we're acquainted with conditional operators and know how to use them, we can branch our code based on certain conditions. In Python, this is achieved with the following structure:
if condition:
execute this if the condition is true
else:
execute this if the condition is false
In the example above, the condition can be either a conditional statement or a boolean value. For example, it might look like a != 4 or b > 100. The commands beneath if and else are instructions to be executed depending on whether the condition is met. These could be print() functions, variable assignments, arithmetic operations, string manipulations, and more.
Let's walk through an example: Suppose you have a string and you want to determine if it's long. We'll define a string as 'long' if it contains more than 20 characters.
1234567891011121314151617# Assign some variable test = "small string" # Conditional statement if len(test) > 20: print("This string is long!") else: print("Nothing special") # Check on different string test = "This string is very-very and very large" # Conditional statement if len(test) > 20: print("This string is long!") else: print("Nothing special")
Note
Remember, the code blocks under the
ifandelsestatements should have consistent indentation (e.g., 2 spaces, 4 spaces, etc.).
Kiitos palautteestasi!