Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Python notes

Rating
-
Sold
-
Pages
46
Uploaded on
04-06-2024
Written in
2023/2024

The best short written python program notes for your beginning level...

Institution
Course

Content preview

UNIT-II

OPERATORS AVAILABLE IN PYTHON
Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.
For example:
>>>2+3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the
operation.
Arithmetic operator:

Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc.

Operator Meaning Example

+ Add two operands or unary plus x + y+ 2
Subtract right operand from the left
x -y- 2
or unary minus
* Multiply two operands x *y
Divide left operand by the right one
x/y
(always results into float)
x %y
% Modulus-remainder of the division of left operand by
(remainder of
the right
x/y)
// Floor division-division that results into whole number x //y
adjusted to the left in the number line
x**y
** Exponent-left operand raised to the power of right (x to the power
y)

Example 1: Arithmetic operators in Python
x = 15
y=4
# Output: x +y =19
print('x + y =',x+y)


1

,# Output: x-y = 11
print('x -y=',x-y)

# Output: x * y = 60
print('x *y =',x*y)

# Output: x/ y = 3.75
print('x / y=',x/y)

# Output: x//y =3
print('x // y =',x//y)

Output

x + y = 19
x -y = 11
x *y = 60
x / y = 3.75
x //y = 3
x **y = 50625
Comparison operators
Comparison operators are used to compare values. It returns either True or False according to the
condition

Operator Meaning Example

> Greater than –True if left operand is greater than the right x>y
< Less than –True if left operand is less than the right x<y
== Equal to-True if both operands are equal x == y
!= Not equal to-True if operands are not equal x != y
Greater than or equal to- True if left
x >= y
operand is greater than or equal to the right
Less than or equal to-True if left operand is less than or
<= x <= y
equal to the right

Example 2: Comparison operators in Python

x =10


2

,y =12

# Output: x >y is False

print('x > y is' ,x>y)

# Output: x <y is True

print('x < y is', x<y)

# Output: x == y is False

print('x == y is ',x==y)

# Output: x!= y is True

print('x != y is ',x!=y)

# Output: x >= y is False

print('x >= y is' ,x>=y)

# Output: x <= y is True

print('x <= y is', x<=y)

Output
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
Logical operators
Logical operators are the and, or, not operators.

Operator Meaning Example

and True if both the operands are true x and y

or True if either of the operands is true x or y

not True if operand is false(complements the not x
operand)




3

, Example 3: Logical Operators in Python

x = True

y = False

print('x and y is' ,x and y)

print('x or y is', x or y)

print('not x is', not x)

Output

x and y is False

x or y is True

not x is False

Bitwise operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit,
hence the name.

For example, 2 is 10 in binary and 7 is 111.

In the table below: Let binary) = 10 (0000 1010 in binary) and = 4 (0000 0100 in binary)

Operator Meaning Example

& Bitwise AND x & y = 0(00000000)

| Bitwise OR x | y = 14(00001110)

~ Bitwise NOT ~x = -11(11110101)

^ Bitwise XOR x ^ y = 14(00001110)

>> Bitwise right shift x >> 2 = 2(00000010)

<< Bitwise left shift x << 2 = 40(00101000)


Assignment operators:-

Assignment operators are used in Python to assign values to variables. a = 5 is a simple
assignment operator that assigns the value 5 on the right to the variable a on the left. There are

4

Written for

Institution
Secondary school
Course
School year
1

Document information

Uploaded on
June 4, 2024
Number of pages
46
Written in
2023/2024
Type
Class notes
Professor(s)
Abdulkathar
Contains
All classes

Subjects

$6.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sjayasuriya

Get to know the seller

Seller avatar
sjayasuriya
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions