Python / Python Basics

Exploring Python Operators

Operators are used to perform operations on variables and values. Python divides the operators in the following groups: Arithmetic operators, Assignment operators, Comparison oper…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Introduces fundamental Python concepts, including syntax, variables, and data types.

1. Introduction

This tutorial aims to introduce you to the wide range of operators available in Python. Operators are special symbols that carry out arithmetic or logical computations. They are the backbone of any programming logic, and Python provides a variety of these operators categorized into Arithmetic, Assignment, Comparison, Logical, Identity, Membership, and Bitwise operators.

By the end of this tutorial, you should be able to understand these different types of operators and apply them effectively in Python programming. The only prerequisite for this tutorial is a basic understanding of Python syntax.

2. Step-by-Step Guide

2.1 Arithmetic Operators

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

# Example
x = 10
y = 2

print('x + y =',x+y) # Addition
print('x - y =',x-y) # Subtraction
print('x * y =',x*y) # Multiplication
print('x / y =',x/y) # Division
print('x % y =',x%y) # Modulus
print('x ** y =',x**y) # Exponentiation
print('x // y =',x//y) # Floor division

2.2 Assignment Operators

These are used to assign values to variables.

# Example
a = 10 # Assignment
a += 10 # Addition assignment
a -= 5 # Subtraction assignment
a /= 5 # Division assignment
a *= 2 # Multiplication assignment
a %= 3 # Modulo assignment
a **= 2 # Exponentiation assignment
a //= 3 # Floor division assignment

2.3 Comparison Operators

These are used to compare values. It either returns True or False according to the condition.

# Example
x = 10
y = 12

print('x > y is',x>y) # Greater than
print('x < y is',x<y) # Less than
print('x == y is',x==y) # Equal to
print('x != y is',x!=y) # Not equal to
print('x >= y is',x>=y) # Greater than or equal to
print('x <= y is',x<=y) # Less than or equal to

3. Code Examples

Let's look at some practical examples for the remaining operator types - Logical, Identity, Membership, and Bitwise operators.

3.1 Logical Operators

Logical operators are used to combine conditional statements.

x = True
y = False

print('x and y is',x and y) # Logical AND
print('x or y is',x or y) # Logical OR
print('not x is',not x) # Logical NOT

3.2 Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print('x is z is',x is z) # returns True because z is the same object as x
print('x is y is',x is y) # returns False because x is not the same object as y, even if they have the same content
print('x == y is',x == y) # to demonstrate the difference betweeen "is" and "==": this comparison returns True because x is equal to y

3.3 Membership Operators

Membership operators are used to test whether a value or variable is found in a sequence.

x = ["apple", "banana"]

print('banana' in x) # returns True because a sequence with the value "banana" is in the list
print('pineapple' not in x) # returns True because a sequence with the value "pineapple" is not in the list

3.4 Bitwise Operators

Bitwise operators are used to compare binary numbers.

a = 60            # 60 = 0011 1100 
b = 13            # 13 = 0000 1101 
c = 0

c = a & b;        # 12 = 0000 1100
print("a & b = ", c)

c = a | b;        # 61 = 0011 1101 
print("a | b = ", c)

c = a ^ b;        # 49 = 0011 0001
print("a ^ b = ", c)

c = ~a;           # -61 = 1100 0011
print("~a = ", c)

c = a << 2;       # 240 = 1111 0000
print("a << 2 = ", c)

c = a >> 2;       # 15 = 0000 1111
print("a >> 2 = ", c)

4. Summary

In this tutorial, we covered the different types of Python operators and their uses with practical examples. These operators are fundamental in Python programming and understanding them will help you write more efficient and effective Python code.

5. Practice Exercises

  1. Write a Python script to add two user-input numbers using Arithmetic operators.
  2. Write a Python script to check whether a user-input number is greater than 50, less than 100, and not equal to 90 using Comparison operators.
  3. Write a Python script to check if 'python' is present in the input string using Membership operators.

For further practice, try to build more complex logic using these operators. Happy Coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help