-
Notifications
You must be signed in to change notification settings - Fork 4
Comparison operators and conditional execution
Amin Zamani edited this page Jan 24, 2024
·
4 revisions
- The comparison (otherwise known as relational) operators are used to compare values. The table below illustrates how the comparison operators work, assuming that
x = 0
,y = 1
, andz = 0
:
Operator | Description | Example |
---|---|---|
== |
returns True if operands' values are equal, and False otherwise |
x == z # True |
!= |
returns True if operands' values are not equal, and False otherwise |
x != y # True |
> |
True if the left operand's value is greater than the right operand's value, and False otherwise |
x > y # False |
< |
True if the left operand's value is less than the right operand's value, and False otherwise |
x < y # True |
>= |
True if the left operand's value is greater than or equal to the right operand's value, and False otherwise |
x >= y # False |
<= |
True if the left operand's value is less than or equal to the right operand's value, and False otherwise |
x <= y # True |
- When you want to execute some code only if a certain condition is met, you can use a conditional statement:
- a single
if
statement, e.g.:
x = 10
if x == 10: # condition
print("x is equal to 10") # Executed if the condition is True.
- a series of if statements, e.g.:
x = 10
if x > 5: # condition one
print("x is greater than 5") # Executed if condition one is True.
if x < 10: # condition two
print("x is less than 10") # Executed if condition two is True.
if x == 10: # condition three
print("x is equal to 10") # Executed if condition three is True.
Each if
statement is tested separately.
- an
if-else
statement, e.g.:
x = 10
if x < 10: # Condition
print("x is less than 10") # Executed if the condition is True.
else:
print("x is greater than or equal to 10") # Executed if the condition is False.
- a series of
if
statements followed by anelse
, e.g.:
x = 10
if x > 5: # True
print("x > 5")
if x > 8: # True
print("x > 8")
if x > 10: # False
print("x > 10")
else:
print("else will be executed")
Each if
is tested separately. The body of else
is executed if the last if
is False
.
- The
if-elif-else
statement, e.g.:
x = 10
if x == 10: # True
print("x == 10")
if x > 15: # False
print("x > 15")
elif x > 10: # False
print("x > 10")
elif x > 5: # True
print("x > 5")
else:
print("else will not be executed")
If the condition for if
is False
, the program checks the conditions of the subsequent elif
blocks – the first elif
block that is True
is executed. If all the conditions are False
, the else
block will be executed.
- Nested conditional statements, e.g.:
x = 10
if x > 5: # True
if x == 6: # False
print("nested: x == 6")
elif x == 10: # True
print("nested: x == 10")
else:
print("nested: else")
else:
print("else")
- Introduction
- Variables
- Data Types
- Numbers
- Casting
- Strings
- Booleans
- Operators
- Lists
- Tuple
- Sets
- Dictionaries
- Conditionals
- Loops
- Functions
- Lambda
- Classes
- Inheritance
- Iterators
- Multi‐Processing
- Multi‐Threading
- I/O Operations
- How can I check all the installed Python versions on Windows?
- Hello, world!
- Python literals
- Arithmetic operators and the hierarchy of priorities
- Variables
- Comments
- The input() function and string operators
Boolean values, conditional execution, loops, lists and list processing, logical and bitwise operations
- Comparison operators and conditional execution
- Loops
- [Logic and bit operations in Python]
- [Lists]
- [Sorting simple lists]
- [List processing]
- [Multidimensional arrays]
- Introduction
- Sorting Algorithms
- Search Algorithms
- Pattern-matching Algorithm
- Graph Algorithms
- Machine Learning Algorithms
- Encryption Algorithms
- Compression Algorithms
- Start a New Django Project
- Migration
- Start Server
- Requirements
- Other Commands
- Project Config
- Create Data Model
- Admin Panel
- Routing
- Views (Function Based)
- Views (Class Based)
- Django Template
- Model Managers and Querysets
- Form
- User model
- Authentification
- Send Email
- Flash messages
- Seed
- Organize Logic
- Django's Business Logic Services and Managers
- TestCase
- ASGI and WSGI
- Celery Framework
- Redis and Django
- Django Local Network Access
- Introduction
- API development
- API architecture
- lifecycle of APIs
- API Designing
- Implementing APIs
- Defining the API specification
- API Testing Tools
- API documentation
- API version
- REST APIs
- REST API URI naming rules
- Automated vs. Manual Testing
- Unit Tests vs. Integration Tests
- Choosing a Test Runner
- Writing Your First Test
- Executing Your First Test
- Testing for Django
- More Advanced Testing Scenarios
- Automating the Execution of Your Tests
- End-to-end
- Scenario
- Python Syntax
- Python OOP
- Python Developer position
- Python backend developer
- Clean Code
- Data Structures
- Algorithms
- Database
- PostgreSQL
- Redis
- Celery
- RabbitMQ
- Unit testing
- Web API
- REST API
- API documentation
- Django
- Django Advance
- Django ORM
- Django Models
- Django Views
- Django Rest Framework
- Django Rest Framework serializers
- Django Rest Framework views
- Django Rest Framework viewsets