UNIT-II OPERATORS & BRANCHING
• Variables
Python Variables:
Variable is a name that is used to refer to memory location. Python variable is also
known as an identifier and used to hold value.
Variable names can be a group of both the letters and digits, but they have to begin
with a letter or an underscore.
It is recommended to use lowercase letters for the variable name. Rahul and rahul
both are two different variables.
Data Types:
Every value has a data type, and variables can hold values .
Every value has a data type, and variables can hold values. The interpreter
gives the value implicitly to its type.
A=5
We did not specify the type of the variable a, which has the value five from an
integer. The Python interpreter will automatically interpret the variable as an
integer.
We did not specify the type of the variable a, which has the value five from an
integer. The Python interpreter will automatically interpret the variable as an
integer.
EX: a=10
print(type(a))
list[]:
ordered, changeable , allows duplicates variables.
tuple():
ordered, unchangeable , allows duplicates variables.
set{}:
unordered, unindexed, no duplicates.
dict{}:
Unordered, changeable, indexed, no duplicates.
Arithmetic Operators
• The arithmetic operations are performed by calculator where we can
perform addition, subtraction, multiplication and division. This example
shows the basic arithmetic operations.
• (+)Addition
• (-)Subtraction
• (*)Multiplication
• (/)Division
• (%) reminder
• (**) Exponent
• (//) Floor division
EX:
Assignment Operators
• The assignment operators are used to assign the value of the
right expression to the left operand.
Logical Operators
• The logical operators are used to perform logical operations on
Boolean values.
Gives True or False as the result.
• Following are the logical operators
• and
• or
• not
Logical AND Operator
Gives True if both the booleans are true else, it gives False
Print((2 < 3) and (1 <5))
o/p:- True
Logical OR Operator
Gives True if any one of the booleans is true else, it gives False
print((2 < 3) or (2 < 1))
o/p:- True
Logical NOT Operator
print(not(False))
o/p:- True
Logical Operators
• The logical operators are used primarily in the expression
evaluation to make a decision. Python supports the following
logical operators.
Bitwise Operators
The bitwise operators perform bit by bit operation on the values
of the two operands. Consider the following example.
Ex:
if a = 7
b=6
then, binary (a) = 0111
binary (b) = 0110
hence, a & b = 0110 => If both are 1 , its gives 1
a | b = 0111 => If any bit 1, its gives 1
a ^ b = 0001 => both bits same its gives 0, bits are diff gives 1
~ a = 1000 =>rev the bits
Membership Operators
• Python membership operators are used to check the
membership of value inside a Python data structure.
• If the value is present in the data structure, then the resulting
value is true otherwise it returns false.
Operator:- in , not in
identity operators
• The identity operators are used to decide whether an
element certain class or type.
Operators: is , is not