Python operators

Python Operators 

Introduction

After learning Python variables and data types, the next important topic is Python Operators.

Operators are used to perform operations on values and variables.

In this blog, we'll learn about:
These topics are important for problem-solving, coding and interviews.

What are Operators?

Python can perform calculations using operators. Python provides types of operators that can manipulate and work with different data types. You can also perform normal operation in the IDLE shell


Types of operations

Python have different types of operations:
Used to perform mathematical calculations on numeric values.
  • Addition (+): Addition is used for adding two operands in the simplest form of expression, like op1 operator op2. e.g. 5+2=7 or 7+5=12
  • Subtraction (-): Subtraction is used for subtracting two operands. e.g. 10-4 =6 or 50-25 = 25
  • Multiplication (*): Multiplication is used to multiply two operands together. e.g. 19* 2=38 or 2*4=8
  • Division (/): Division is used to get the result as the quotient of the operands. e.g. 10/5=2 or 50/5=10
  • Modulus (%): Modulus is used to get the result as a reminder of the two operands. e.g. 15%4=3
  • Power (**): Power is used to get the number of times an integer is multiplied by itself. e.g. 2**4= 16
  • Floor division (//): Floor division is used to get the quotient in an integer, even if the result is in decimal. e.g. 5//2=2
Example:

a=10
b=3

print(a+b) #13
print(a-b) #7
print(a*b) #30
print(a/b) #3.33
print(a%10) #1
print(a//b) #3

2. Comparison Operators


Used to perform a comparison between two operands.
  • Equal to (==): It is used to compare two values; if equal, then return true, else false. e.g. 5==0 then false
  • Not equal to (!=): It is just the opposite of equal to; if values are not equal, then return true, else false. e.g. 5!=0 then true 
  • Greater than (>): It is used to compare which value is greater. e.g. 5>10 then false
  • Less than (<): It is used to compare which value is smaller. e.g. 5<10 then true
  • Greater than or equal (>=): It is used to compare values; if greater than or equal to, then true, else false. e.g. 10>=5 then true 
  • Less than or equal (<=): It is used to compare values; if less than or equal to, then true, else false. e.g. 5<=5 then true
Example:

a= 3
b=4

print(a<b) #true
print(a>b) #false
print(a==b) #false

3. Logical Operators

Used to combine conditions
  • and: It results in true if both conditions are true. 
  • or: It results in true if one of the two conditions is true.
  • not: It reverses the result
Example:

age= 20
marks= 85

print(age> 18 and marks >50) # true
print(age<18 or marks >50) #true
print(not(age>18)) #false

Summary

  • Operator performs operations on values.
  • Arithmetic operations are used for calculation
  • Comparison operation compares values
  • Logical operation combines conditions

Practice Questions

Section A: Basic questions

1. What are operators in Python?

2. Write the output of the following program:

    print(10+5*2)

3. What operator is used for 
  • Power
  • Modulus
  • Floor Division
4. What is the output of the following program:
 
    print(5//4)

5. What is the difference between / and //?

Section B: Arithmetic operators

6. Write a python program to:
  • Take two number
  • Print their sum, difference, product and division
7. What will be the output?
    a=8
    b=5
    print(a%b)

8. Write the output:
    
    print(4**4)

Section C: Comparison Operator

9. What will be the output?

    x=10
    y=4
    print(x>y)
10. Write a program to check if a number is greater than 60.


Comments

Popular posts from this blog

Python Input and Output(input() & print())

Python Conditional Statements(if, elif, else)

Introduction To Database Management System