Basics of Programming Language
Basics of Programming Language
>>> num1 = 13
>>> num2 = 4
>>> num1 // num2
3
>>> num2 // num1
0
Exponent (**)
Performs exponential (power) calculation on
operands. That is, raise the operand on the left to the
power of the operand on the right
>>> num1 = 3
>>> num2 = 4
>>> num1 ** num2
81
Relational Operators
Logical Operators
Logical AND >>> True and True
If both the operands are True
True, then condition >>> num1 = 10
becomes True
>>> num2 = -20
>>> bool(num1 and num2)
True
>>> True and False
False
>>> num3 = 0
>>> bool(num1 and num3)
False
>>> False and False
False
Logical OR
If any of the two >>> True or True
operands are True, then True
condition becomes True >>> True or False
True
>>> bool(num1 or num3)
True
>>> False or False
False
Logical NOT
Used to reverse the >>> num1 = 10
logical state of its >>> bool(num1)
operand True
>>> not num1
>>> bool(num1)
False
Assignment Operators
= (Equal Symbol)
Assigns value from right-side operand to left- side
operand
>>> num1 = 2
>>> num2 = num1
>>> num2
2
>>> country = 'India'
>>> country
'India'
+=
It adds the value of right-side >>> num1 = 10
operand to the left-side operand >>> num2 = 2
and assigns the result to the left- >>> num1 += num2
side operand
Note: x += y is same as x = x + y
>>> num1
12
>>> num2
2
>>> str1 = 'Hello'
>>> str2 = 'India'
>>> str1 += str2
>>> str1
'HelloIndia'
-=
It subtracts the value of right-side operand from the
left-side operand and assigns the result to left-side
operand
Note: x -= y is same as x = x – y
>>> num1 = 10
>>> num2 = 2
>>> num1 -= num2
>>> num1
8
*=
It multiplies the value of right-side operand with the value of
left-side operand and assigns the result to left-side operand
Note: x *= y is same as x = x * y
>>> num1 = 2
>>> num2 = 3
>>> num1 *= 3
>>> num1
6
>>> a = 'India'
>>> a *= 3
>>> a
'IndiaIndiaIndia'
/=
It divides the value of left-side operand by the value of
right-side operand and assigns the result to left-side
operand
Note: x /= y is same as x = x / y
>>> num1 = 6
>>> num2 = 3
>>> num1 /= num2
>>> num1
2.0
%=
It performs modulus operation using two operands and
assigns the result to left-side operand
Note: x %= y is same as x = x % y
//=
It performs floor division using two operands and
assigns the result to left-side operand
Note: x //= y is same as x = x // y
**=
It performs exponential (power) calculation on
operators and assigns value to the left-side operand
Note: x **= y is same as x = x ** y
>>> num1 = 2
>>> num2 = 3
>>> num1 **= num2
>>> num1
8
Identity Operators
Identity operators are used to determine whether the
value of a variable is of a certain type or not.
Identity operators can also be used to determine
whether two variables are referring to the same
object or not
>>> num2 = num1
>>> id(num1)
>>> num1 = 5 1433920576
>>> type(num1) is int >>> id(num2)
1433920576
True >>> num1 is num2
True
>>> num1 is not num2
False
Membership Operators
Membership operators are used to check if a value is a
member of the given sequence or not.
>>> a = [1,2,3]
>>> 2 in a
True
>>> '1' in a
False
>>> a = [1,2,3]
>>> 10 not in a
True
>>> 1 not in a
False
Data Types in Python
Numeric
Sequence
Sets
None
Dictionary
Operators:
Arithmetic Operators
Assignment Operators
Logical Operators
Relational Operators
Identity Operators
Membership Operators
EXPRESSIONS
An expression is defined as a combination of
constants, variables, and operators.
An expression always evaluates to a value.
A value or a standalone variable is also considered as
an expression but a standalone operator is not an
expression.
(i) 100 (iv) 3.0 + 3.14
(ii) num (v) 23/3 -5 * 7(14 -2)
(iii) num – 20.4 (vi) "Global" + "Citizen"
Precedence of Operators
Evaluation of the expression is based on precedence of operators
When an expression contains different kinds of operators,
precedence determines which operator should be applied first
Higher precedence operator is evaluated before the lower
precedence operator.