0% found this document useful (0 votes)
28 views30 pages

Python Keywords and Operators Guide

The document provides an overview of Python keywords and operators, categorizing them into types such as arithmetic, comparison, assignment, bitwise, logical, membership, and identity operators. It explains the function and examples of each operator type, including their syntax and usage. Additionally, it discusses operator precedence, which affects how expressions are evaluated in Python.

Uploaded by

Akash Shaju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views30 pages

Python Keywords and Operators Guide

The document provides an overview of Python keywords and operators, categorizing them into types such as arithmetic, comparison, assignment, bitwise, logical, membership, and identity operators. It explains the function and examples of each operator type, including their syntax and usage. Additionally, it discusses operator precedence, which affects how expressions are evaluated in Python.

Uploaded by

Akash Shaju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Keywords in

Python
• All the Python keywords contain lowercase letters only.

• These are reserved words and you cannot use them as


constant or variable or any other identifier names.
Basic
Operators
Types of Operator
• An operator is a special symbol which indicates a certain
process is carried out.

• Programmers work with data. The operators are used to


process data.
Logical
Assignment Bitwise
Comparison Operators
Operators Operators
Membership
(i.e., Relational)
Operators Operators
Arithmetic Identity
Operators Operators
Operator
1. Arithmetic
Operators
• Eg:
• >>>a = 21
• >>>b = 10
• >>>c = 0
• # perfom addition, subtaction,
multiplication and division.
• # display the remainder
Operator Description Example

+ Addition Adds values on either side of the operator. a + b = 30


Subtracts right hand operand from left
- Subtraction a – b = -10
hand operand.
Multiplies values on either side of the
* Multiplication a * b = 200
operator
Divides left hand operand by right hand
/ Division b/a=2
operand
Divides left hand operand by right hand
% Modulus b%a=0
operand and returns remainder
Performs exponential (power) calculation
** Exponent a**b =10 to the power 20
on operators

Floor Division - The division of operands


// where the result is the quotient in which the 9//2 = 4 and 9.0//2.0 = 4.0
digits after the decimal point are removed.
2. Comparison
Operators
• These operators compare the values on either sides of them
and decide the relation among them. They are also called
Relational operators.

Operator Description Example


If the values of two operands are equal, then the
== (a == b) is not true.
condition becomes true.
If values of two operands are not equal, then
!=
condition becomes true.
(a <> b) is true. This
If values of two operands are not equal, then
<> is similar to !=
condition becomes true.
operator.
If the value of left operand is greater than the value
> (a > b) is not true.
of right operand, then condition becomes true.
If the value of left operand is less than the value of
< (a < b) is true.
right operand, then condition becomes true.
If the value of left operand is greater than or equal
>= to the value of right operand, then condition (a >= b) is not true.
becomes true.
If the value of left operand is less than or equal to
<= the value of right operand, then condition becomes (a <= b)
true.

Assume variable a holds 10 and variable b holds 20, then −


3. Assignment
Operator
Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
Assigns values from right side c = a + b assigns value of a + b into
=
operands to left side operand c
It adds right operand to the left
+= Add AND operand and assign the result to left c += a is equivalent to c = c + a
operand
It subtracts right operand from the
-= Subtract AND left operand and assign the result to c -= a is equivalent to c = c - a
left operand
It multiplies right operand with the
*= Multiply AND left operand and assign the result to c *= a is equivalent to c = c * a
left operand
It divides left operand with the right
c /= a is equivalent to c = c / ac /= a
/= Divide AND operand and assign the result to left
is equivalent to c = c / a
operand
%= Modulus It takes modulus using two operands
c %= a is equivalent to c = c % a
AND and assign the result to left operand
Performs exponential (power)
**= Exponent
calculation on operators and assign c **= a is equivalent to c = c ** a
AND
value to the left operand
It performs floor division on
//= Floor Division operators and assign value to the left c //= a is equivalent to c = c // a
operand
4. Bitwise
Operator
• Bitwise operator works on bits and performs bit by bit
operation.
• Assume if a = 60; and b = 13;
• Now in binary format they will be as follows −
• a = 0011 1100
• b = 0000 1101

• a&b = 0000 1100


• a|b = 0011 1101
• a^b = 0011 0001
• ~a = 1100 0011
Operator Description Example

Operator copies a bit to the result


& Binary AND (a & b) (means 0000 1100)
if it exists in both operands
It copies a bit if it exists in either
| Binary OR (a | b) = 61 (means 0011 1101)
operand.
It copies the bit if it is set in one
^ Binary XOR (a ^ b) = 49 (means 0011 0001)
operand but not both.
(~a ) = -61 (means 1100 0011 in
~ Binary Ones It is unary and has the effect of
2's complement form due to a
Complement 'flipping' bits.
signed binary number.
The left operands value is moved
<< Binary Left Shift left by the number of bits specified a << = 240 (means 1111 0000)
by the right operand.

The left operands value is moved


>> Binary Right
right by the number of bits a >> = 15 (means 0000 1111)
Shift
specified by the right operand.
5. Logical
Operator
6. Membership
Operator
• Python’s membership operators test for membership in a
sequence, such as strings, lists, or tuples.
• There are two membership operators
7. Identity
Operator
• Identity operators compare the memory locations of two objects.
• There are two Identity operators
Python Operator
Precedence
• Operator precedence determines the grouping of terms in an expression.
• This affects how an expression is evaluated.
• Certain operators have higher precedence than others.
• The following table lists all operators from highest precedence to lowest:

Operator Description

** Exponentiation (raise to the power)

Ccomplement, unary plus and minus (method names for the last two are +@
~+-
and -@)

* / % // Multiply, divide, modulo and floor division


+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'td>
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators

You might also like