Unit 1 - Lesson 9

9.1.1. Arithmetic Operators - An overview

(a) Python supports 7 arithmetic operators.
(b) An exponent operator is represented by ** in Python.
(e) // is called as Floor division operator.

9.1.2. Using Arithmetic Operators

#Arithmetic Operators are +, -, *, /, **, %, //
num1 = int(input("num1: "))
num2 = int(input("num2: "))

# print num1+num2
print(num1, "+", num2,"=", num1+num2)
# print num1-num2
print(num1, "-", num2,"=", num1-num2)
# print num1*num2
print(num1, "*", num2,"=", num1*num2)
# print num1/num2
print(num1, "/", num2,"=", num1/num2)
# print num1**num2
print(num1, "**", num2,"=", num1**num2)
# print num1%num2
print(num1, "%", num2,"=", num1%num2)
# print num1//num2
print(num1, "//", num2,"=", num1//num2)

9.1.3. Writing a program on arithmetic operators

num1 = int(input ("num1: "))
num2 = int(input ("num2: "))

# Print the addition of num1 and num2
print("Addition of", num1, "and", num2, "=", num1+num2)
# Print the subtraction of num1 and num2
print("Subtraction of", num1, "and", num2, "=", num1-num2)
# Print the multiplication of num1 and num2
print("Multiplication of", num1, "and", num2, "=", num1*num2)
# Print the division of num1 and num2
print("Division of", num1, "and", num2, "=", num1/num2)

9.1.4. Some more arithmetic operators

num1 = int(input("num1: "))
num2 = int(input("num2: "))

# Print the exponenet of num1 to the power of num2
print("Exponent of", num1, "with", num2, "=", num1**num2)
# Print the modulous function of num1 and num2
print("Modulus of", num1, "and", num2, "=", num1%num2)
# Print the floor division function of num1 and num2
print("Floor Division of", num1, "and", num2,"=", num1//num2)

9.1.5. Writing a program on some more arithmetic operators

num1 = int(input("num1: "))
num2 = int(input("num2: "))
print("Exponent of", num1, "with", num2,"=", num1**num2)
print("Modulus of", num1, "and", num2, "=", num1%num2)
print("Floor Division of", num1, "and", num2, "=", num1//num2)

9.1.6. Explanation of divmod function

#Program to illustrate divmod() function
# Input num1 with message "Enter number-1: "
num1= int(input("num1: "))
# Input num2 with message "Enter number-2: "
num2= int(input("num2: "))
# use divmod() and store results in 2 variables x, and y
print((num1), '//', (num2), '=', (num1//num2)) # replace variables in () and print the results
print((num1), '%', (num2), '=', (num1%num2))

9.2.1. Comparison Operators - An overview

num1 = int(input("num1: "))
num2 = int(input("num2: "))

# Print Is num1 greater than num2.
print("Is", num1, "greater than", num2, "=", num1>num2)
# Print Is num1 less than num2.
print("Is", num1, "less than", num2, "=", num1<num2)
# Print Is num1 equal to num2.
print("Is", num1, "equal to", num2, "=", num1==num2)
# Print Is num1 not equal to num2.
print("Is", num1, "not equal to", num2, "=", num1!=num2)
# Print Is num1 less than or equal to num2.
print("Is", num1, "less than or equal to", num2, "=", num1<=num2)
# Print Is num1 greater than or equal to num2.
print("Is", num1, "greater than or equal to", num2, "=", num1>=num2)

9.2.2. Using Comparison operators

# Comparision Operators >, <, ==, !=, >=, <= on numbers
num1= int(input("num1: "))
num2= int(input("num2: "))
print("Is", num1, "greater than", num2, "=", num1>num2)
print("Is", num1, "less than", num2, "=", num1<num2)
print("Is", num1, "equal to", num2, "=", num1==num2)
print("Is", num1, "not equal to", num2, "=", num1!=num2)
print("Is", num1, "greater than or equal to", num2, "=", num1>=num2)
print("Is", num1, "less than or equal to", num2, "=", num1<=num2)

9.2.3. Comparison operators with strings

a= input("str1: ")
b= input("str2: ")
print("Is", a, "greater than", b, "=", a>b)
print("Is", a, "less than", b,"=", a<b)
print("Is", a, "equal to", b, "=", a==b)
print("Is", a, "not equal to", b, "=", a!=b)
print("Is", a, "greater than or equal to", b, "=", a>=b)
print("Is", a, "less than or equal to", b, "=", a<=b)

9.2.4. Writing Comparison Operators on Strings

str1 = input("str1: ")
str2 = input("str2: ")
# Print Is str1 greater than str2
print("Is", str1,"greater than", str2, "=", str1>str2)
# Print Is str1 less than str2
print("Is", str1, "less than", str2, "=", str1<str2)
# Print Is str1 is equal to str2
print("Is", str1, "equal to", str2, "=", str1==str2)
# Print Is str1 not equal to str2
print("Is", str1, "not equal to", str2, "=", str1!=str2)
# Print Is str1 greater than or equal to str2
print("Is", str1, "greater than or equal to", str2, "=", str1>=str2)
# Print Is str1 less than or equal to str2
print("Is", str1, "less than or equal to", str2, "=", str1<=str2)

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.