Expressions
Expressions
add = x + y
sub = x - y
pro = x * y
div = x / y
print(add)
print(sub)
print(pro)
3. Integral Expressions: These are the kind of expressions that produce
only integer results after all computations and type conversions.
Example:
a = 13
b = 12.0
c = a + int(b)
print(c)
5. Relational Expressions: In these types of expressions, arithmetic expressions are
written on both sides of relational operator (> , < , >= , <=). Those arithmetic expressions
are evaluated first, and then compared as per relational operator and produce a boolean
output in the end. These expressions are also called Boolean expressions.
Example:
# Relational Expressions
a = 21
b = 13
c = 40
d = 37
p = (a + b) >= (c - d)
print(p)
6. Logical Expressions: These are kinds of expressions that result in either
True or False. It basically specifies one or more conditions. For example,
(10 == 9) is a condition if 10 is equal to 9. As we know it is not correct, so it
will return False. Studying logical expressions, we also come across some
logical operators which can be seen in logical expressions most often.
Here are some logical operators in Python:
Operator Syntax Functioning
and P and Q It returns true if both P and Q are true
otherwise returns false
or P or Q It returns true if at least one of P and Q
is true
not not P It returns true if condition P is false
P = (10 == 9)
Q = (7 > 5)
# Logical Expressions
R = P and Q
S = P or Q
T = not P
print(R)
print(S)
Augmented assignment
Augmented assignment is a combination of an arithmetic or a binary
operation and an assignment operation in a single statement.
We can combine arithmetic operators in assignments to form an augmented
assignment statement.
The combined operations of the augmented assignments are represented
using the following operators : +=, -=, *=, /=, %=
Let us consider a simple example:
a += b
The above statement is a shorthand for the below simple statement.
a=a+b
In augmented assignment statements, the left-hand side is evaluated before
the right-hand side
Let us discuss with a simple example:
a += b
Here the left-hand side i.e. a is evaluated first, then value of b is evaluated
and then addition is performed, and finally the addition result is written back
to a.
An augmented assignment expression like
x += 1
can be rewritten as
x=x+1
Both the above statements give out similar result, but the effect is
slightly different.
In the augmented version, the value of x is evaluated only once.
Furthermore, whenever feasible, the actual operation takes place in
the existing memory location of x, without creating a new memory
space and then assigning it to the target variable
type() function and Typecasting
type function is used to find the data type of a given
variable in Python.
For example:
a = 31
type(a) #class<int>
b = “31”
type(b) #class<str>
Example Program
Typecasting
A number can be converted into a string and vice versa (if possible)
There are many functions to convert one data type into another.
a & b = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise or operator
Returns 1 if either of the bit is 1 else 0
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1010
|
0100
= 1110
= 14 (Decimal)
Most of the bitwise operators are binary, which means
that they expect two operands to work with, typically
referred to as the left operand and the right operand.
Bitwise NOT (~) is the only unary bitwise operator since it
expects just one operand.
Bitwise not operator
Returns one’s complement of the number.
Example:
a = 10 = 1010 (Binary)
~a = ~1010
= -(1010 + 1)
= -(1011)
= -11 (Decimal)
Bitwise xor operator
Returns 1 if one of the bits is 1 and the other is 0 else
returns false.
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a ^ b = 1010
^
0100
= 1110
= 14 (Decimal)
Bitwise right shift
Shifts the bits of the number to the right and fills 0 on
voids left( fills 1 in the case of a negative number) as a
result.
Gets similar results as of dividing the number with some
power of two.
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5
Bitwise left shift
Shifts the bits of the number to the left and fills 0 on voids
right as a result.
Gets similar results as of multiplying the number with
some power of two.
Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
Precedence of Python Operators
There can be multiple operators in one expression as well.
To evaluate these types of expressions there is a rule of
precedence in Python. It guides the order in which these
operations are carried out.
For example, multiplication has higher precedence than
subtraction.
>>> 10 - 4 * 2
2 (result)
But we can change this order using parentheses () as it
has higher precedence than multiplication.
For Example:
Parentheses () has higher precedence
>>> (10 - 4) * 2
12 (Result)
The operator precedence in Python is listed in the
following table. It is in descending order (upper group has
higher precedence than the lower ones).
Associativity of Python Operators
We can see in the above table that more than one
operator exists in the same group. These operators have
the same precedence.
Statement Expressions
A statement executes something An expression evaluates to a
Execution of a statement may or value.
may not produces or displays a Evaluation of an expression
result value, it only does whatever always Produces or returns a
the statement says. result value.
Every statement can be an Every expression can’t be a
expression. statement
Example: >>> x = 3 Example: >>> a + 16
>>> print(x) >>> 20
Output: 3
Commments
Lines in the code that are ignored by the python
interpreter during the execution of the program.
Comments enhance the readability of the code and help
the programmers to understand the code very carefully.
Types of comments
There are three types of comments in Python –
OUTPUT SCREEN-->