Different Python Operators
The variables and values may be operated on using Python operators. Python's primary operators are:Operators of assignments
Variables are given values through assignment operators.
Operator | Example | Same As |
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
%= |
x %= 3 |
x = x % 3 |
//= |
x //= 3 |
x = x // 3 |
**= |
x **= 3 |
x = x ** 3 |
&= |
x &= 3 |
x = x & 3 |
|= |
x |= 3 |
x = x | 3 |
^= |
x ^= 3 |
x = x ^ 3 |
>>= |
x >>= 3 |
x = x >> 3 |
<<= |
x <<= 3 |
x = x << 3 |
Calculus Operators
Common mathematical operations are carried out using arithmetic operators.Operator | Name | Example |
+ |
Addition |
x + y |
- |
Subtraction |
x - y |
* |
Multiplication |
x * y |
/ |
Division |
x / y |
% |
Modulus |
x % y |
** |
Exponentiation |
x ** y |
// |
Floor division |
x // y |
Operators for Comparison
To compare two values, comparison operators are utilised.Operator | Name | Example |
== |
Equal |
x == y |
!= |
Not equal |
x != y |
> |
Greater than |
x > y |
< |
Less than |
x < y |
>= |
Greater than or equal to |
x >= y |
<= |
Less than or equal to |
x <= y |
Intelligent Operators
Conditional statements are combined using logical operators.Operator | Description | Example |
and |
Returns True if both statements are true |
x < 5 and x < 10 |
or |
Returns True if one of the statements is true |
x < 5 or x < 4 |
not |
Reverse the result and if it is true, it returns False. |
not(x < 5 and x < 10) |
Membership Operators
A sequence's presentation in an object is checked using membership operators.Operator | Description | Example |
in |
Returns True if a sequence with the specified value is present in the object |
x in y |
not in |
Returns True if a sequence with the specified value is not present in the object |
x not in y |
Operators in bits
Binary number comparisons are performed using bitwise operations.Operator | Name | Description |
& |
AND |
Sets each bit to 1 if both bits are 1 |
| |
OR |
Sets each bit to 1 if one of two bits is 1 |
^ |
XOR |
Sets each bit to 1 if only one of two bits is 1 |
~ |
NOT |
Inverts all the bits |
<< |
Zero fill left shift |
Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> |
Signed right shift |
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
Data Types
Every value is referred to as an object in Python, and each object has a unique data type. In Python, strings, floating-point numbers, and integers are the three most popular data types.Integer (int)
An item, such as "number 5", is represented as an integer.Example: -2, -1, 0, 1, 2, 3
Number in Floating-Point (float)
Float is the standard symbol for floating-point numbers.Example: -1.25, -1.0, – 0.5, 0.0, 0.5, 1.0, 1.25
String
It is possible to alter a string of characters. Take the word "hello" as an example. Also keep in mind that Python supports immutable strings. This implies that once you define one, you cannot subsequently edit it.Example: ‘hello’, ‘bye’, hi’, ‘goodbye’
Lists, dictionaries, and tuples are additional frequent Python data types.
Variables
Data is temporarily stored in the computer's memory via variables.Example:
price = 100rating = 4.7
course_name = ‘Python for Beginners’
published = True
Price is a whole number without any decimal points in the example above, rating is a floating-point number with a decimal point, course name is a string with a list of characters, and published is a boolean.
Python only recognises True and False as boolean values.
Functions
Python functions are used to divide the code into manageable pieces. These sections are simpler to read and keep up with. Additionally, locating flaws in a tiny section of the software is simpler than doing so for the complete thing. These pieces can also be utilised again.Example:
def greet_user(name):
print(f”Hi {name}”)
greet_user(“John”)
Data passing across functions is done through parameters.
Values provided are considered arguments.
Positional arguments (where the position matters) and keyword arguments (where the location doesn't matter) are the two categories of values.
Example of a Positional Argument:
greet_user(“John”, “Smith”)
Keyword Argument Example:
calculate_total(order=30, shipping=8, tax=0.1)
Values may be returned by functions.
If the return statement is not used, None is always returned.
None stands for the lack of a value.
Example:
def square(number):
return number * number
result = square(2)
print(result)
The above function will print 4 as a result.
Flow Control
Comparison Operators
Depending on the values you provide, comparison operators compare two values and determine whether they are True or False.Operator | Meaning |
== |
Equal to |
!= |
Not equal to |
< |
Less than |
> |
Greater Than |
<= |
Less than or Equal to |
>= |
Greater than or Equal to |
Boolean Evaluation
Never use the == or!= operators to evaluate a boolean operation in Python.Use implicit boolean evaluation using the "is" or "is not" operators.
Boolean Operators
Python supports the and, or, and not Boolean operators.The Truth Table of the "or" operator:
Expression | Value |
True or True |
True |
True or False |
True |
False or True |
True |
False or False |
False |
The “or not” operator’s Truth Table:
Expression | Evaluates to |
not True |
False |
not False |
True |
The “and” operator’s Truth Table:
Expression | Value |
True and True |
True |
True and False |
False |
False and True |
False |
False and False |
False |
If-Else Propositions
The true and false components of a given condition are both executed using the if-else expression. The "if" piece of code is run only if the condition is satisfied. The "else" block code is carried out if the condition is false. If the preceding condition is not true, the elif keyword instructs your programme to attempt a different condition.Loops in Python
For loops and while loops are two useful loop statements in Python.For Loop
The for loop is used to iterate over a sequence such as a list, string, tuple, etc.
During Loop
While the condition is true, you may run a series of statements using the while loop.
Break and carry on
Even if the condition is true, the loops can be changed using the break and continue commands.
It is applicable to both for and while loops.
Range and Loop Functions
You may use the range() method to repeatedly loop through a block of code.
It returns a series of numbers that begins at zero by default, increases by one, and terminates at a given value.