In the previous chapter, we learned about Python syntax,variables, and data types. These basics help you install Python, run programs, and write code correctly to get the results you want. If you haven’t finished the previous tutorial, make sure to go back and complete it before moving on.
Why use Operators?
An operator is a symbol that allows us to perform operations on values. The values on which the operator works are called operands.
Example: 5 + 3
Here 5 and 3 are operand and "+" is operator. They are generally divided into three main categories
Arithmetic Operators (Math Operators)
There are multiple operators that come under arithmetic operators.
- (+) - Addition
- (-) - Subtraction
- (*) - Multiplication
- (/) - Division - gives the Quotient with decimal
- (//) - Floor Division - gives the Quotient without decimal part
- (%) - Modulus - gives the Remainder
- (**) - Power - square, cube etc.
Comparision Operators
These are used to compare values and give output in True/False.
- (==) Equal to
- (!=) Not equal to
- (>) - Greater than
- (<) - Less than
- (>=) - Greater than or equal to
- (<=) - Less than or equal.
Logical Operators
Used to combine conditional statements and return True or False based on logical conditions.
- And - Returns True if both conditions are True.
- OR - Returns True if at least one condition is True.
- Not - Reverses the result (True becomes False, False becomes True).
Decision-making is an important part of programming. It allows a program to execute certain statements only when a specific condition is true. Conditional statements help programmers control the flow of execution in a program.
If Statement
This statement only run when the condition is True. While False condition skip the block.
If-else Statement
This statement is used to check two conditions. It ensures that only one block of code executes based on whether the condition is True or False.
This is used when there are more than two conditions to check. The interpreter checks each condition one by one, and when a particular condition becomes true, the corresponding code block is executed. The remaining conditions are skipped.
Nested Condition
When we use an
if statement inside another if statement, it is called a nested condition. It is used when multiple conditions must be checked at the same time.Check number is Negative or Positive
Task 2:
Ask a Number and then answer is ODD or Even.
Use this code to ask a number: number = int(input("Type a Number: "))
Conclusion
Here I've shared another Python tutorial. If you haven't read the previous one, visit the home page or search for python tutorial.
You can also visit: Python Variables, and Data Types
