Python
Python Basics
- Introduction to Python and Its History
- Python Syntax and Indentation
- Python Variables and Data Types
- Dynamic and Strong Typing
- Comments and Docstrings
- Taking User Input (input())
- Printing Output (print())
- Python Operators (Arithmetic, Logical, Comparison)
- Type Conversion and Casting
- Escape Characters and Raw Strings
Data Structures in Python
- Lists
- Dictionaries
- Dictionary Comprehensions
- Strings and String Manipulation
- Tuples
- Python Sets: Unordered Collections
- List Comprehensions and Generator Expressions
- Set Comprehensions
- String Formatting
- Indexing and Slicing
Control Flow and Loops
- Conditional Statements: if, elif, and else
- Loops and Iteration
- While Loops
- Nested Loops
- Loop Control Statements
- Iterators and Iterables
- List, Dictionary, and Set Iterations
Functions and Scope
- Defining and Calling Functions (`def`)
- Function Arguments (`*args`, `**kwargs`)
- Default Arguments and Keyword Arguments
- Lambda Functions
- Global and Local Scope
- Function Return Values
- Recursion in Python
Object-Oriented Programming (OOP)
- Object-Oriented Programming
- Classes and Objects
- the `__init__()` Constructor
- Instance Variables and Methods
- Class Variables and `@classmethod`
- Encapsulation and Data Hiding
- Inheritance and Subclasses
- Method Overriding and super()
- Polymorphism
- Magic Methods and Operator Overloading
- Static Methods
- Abstract Classes and Interfaces
Python Programs
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions
- Program: Calculate Pi in Python
- String Formatting in Python
Python Operators (Arithmetic, Logical, Comparison)
Python is a versatile and beginner-friendly programming language widely used for web development, data analysis, artificial intelligence, and more. One of the fundamental concepts in Python is the use of operators. Operators are special symbols or keywords that perform specific operations on variables and values. In this guide, we’ll explore three essential types of operators in Python: arithmetic, logical, and comparison operators. By the end of this article, you’ll have a clear understanding of how these operators work and how to use them in your code.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. These operators are essential for performing calculations in Python. Let’s take a look at the most common arithmetic operators:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 15 / 3 | 5.0 |
% | Modulus (remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
// | Floor Division | 17 // 3 | 5 |
Examples of Arithmetic Operators in Action
# Additionprint(10 + 5) # Output: 15
# Subtractionprint(20 - 7) # Output: 13
# Multiplicationprint(6 * 4) # Output: 24
# Divisionprint(25 / 5) # Output: 5.0
# Modulusprint(10 % 3) # Output: 1
# Exponentiationprint(2 ** 4) # Output: 16
# Floor Divisionprint(17 // 3) # Output: 5
Arithmetic operators are straightforward and work just like they do in basic math. However, remember that the /
operator always returns a float, even if the result is a whole number. If you want an integer result, use the //
operator for floor division.
2. Comparison Operators
Comparison operators are used to compare two values or variables. They return a Boolean value (True
or False
) based on whether the comparison is true or false. These operators are often used in decision-making and loops.
Here are the most common comparison operators:
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 10 != 5 | True |
> | Greater than | 7 > 3 | True |
< | Less than | 4 < 2 | False |
>= | Greater than or equal to | 8 >= 8 | True |
<= | Less than or equal to | 6 <= 5 | False |
Examples of Comparison Operators
# Equal toprint(10 == 10) # Output: True
# Not equal toprint(7 != 3) # Output: True
# Greater thanprint(15 > 10) # Output: True
# Less thanprint(4 < 2) # Output: False
# Greater than or equal toprint(8 >= 8) # Output: True
# Less than or equal toprint(6 <= 5) # Output: False
Comparison operators are essential for controlling the flow of your program. For example, you can use them in if
statements to execute specific code blocks based on certain conditions.
3. Logical Operators
Logical operators are used to combine multiple conditions and evaluate them as a single expression. These operators are often used in decision-making and loops to create more complex conditions.
The three main logical operators in Python are:
Operator | Description | Example | Result |
---|---|---|---|
and | True if both conditions are true | (5 > 3) and (10 < 20) | True |
or | True if at least one condition is true | (5 > 3) or (10 > 20) | True |
not | Reverses the result of the condition | not (5 > 3) | False |
Examples of Logical Operators
# AND operatorprint((5 > 3) and (10 < 20)) # Output: True
# OR operatorprint((5 > 3) or (10 > 20)) # Output: True
# NOT operatorprint(not (5 > 3)) # Output: False
Logical operators are powerful tools for creating complex conditions. For example, you can use the and
operator to ensure that multiple conditions are met before executing a block of code.
Combining Operators
In real-world programming, you’ll often need to combine different types of operators to achieve your goals. For example, you might use arithmetic operators to calculate a value, comparison operators to compare it with another value, and logical operators to make a decision based on the result.
Here’s an example that combines all three types of operators:
# Calculate the area of a rectanglelength = 10width = 5area = length * width # Arithmetic operator
# Check if the area is greater than 40 and evenif area > 40 and area % 2 == 0: # Comparison and logical operators print("The area is greater than 40 and even.")else: print("The area does not meet the conditions.")
In this example, we first calculate the area using the *
arithmetic operator. Then, we use the >
comparison operator and the and
logical operator to check if the area meets specific conditions.