Python Keywords
Python Keywords
Python Keywords are special reserved words that convey a special meaning to the
compiler/interpreter. Each keyword has a special meaning and a specific operation. These
keywords can't be used as a variable. Following is the List of Python Keywords.
A B A and B
A B A and B
5. or - It is a logical operator in Python. It returns true if one of the conditions is true.
Consider the following truth table.
6. not - It is a logical operator and inverts the truth value. Consider the following truth
table.
A Not A
True False
False True
7. assert - This keyword is used as the debugging tool in Python. It checks the correctness
of the code. It raises an AssertionError if found any error in the code and also prints the
message with an error.
Example:
a = 10
b=0
print('a is dividing by Zero')
assert b != 0
print(a / b)
Output:
a is dividing by Zero
Runtime Exception:
Traceback (most recent call last):
File "/home/40545678b342ce3b70beb1224bed345f.py", line 4, in
assert b != 0, "Divide by 0 error"
AssertionError: Divide by 0 error
Python Literals
Python Literals can be defined as data that is given in a variable or constant.
Python supports the following literals:
1. String literals:
String literals can be formed by enclosing a text in the quotes. We can use both single as well as
double quotes to create a string.
Example:
"Aman", '12345'
Types of Strings:
There are two types of Strings supported in Python:
a) Single-line String- Strings that are terminated within a single-line are known as Single
line Strings.
Example:
text1='hello'
b) Multi-line String - A piece of text that is written in multiple lines is known as multiple
lines string.
There are two ways to create multiline strings:
Numbers( can be both Integers of unlimited size Real numbers with In the form of a+bj where a forms the
positive and negative) followed by lowercase or both integer and
real part and b forms the imaginary part
with no fractional uppercase L eg: fractional part eg: -
part.eg: 100 87032845L 26.2 of the complex number. eg: 3.14j
#Float Literal
float_1 = 100.5
float_2 = 1.5e2
#Complex Literal
a = 5+3.14j
print(x, y, z, u)
print(float_1, float_2)
print(a, a.imag, a.real)
Output:
20 100 141 301
100.5 150.0
(5+3.14j) 3.14 5.0
Example
dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}
print(dict)
Output:
{'name': 'Pater', 'Age': 18, 'Roll_nu': 101}
Tuple:
o Python tuple is a collection of different data-type. It is immutable which means it
cannot be modified after creation.
o It is enclosed by the parentheses () and each element is separated by the comma(,).
Example
tup = (10,20,"Dev",[2,3,4])
print(tup)
Output:
(10, 20, 'Dev', [2, 3, 4])
Set:
o Python set is the collection of the unordered dataset.
o It is enclosed by the {} and each element is separated by the comma(,).
Example: - Set Literals
set = {'apple','grapes','guava','papaya'}
print(set)
Output:
{'guava', 'apple', 'papaya', 'grapes'}
Python Operators
The operator can be defined as a symbol which is responsible for a particular operation
between two operands. Operators are the pillars of a program on which the logic is built in a
specific programming language. Python provides a variety of operators, which are described
as follows.
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations between two operands. It
includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor
division), and exponent (**) operators.
Consider the following table for a detailed explanation of arithmetic operators.
Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first operand. If the first operand is
less than the second operand, the value results negative. For example,
if a = 20, b = 10 => a - b = 10
/ (divide) It returns the quotient after dividing the first operand by the second operand.
* (Multiplication) It is used to multiply one operand with the other. For example,
% (reminder) It returns the reminder after dividing the first operand by the second operand.
// (Floor It gives the floor value of the quotient produced by dividing the two operands.
division)
Comparison operator
Comparison operators are used to comparing the value of the two operands and returns
Boolean true or false accordingly. The comparison operators are described in the following
table.
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the condition becomes true.
> If the first operand is greater than the second operand, then the condition becomes true.
< If the first operand is less than the second operand, then the condition becomes true.
Assignment Operators
The assignment operators are used to assign the value of the right expression to the left
operand. The assignment operators are described in the following table.
Operator Description
+= It increases the value of the left operand by the value of the right operand and assigns the modified
value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a = a+ b
-= It decreases the value of the left operand by the value of the right operand and assigns the
modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be
*= It multiplies the value of the left operand by the value of the right operand and assigns the
modified value back to then the left operand. For example, if a = 10, b = 20 => a* = b will be
%= It divides the value of the left operand by the value of the right operand and assigns the reminder
back to the left operand. For example, if a = 20, b = 10 => a % = b will be equal to a = a % b
and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.
Bitwise Operators
The bitwise operators perform bit by bit operation on the values of the two operands.
Consider the following example.
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the result.
Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit
<< (left shift) The left operand value is moved left by the number of bits present in the right operand.
>> (right shift) The left operand is moved right by the number of bits present in the right operand.
Logical Operators
The logical operators are used primarily in the expression evaluation to make a decision.
Python supports the following logical operators.
Operator Description
and If both the expression are true, then the condition will be true. If a and b are the two expressions,
or If one of the expressions is true, then the condition will be true. If a and b are the two expressions,
not If an expression a is true, then not (a) will be false and vice versa.
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 Description
in It is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand (list, tuple, or
dictionary).
Identity Operators
The identity operators are used to decide whether an element certain class or type.
Operator Description
is It is evaluated to be true if the reference present at both sides point to the same object.
is not It is evaluated to be true if the reference present at both sides do not point to the same object.
Operator Precedence
The precedence of the operators is essential to find out since it enables us to know which
operator should be evaluated first. The precedence table of the operators in Python is given
below.
Operator Description
** The exponent operator is given priority over all the others used in the expression.
<= < > >= Comparison operators (less than, less than equal to, greater than, greater then equal to).