Skip to main content

Operators in Python


Operators

Operators are the symbols which perform the operation on some values. These values are known as operands. For example if we write a + b, the operator + acts on the two operand a and b, this is called binary operator. If an operator acts on a single operand then it is called unary operator.
Example:-
-a                            Unary Minus operator
+a                           Unary Plus Operator
a + b                      Binary Operator

Operators are divided into eight parts:
  • Unary Operator
  • Arithmetic Operator
  • Assignment Operator
  • Relational Operator
  • Logical Operator
  • Identity Operator
  • Membership Operator
  • Bitwise Operator

Unary Operator

When an operator acts on a single variable then it is called Unary operator.
There are two types of unary operator:
Unary Plus Operator
Unary Minus Operator
Example:-
x = 10
a = x
b = -x
print(“The value of a is: “,a)
print(“The value of b is: “,b)
Output:-
The value of a is: 10
The value of b is: -10

Arithmetic Operator

These operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, etc.
Since this operator works on two operand it is called binary operator. There are seven arithmetic operators in python:
Operator
Meaning
Example
Result
+
Addition
10 + 2
12
Subtraction
12 – 2
10
*
Multiplication
10 * 2
20
/
Division
10 / 2
5
%
Remainder
13 % 2
1
**
Exponential
10 **2
100
//
Quotient
13 // 2
6

Relational Operator

This operator is also known as comparison operator because it compares two values and returns the Boolean value i.e. either ‘True’ or ‘False’.
The value of x is 10 and y is 9
Operator
Meaning
Example
Result
> 
Greater than
x > y
True
< 
Less than
x < y
False
>=
Greater than equal to
x >= y
True
<=
Less than equal to
x <= y
False
==
Equal to
x == y
False
!=
Not equal to
x != y
True

Assignment Operator

Assignment operator is used to assign values in the variable or operand. Assignment operator is also used perform simple arithmetic operations like addition, subtraction, multiplication, etc.
The value of x is 10 and y is 9
Operator
Meaning
Example
Result
=
Assignment Operator
x=y
x=3
+=
Assignment plus operator
x+=y
x = x + y
x = 19
-=
Assignment minus operator
x-=y
x = 1
*=
Multiplication assignment operator
x*=y
x = 90

Logical Operator

Logical operator is used to check two or more than two conditions. The logical operator also returns the Boolean result either true or false.
The value of x is 10 and y is 9
Operator
Meaning
Example
Result
AND
It returns true if two or more conditions are true
x=10 and y=9
True
OR
It returns true if any two or more conditions are true
x=10 or y=19
True
NOT
Returns true if operand is false
Not x=10
False

Identity Operator

Identity operators are used to compare the objects with the same memory location.
The value of x is 9 and y is 9.0
Operator
Meaning
Example
Result
is
Returns true if both variables are the same object
x is y
False
Is not
Returns true if both variables are not the same object
x is not y
True

Membership Operator

Membership Operators are the operators, which are used to check whether a value exists in the sequence like string, list, etc. or not.
The value of x is 9 and list1 = [5,7,9,11,13]
Operator
Meaning
Example
Result
In
Returns true if value found in a sequence
x in list1
True
not in
Returns true if value not found in a sequence
x not in list1
False

Bitwise Operator

Python bitwise operators are used to perform bitwise calculations on integers. The integers are converted into binary format and then operations are performed bit by bit, hence the name bitwise operators. Python bitwise operators work on integers only and the final output is returned in the decimal format.

Comments

Popular posts from this blog

Q & A Chapter 1 | Internet Basics | Class 10 CBSE | Computer Application

 Q & A of Chapter 1 Internet Basics A. Tick the correct options – 1. (b) TCP/IP 2. (a) Internet Service Provider 3. (a) URL 4. (b) HTTP 5. (a) Bridge 6. (a) home page 7. (d) All of these 8. (b) Domain Naming System 9. (b) Rules 10. (d) ARPANET B. Very Short Answer Type Questions – 1. In the URL http://www.myfirm.co.in/about.html , what do the following components depict? (a) http                                                 (b) in (a) Http represents a type of server and the protocol on the Internet (b) ‘in’ represents the domain extension of the website. 2. In the URL http://www.favmusic.com/contactus.html what does www.favmusic stand for? www.favmusic represents the domain name i.e. favmusic in www. 3. What does FTP stands for? FTP stands for File Transfer Protocol. 4. Differentiate between webpage and home page. A webpage refers to an electronic page usually written in HTML. It may contain text, images, graphics or hyperlinks. Th

Q & A Chapter 2 | Internet Services | Class 10 CBSE | Computer Application

 Q & A of Chapter 2 A. Tick the correct option – 1. (b) Browser 2. (a) surfing 3. (a) downloading 4. (d) FTP 5. (b) e-mail 6. (c) chat 7. (a) @ 8. (c) Acronyms 9. (d) Emoticons 10. (a) newsgroup B. Very short answer questions – 1. Name a few search engines. Which is the most popular one? Some common search engines are Bing, Google, Ask, Yahoo and MSN. The most popular search engine is Google. 2. What is an Acronym of FTP? File Transfer Protocol. 3. What is a newsreader? Give an example. A newsreader is a software tool or utility used to browse through newsgroup postings. A very popular newsreader in use today is Microsoft Outlook Express. 4. Name three sites that support e-mail services. Gmail, Outlook and Yahoo are three sites that support e-mail services. 5. What is the difference between attachments and FTP? Attachments are the files which are send through an e-mail for communication, while in FTP transfer of files takes place between FTP server and FTP clients and no communicat

Bit by Bit | Chapter 6 - Graphics and Sound in QB64 | QBasic | Class 7 CBSE | Computer Application

Bit by Bit | Chapter 6 - Graphics and Sound in QB64 A. Tick the correct option. 1. (d) screen mode 0 2. (b) 640 x 350 3. (d) White 4. (a) Lower-Right 5. (c) PSET 6. (c) Generate a tone for a short duration B. Fill in the blanks. 1. CIRCLE 2. PAINT 3. PLAY 4. Graphic 5. Pixels 6. Box C. True or False. 1. True 2. False 3. False 4. True 5. True 6. True D. Answer the following questions. 1. What is the use of SCREEN statement? The SCREEN statement is used to set the screen mode for displaying graphics. There are many screen modes and each screen modes have their own number which is from SCREEN 1 to SCREEN 13. The various screen modes differ in resolution. 2. What is a pixel? How is it related to resolution? The graphics which is displaying on the computer screen is made up of thousands of dots called pixel. Pixel stands for picture element. The number of pixels in an image determines the resolution of the image. Higher the resolution, cleaner the image will appear. 3. What is the differenc