Python Notes
Python Notes
Operators are the symbols used to perform a specific operation on different values and variables.
These values and variables are considered as the Operands, on which the operator is applied. The
valid combination of both operands and operators makes an Expression which returns a computed
result.
Python Unary operators Python operators that require one operand to perform a specific
operation are known as unary operators.
Python Binary operators Python operators that require two operands to perform a
specific operation are known as binary operators.
Python Arithmetic operators are used to perform basic mathematical operations such
as addition, subtraction, multiplication, etc.
Example
Operator Name Description
(if a = 10 and b = 5)
Python Comparison/ Relational operators compare the values on either side of them
and decide the relation among them. They are also called Relational operators.
Example
Operator Name Output
(if a = 10 and b = 20)
== Equal to (a = = b) False
= Assignment Operator a = 10
Python logical operators are used to combine two or more conditions and check the
final result. There are following logical operators supported by Python language.
Assume variable a holds 10 and variable b holds 20 then
Example
Operator Description Explanation Output
(if a = 5 and b = 10)
and If both of the operands are
(a = = 5) -> True
Logical true then the condition a = = 5 and b > = 10 True
(b > = 10) -> True
AND becomes true.
or If any of the two operands
(a < 5) -> False
Logical is non-zero then the a < 5 or b ! = 10 False
(b ! = 10) -> False
OR condition becomes true.
not
Used to reverse the logical
Logical not (a ! = 5) (a ! = 5) -> False True
state of its operand
NOT
Precedence: - The precedence of an operator tells the compiler the order in which the
operators should be evaluated.
Operators Meaning
() Parentheses
** Exponent
+x, -x Unary plus, Unary minus
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
==, !=, >, >=, <, <= Comparisons operators
not Logical NOT
and Logical AND
or Logical OR
These include:
if statement: Executes a block of code if a specified condition is true.
if-else statement: Executes one block of code if the condition is true and another block
if the condition is false.
elif (else if) statement: Checks multiple conditions in a sequence and executes a block
of code as soon as one of the conditions evaluates to true.
Nested if statements: You can nest if statements within another if or else block to
create complex decision trees.
Iterative statements refer to the statements that enable the execution of a set of
statements to be repeated till the condition is True. As soon as the condition becomes
False, the control comes out of the loop and the repetition stops.
Indentation in Python
• In Python, indentation is used to declare a block. If two statements are at the same
indentation level, then they are the part of the same block.
• For the ease of programming and to achieve simplicity, python doesn't allow the use
of curly braces or parentheses for the block level code.
• Indentation is the most used part of the python programming language.
• Generally, a tab space or four spaces are given to indent the statements in python.