Bytes

home

bytes

tutorials

python

operators in python

Operators in Python

Module - 2 Basics of Python Programming
Operators in Python

Overview

In Python, operators ūüĒć are special symbols or keywords that perform some operations on one or more operands ūüĒĘ. In this lesson

ūüďĖ

ūüďĖ
, we will learn about operators and different types ūüďä of operators ūüĒĘ in Python.

Frame 36-min.png

What are Operators?

Operators are symbols or words that represent an action or process. They have commonly used in programming ūüíĽ and mathematics ūüßģ to manipulate data or perform calculations. Operators can be classified into different categories, such as arithmetic, relational, logical, and bitwise, each with its own rules and functions. Arithmetic operators perform mathematical operations like addition ‚ěē, subtraction ‚ěĖ, multiplication ‚úĖÔłŹ, and division ‚ěó.Relational operators compare two values and return a Boolean value based on the comparison result. Logical operators combine multiple conditions and return a Boolean value based on the outcome of the mixed conditions. Bitwise operators perform operations on the binary representation of values. Operators are essential for building complex algorithms and functions in programming. Understanding the different types of operators and their functions is necessary to write code effectively and solve problems.

Different Types of Operators

Frame 18-min.png

There are several types of operators in Python, which can be classified as follows:

  1. Arithmetic Operators:

Arithmetic operators are a fundamental part of mathematics and computer programming . They represent mathematical operations such as addition , subtraction , multiplication , and division . These operators are used to perform calculations and manipulate numerical data ūüĒĘ. It is essential to clearly understand these operators to work with and analyze numerical data effectively ūüíĽ. We have arithmetic operators like:

For example:

In the manufacturing industry, tracking the production of different products and understanding how various factors impact the production process is essential. Arithmetic operators are critical tools in Python that allow you to perform mathematical operations on variables representing production data.

production_rate = 100
increase = 25
decrease=20
total_products = 1000
defect_rate = 0.05
total_units = 50


new_production_rate = production_rate + increase
print(new_production_rate)#addition
new_production_rate = production_rate - decrease
print(new_production_rate)#substraction
defective_products = total_products * defect_rate
print(defective_products)#multiplication
cost_per_unit = production_cost / total_units
print(cost_per_unit)#division
cost_per_unit = production_cost // total_units
print(cost_per_unit) #floor division(gives values before decimal)
cost_per_unit = production_cost % total_units
print(cost_per_unit)  #modulus(returns remainder)
  1. Comparison Operators:

Comparison operators are utilized to compare values and return a Boolean True or False value. They include the equality operator (==), inequality operator (!=), greater than operator (>), less than operator (<), greater than or equal to the operator (>=), and less than or equal to the operator (<=).

product_type = "Widget"
order_status = "Fulfilled"
production_rate = 1000 
target_rate = 900
defect_rate = 10 
quality_threshold = 100
production_cost = 2000
target_cost = 1000
order_quantity = 900
available_inventory = 1200

print(product_type == "Widget")  #equality check
print(order_status != "Fulfilled")  #inequality check
print(production_rate > target_rate)  #greater than 
print(defect_rate < quality_threshold) #less than
print(production_cost >= target_cost) #greater than equal to
print(order_quantity <= available_inventory) #less than equal to
  • product_type == "Widget":¬†This comparison can help determine if the product is a Widget. If not, alternative production processes or quality control measures may be necessary.
  • order_status != "Fulfilled":¬†This comparison can check if a customer order is fulfilled. If the order status is not "Fulfilled," production and fulfillment may need to be expedited to meet the customer's expectations.
  • production_rate > target_rate:¬†This comparison checks if the current production rate meets the target rate. If the production rate is too low, it can be increased. It can be slowed down to avoid overproduction if it's too high.
  • defect_rate < quality_threshold:¬†This comparison can help check if a product's defect rate is below a certain quality threshold. If the defect rate is high, additional inspections or changes to production procedures can improve quality control processes.
  • order_quantity <= available_inventory:¬†This comparison helps ensure enough inventory to fulfill a customer order. A supplier must produce or acquire more products if the order exceeds stock.
  • production_cost >= target_cost:¬†This comparison can determine if the production cost exceeds the target cost. If the price is too high, reduce it by optimizing processes, using cheaper materials, or negotiating with suppliers.
  1. Logical Operators:

These are used to combine multiple conditions and return a Boolean ‚úÖ value. These operators work like tools a farmer would use to sort their apples into different baskets based on multiple conditions, such as size and color.

product_A_production_rate = 10
product_B_production_rate =20
assembly_line_1_status = "running" 
assembly_line_2_status = "not running"
product_defect_rate = 0.02

print(product_A_production_rate > 0 and product_B_production_rate > 0) 
print(assembly_line_1_status == "running" or assembly_line_2_status == "running")  
print(not product_defect_rate >= 0.05) 
  1. Bitwise Operators:

These operators are utilized to perform bitwise operations on binary numbers. They include the bitwise and operator (&), the bitwise or operator (|), the bitwise exclusive or operator (^), the bitwise left shift operator (<<), and the bitwise right shift operator (>>).

product_A = 0b10101010
product_B = 0b11001100  

print(product_A & product_B)   #and
print(product_A | product_B)   #or
print(product_A ^ product_B)   #xor
print(product_A << 2) #left shift
print(product_A >> 2)  #right shift
  1. Assignment Operators:

These operators are utilized to assign a value to a variable. They include the equal to the operator (=), the plus equal to the operator (+=), the minus equal to the operator (-=), the multiply equal to the operator (*=), and the divide equal to the operator (/=).

production_rate=50

production_rate += 40
print(production_rate) 

production_rate -= 20  
print(production_rate)

production_rate *= 5
print(production_rate)
  
production_rate /= 10
print(production_rate)  

Conclusion:

This lesson explains what operators are in Python and the different types of operators, including arithmetic, comparison, , logical, and assignment operators. Operators are tools that help manipulate values and make decisions based on them, just like a ūüßĎ‚ÄćūüĆĺ farmer needs tools to count and sort apples.

Key takeaways

  • Operators in Python are special symbols or keywords that perform specific operations on one or more operands. ūüíĽ
  • Several operators in Python include arithmetic, comparison, logical, bitwise, and assignment operators. ūüßģ
  • Arithmetic operators are used for mathematical operations like addition ‚ěē, subtraction ‚ěĖ, multiplication ‚úĖÔłŹ, and division ‚ěó.
  • Comparison operators are utilized to compare values and return a Boolean True or False value ‚úÖ‚ĚĆ.
  • Logical operators are used ūü§Ē for combining multiple conditions and return a Boolean value ‚úÖ‚ĚĆ.
  • Bitwise operators are used for performing bitwise operations on binary numbers ūü§Ė. Assignment operators assign a value to a variable while operating on it ūüĒß.

Quiz

  1. Which of the following is an arithmetic operator in Python?
    1.  &&
    2. ||
    3.  +
    4. !

Answer: C. +

  1. What is the output of the following code snippet?
a = 5
b = 3
print(a % b)
  1. 2
  2. 1
  3. 0
  4. 3

Answer: B. 1

  1. Which of the following is a comparison operator in Python?
    1.  =
    2.  ==
    3. ->
    4. =>

Answer: B. ==

  1. What is the output of the following code snippet?
a = 3
b = 5
print(a > 2 and b < 10)
  1. True
  2. False
  3. None
  4. Error

Answer: A. True

  1. Which of the following is a logical operator in Python?
    1.  %
    2. &
    3. !
    4. or

Answer: D. or

Recommended Courses
Masters in CS: Data Science and Artificial Intelligence
Course
20,000 people are doing this course
Join India's only Pay after placement Master's degree in Data Science. Get an assured job of 5 LPA and above. Accredited by ECTS and globally recognised in EU, US, Canada and 60+ countries.
Certification in Full Stack Data Science and AI
Course
20,000 people are doing this course
Become a job-ready Data Science professional in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

avatar
Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter

Vikash SrivastavaCo-founder & CPTO AlmaBetter

Vikas CTO

Related Tutorials to watch

Top Articles toRead

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2023 AlmaBetter