Conditions in Python
Python : Conditions
Conditions are used to check if a particular statement is true or false. The most common way of using conditions in Python is through the
if statement
.
If statement in Python
if condition:
statement(s)
x = 5
if x > 0:
print("x is positive")
if-else statement in Python
if condition:
statement(s) if condition is true
else:
statement(s) if condition is false
elif statement in python
if condition1:
statement(s) if condition1 is true
elif condition2:
statement(s) if condition2 is true
...
else:
statement(s) if all conditions are false
Example-
x = 5
if x < 0:
print("x is negative")
elif x == 0:
print("x is zero")
else:
print("x is positive")
Overall, conditions are an important part of Python programming as they allow us to make decisions and execute different blocks of code based on those decisions.
Note: We can't use turnary operator directly in Python similar to other languages like javascript etc.
Previous Article
Next Article
Python Tutorials
- Hello World
- Variables and Types
- Lists
- Tuple
- Basic Operators
- Strings
- Conditions
- Loops
- Functions
- Classes and Objects
- Dictionaries
- Map
- Filter
- Reduce
- Sets
- Decorators
- Generators
- Modules and Packages
- Numpy Arrays
- Pandas Basics
- List Comprehensions
- Lambda functions
- Multiple Function Arguments
- Partial functions
- Regular Expressions
- Exception Handling
- Serialization
- Code Introspection