Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

EXPRESSIONS AND STATEMENTS

 An expression is a combination of operators and


operands that is interpreted to produce some other
value.
 In any programming language, an expression is
evaluated as per the precedence of its operators. 
  So that if there is more than one operator in an
expression, their precedence decides which
operation will be performed first. 
TYPES OF EXPRESSIONS
 1. Constant Expressions: These are the
expressions that have constant values only.
 Example
 x = 1.5 + 6.3
 print(x)
ARITHMETIC EXPRESSION
 An arithmetic expression is a combination of
numeric values, operators, and sometimes
parenthesis.
 The result of this type of expression is also a
numeric value.
 The operators used in these expressions are
arithmetic operators like addition, subtraction, etc. 
 Here are some arithmetic operators in Python:

Operators Syntax Functioning

+ x+y Addition

– x–y Subtraction

* x*y Multiplication

/ x/y Division

// x // y Quotient

% x%y Remainder

** x ** y Exponentiation
 x = 40
 y = 8
 add = x + y
sub = x - y
mul = x * y
div = x / y
 print(add)
 print(sub)
 print(mul)
 print(div)
INTEGRAL EXPRESSIONS
 Integral Expressions: These are the kind of
expressions that produce only integer results after
all computations and type conversions.
 a = 1.5
 b = 5
 c = int(a) + b
 print(c)
FLOATING EXPRESSIONS
These are the kind of expressions which produce
floating point numbers as result after all
computations and type conversions.
Example:
 a = 18
 b = 5
 c = a / b
 print(c)
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.
 a = 18
 b = 5
 c = a > b
 print(c)

 Output: True
EXAMPLE 2
 a = 18
 b = 5
 c = 54
 d = 8
 e = (a +b) > (c + d)
 print(e)

 Output: False
LOGICAL EXPRESSIONS
 These are kinds of expressions that result in
either True or False. It basically specifies one or
more conditions.
 Here are some logical operators in Python:

Operator Syntax Functioning

It returns true if both P and Q


and P and Q are true otherwise returns
false

It returns true if at least one of


or P or Q
P and Q is true

It returns true if condition P is


not not P
false
 P = (10 == 8)
 Q = (8 > 6)

 R = P and Q
S = P or Q
T = not P
 print(R)
 print(S)
 print(T)

 Output: False
 True
 True
BITWISE EXPRESSIONS
 These are the kind of expressions in which
computations are performed at bit level.
 a = 12
 x = a >> 2
 y = a << 1
 print(x, y)

 Output:
 3 24
COMBINATIONAL EXPRESSIONS
 It is the combination of different expressions.
 Eg.
 a = 12
 b = 14

 c = a + (b >> 1)
 print(c)

 Output: 19
STATEMENT

A statement in Python is a logical instruction


which can be read and executed by Python
interpreter.
 For example
 a = 12 is an assignment statement
TYPES OF STATEMENTS
 There are basically two types of statements:
 Basic assignment statements
 Augmented assignment statements
BASIC ASSIGNMENT
STATEMENTS
 a = 45
AUGMENTED ASSIGNMENT
STATEMENTS
 Augmented assignment statement is a
combination of an arithmetic or a binary
operation and an assignment operations in a
single statement.
 Eg. +=, -=, *=, /=, %=
 Addition and Assignment (+=):
 a = a + b can be written as a += b

  Subtraction and Assignment (-=): 


 a = a – b can be written as a -= b

 Multiplication and Assignment (*=): 


 a = a * b can be written as a *= b
 4. Division and Assignment (/=):
 a = a / b can be written as a /= b

 Floor Division and Assignment (//=): 


 a = a // b can be written as a //= b

 Modulo and Assignment (%=): 


 a = a % b can be written as a %= b
 Power and Assignment (**=):
 a = a**b can be written as a **= b

 Bitwise AND & Assignment (&=):


 a = a & b can be written as a &= b

 Bitwise OR and Assignment (|=): 


 a = a | b can be written as a |= b
 Bitwise XOR and Assignment (^=): 
 a = a ^ b can be written as a ^= b

 Bitwise Left Shift and Assignment (<<=): 


 a = a << b can be written as a <<= b

 Bitwise Right Shift and Assignment (>>=): 


 a = a >> b can be written as a >>= b

You might also like