Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Keywords
Next article icon

In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types ofPython operators. 

Types of Operators in Python

Python Operators

Arithmetic Operators in Python

PythonArithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication anddivision.

In Python 3.x the result of division is a floating-point while in Python 2.x division of 2 integers was an integer. To obtain an integer result in Python 3.x floored (// integer) is used.

Example of Arithmetic Operators in Python:

Python
# Variablesa=15b=4# Additionprint("Addition:",a+b)# Subtractionprint("Subtraction:",a-b)# Multiplicationprint("Multiplication:",a*b)# Divisionprint("Division:",a/b)# Floor Divisionprint("Floor Division:",a//b)# Modulusprint("Modulus:",a%b)# Exponentiationprint("Exponentiation:",a**b)

Output
Addition: 19Subtraction: 11Multiplication: 60Division: 3.75Floor Division: 3Modulus: 3Exponentiation: 50625

Note: Refer toDifferences between / and //for some interesting facts about these two Python operators.

Comparison of Python Operators

In PythonComparisonof Relational operators compares the values. It either returnsTrue orFalse according to the condition.

Example of Comparison Operators in Python

Let's see an example of Comparison Operators in Python.

Python
a=13b=33print(a>b)print(a<b)print(a==b)print(a!=b)print(a>=b)print(a<=b)

Output
FalseTrueFalseTrueFalseTrue

Logical Operators in Python

PythonLogical operators performLogical AND,Logical OR and Logical NOT operations. It is used to combine conditional statements.

The precedence of Logical Operators in Python is as follows:

  1. Logical not
  2. logical and
  3. logical or

Example of Logical Operators in Python:

Python
a=Trueb=Falseprint(aandb)print(aorb)print(nota)

Output
FalseTrueFalse

Bitwise Operators in Python

PythonBitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers.

Bitwise Operators in Python are as follows:

  1. Bitwise NOT
  2. Bitwise Shift
  3. Bitwise AND
  4. Bitwise XOR
  5. Bitwise OR

Example of Bitwise Operators in Python:

Python
a=10b=4print(a&b)print(a|b)print(~a)print(a^b)print(a>>2)print(a<<2)

Output
014-1114240

Assignment Operators in Python

PythonAssignment operators are used to assign values to the variables. This operator is used to assign the value of the right side of the expression to the left side operand.

Example of Assignment Operators in Python:

Python
a=10b=aprint(b)b+=aprint(b)b-=aprint(b)b*=aprint(b)b<<=aprint(b)

Output
102010100102400

Identity Operators in Python

In Python,is andis not are theidentity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical. 

is True if the operands are identical
is not True if the operands are not identical

Example of Identity Operators in Python:

Python
a=10b=20c=aprint(aisnotb)print(aisc)

Output
TrueTrue

Membership Operators in Python

In Python,in andnot in are the membership operators that are used to test whether a value or variable is in a sequence.

in True if value is found in the sequence
not in True if value is not found in the sequence

Examples of Membership Operators in Python:

Python
x=24y=20list=[10,20,30,40,50]if(xnotinlist):print("x is NOT present in given list")else:print("x is present in given list")if(yinlist):print("y is present in given list")else:print("y is NOT present in given list")

Output
x is NOT present in given listy is present in given list

Ternary Operator in Python

in Python,Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. 

It simply allows testing a condition in asingle line replacing the multiline if-else making the code compact.

Syntax :  [on_true] if [expression] else [on_false] 

Examples of Ternary Operator in Python:

Python
a,b=10,20min=aifa<belsebprint(min)

Output
10

Precedence and Associativity of Operators in Python

In Python,Operator precedence and associativity determine the priorities of the operator.

Operator Precedence in Python

This is used in an expression with more than one operator with different precedence to determine which operation to perform first.

Example:

Python
expr=10+20*30print(expr)name="Alex"age=0ifname=="Alex"orname=="John"andage>=2:print("Hello! Welcome.")else:print("Good Bye!!")

Output
610Hello! Welcome.

Operator Associativity in Python

If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.

Example:

Python
print(100/10*10)print(5-2+3)print(5-(2+3))print(2**3**2)

Output
100.060512

To try your knowledge of Python Operators, you can take out thequiz on Operators in Python

Python Operator Exercise Questions

Below are two Exercise Questions on Python Operators. We have covered arithmetic operators and comparison operators in these exercise questions. For more exercises on Python Operators visit the page mentioned below.

Q1.Code to implement basic arithmetic operations on integers

Python
num1=5num2=2sum=num1+num2difference=num1-num2product=num1*num2quotient=num1/num2remainder=num1%num2print("Sum:",sum)print("Difference:",difference)print("Product:",product)print("Quotient:",quotient)print("Remainder:",remainder)

Output
Sum: 7Difference: 3Product: 10Quotient: 2.5Remainder: 1

Q2.Code to implement Comparison operations on integers

Python
num1=30num2=35ifnum1>num2:print("The first number is greater.")elifnum1<num2:print("The second number is greater.")else:print("The numbers are equal.")

Output
The second number is greater.

Quiz:

Related Posts:

Recommended Problems:

Explore more Exercises:Practice Exercise on Operators in Python


Arithmetic Operators in Python
Visit Courseexplore course icon
Video Thumbnail

Arithmetic Operators in Python

Video Thumbnail

Logical Operators in Python

Video Thumbnail

Identity Comparison Operators in Python

Video Thumbnail

Bitwise Operators in Python Part 1

Video Thumbnail

Bitwise Operators in Python Part 2

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp