SUB: Python Programming (UNIT -2)
Q) Python Operators ?
Python Operators in general are used to perform operations on values and
variables. These are standard symbols used for the purpose of logical and
arithmetic operations.
OPERATORS: Are the special symbols. Eg- + , * , /, etc.
OPERAND: It is the value on which the operator is applied.
Arithmetic Operators
Arithmetic operators are used to performing mathematical operations like addition,
subtraction, multiplication, and division.
Operator Description Syntax
+ Addition: adds two operands x+y
– Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
Modulus: returns the remainder when the first operand
% is divided by the second x%y
** Power: Returns first raised to power second x ** y
Comparison Operators
Comparison of Relational operators compares the values. It either
returns True or False according to the condition.
Operato
r Description Syntax
Greater than: True if the left operand is greater than the
> right x>y
< Less than: True if the left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
Greater than or equal to True if the left operand is
>= greater than or equal to the right x >= y
Less than or equal to True if the left operand is less than
<= or equal to the right x <= y
is x is the same as y x is y
x is not
is not x is not the same as y y
Assignment Operators
Assignment operators are used to assign values to the variables.
Operato
r Description Syntax
Assign value of right side of expression to left side
= operand x=y+z
Add AND: Add right-side operand with left side a+=b
+= operand and then assign to left operand a=a+b
Subtract AND: Subtract right operand from left a-=b a=a-
-= operand and then assign to left operand b
Multiply AND: Multiply right operand with left a*=b
*= operand and then assign to left operand a=a*b
Divide AND: Divide left operand with right operand a/=b
/= and then assign to left operand a=a/b
Modulus AND: Takes modulus using left and right a%=b a=a
%= operands and assign the result to left operand %b
Divide(floor) AND: Divide left operand with right
operand and then assign the value(floor) to left a//=b
//= operand a=a//b
Exponent AND: Calculate exponent(raise power)
value using operands and assign value to left a**=b
**= operand a=a**b
Operato
r Description Syntax
Performs Bitwise AND on operands and assign a&=b
&= value to left operand a=a&b
Performs Bitwise OR on operands and assign value a|=b a=a|
|= to left operand b
Performs Bitwise xOR on operands and assign value a^=b
^= to left operand a=a^b
Performs Bitwise right shift on operands and assign a>>=b
>>= value to left operand a=a>>b
Performs Bitwise left shift on operands and assign a <<= b
<<= value to left operand a= a << b
Logical Operators
Logical operators perform Logical AND, Logical OR, and Logical
NOT operations. It is used to combine conditional statements.
Operato
r Description Syntax
and Logical AND: True if both the operands are true x and y
Logical OR: True if either of the operands is
or true x or y
Operato
r Description Syntax
not Logical NOT: True if the operand is false not x
Bitwise Operators
Bitwise operators act on bits and perform the bit-by-bit operations. These are used to
operate on binary numbers.
Operato
r Description Syntax
& Bitwise AND x&y
| Bitwise OR x|y
~ Bitwise NOT ~x
^ Bitwise XOR x^y
>> Bitwise right shift x>>
<< Bitwise left shift x<<
Identity Operators
is and is not are the identity operators both are used to check if two values
are located on the same part of the memory. Two variables that are equal do
not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical
Membership Operators
in and not in are the membership operators; used to test whether a value or
variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence
……………. End ……………
Q)control statements?
[Link] if statement
The syntax of if statement in Python is:
if condition:
#body of if statement
The if statement evaluates condition.
1. If condition is evaluated to True, the code inside the body of if is
executed.
2. If condition is evaluated to False, the code inside the body of if is
skipped.
Example:
Output:
[Link]...else Statement
An if statement can have an optional else clause.
The syntax of if...else statement is:
if condition:
#block of code if condition is true
else:
#block of code if condition is false
The if...else statement evaluates the given condition:
If the condition evaluates to True,
the code inside if is executed
the code inside else is skipped
If the condition evaluates to False,
the code inside else is executed
the code inside if is skipped
example:
Output:
[Link] statements
In Python, we have one more conditional statement called “elif” statements.
“elif” statement is used to check multiple conditions only if the given condition
is false. It’s similar to an “if-else” statement and the only difference is that in
“else” we will not check the condition but in “elif” we will check the condition.
“elif” statements are similar to “if-else” statements but “elif” statements
evaluate multiple conditions.
Syntax:
if condtion1:
#statemets1
elif condition2:
#statements2
elif condition3:
#statements3
………………….
………………..
else:
#statements
Example:
Output:
correct
4) nested if else:
One if else statement contains another if else statement is called nested if else
statement.
Syntax:
if condition1:
if condition2:
#statements
else:
#statements
else:
#statements
Example:
Output: third
…………………….. end …………..
Q) for loop?
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated
programming languages.
With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.
Syntax:
for iterator_var in sequence:
statements(s)
Example:
Output:
apple
banana
cherry
……………….. end ……………
Q) while loop?
While Loop is used to execute a block of statements repeatedly until a given
condition is satisfied. And when the condition becomes false, the line
immediately after the loop in the program is executed.
Syntax:
while expression:
statement(s)
example:
Output:
……………… end …………
Q) break ?
Python break is used to terminate the execution of the loop.
Python break statement Syntax:
Loop{
Condition:
break
}
Python break statement
break statement in Python is used to bring the control out of the loop when
some external condition is triggered. break statement is put inside the loop
body (generally after if condition). It terminates the current loop, i.e., the
loop in which it appears, and resumes execution at the next statement
immediately after the end of that loop. If the break statement is inside a
nested loop, the break will terminate the innermost loop.
Example:
Output:
……………….. end ………….
Q)continue?
The continue keyword is used to end the current iteration in a for loop (or
a while loop), and continues to the next iteration.
Example:
Output:
…………………. End …………..
Q) types?
Int
Int, or integer, is a whole number, positive or negative, without decimals, of
unlimited length.
Example:
Output:
Strings
Strings in python are surrounded by either single quotation marks, or double
quotation marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:
Example:
Output:
Boolean
In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two
answers, True or False.
When you compare two values, the expression is evaluated and Python
returns the Boolean answer:
Example:
Output:
……………… end ……………
Q) expressions and order of evaluations?
It’s a quite simple process to get the result of an expression if there is only
one operator in an expression. But if there is more than one operator in an
expression, it may give different results on basis of the order of operators
executed. To sort out these confusions, the operator precedence is defined.
Operator Precedence simply defines the priority of operators that which operator is to be executed first.
Here we see the operator precedence in Python, where the operator higher in the list has more
precedence or priority:
Precedence Name Operator
1 Parenthesis ()[]{}
2 Exponentiation **
3 Unary plus or minus, complement -a , +a , ~a
4 Multiply, Divide, Modulo / * // %
5 Addition & Subtraction + –
6 Shift Operators >> <<
7 Bitwise AND &
8 Bitwise XOR ^
9 Bitwise OR |
10 Comparison Operators >= <= > <
11 Equality Operators == !=
12 Assignment Operators = += -= /= *=
Identity and membership
13 operators is, is not, in, not in
Precedence Name Operator
14 Logical Operators and, or, not
………………….. end ……………..