This document is a comprehensive guide to basic programming in Python, focusing on various types of operators such as arithmetic, assignment, comparison, logical, bitwise, identity, and membership operators. It provides numerous examples for arithmetic operations, including user input handling, and explains how to perform calculations like summation, differences, products, quotients, and more. Additionally, it covers functions from the math module, along with practical problems to reinforce understanding of these concepts.
Topic
• Operator
• ArithmeticOperation
• Assignment Operation
• Arithmetic Operation More Example
• More Built in Function Example
• More Math Module Example
4.
Operator in Python
•Operators are special symbols in that carry out arithmetic or logical
computation.
• The value that the operator operates on is called the operand.
• Type of Operator in Python
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
• Identity operators
• Membership operators
Assignment Operator inPython
Operation Operator
Assign =
Add AND Assign +=
Subtract AND Assign -=
Multiply AND Assign *=
Divide AND Assign /=
Modulus AND Assign %=
Exponent AND Assign **=
Floor Division Assign //=
Note: Logical and Bitwise Operator can be used with assignment.
Summation of twonumber – User Input
a = int(input())
b = int(input())
sum = a + b
print(sum)
10.
Difference of twonumber
a = int(input())
b = int(input())
diff = a - b
print(diff)
11.
Product of twonumber
a = int(input())
b = int(input())
pro = a * b
print(pro)
12.
Quotient of twonumber
a = int(input())
b = int(input())
quo = a / b
print(quo)
13.
Reminder of twonumber
a = int(input())
b = int(input())
rem = a % b
print(rem)
14.
Practice Problem 1.1
Inputtwo Number form User and calculate the followings:
1. Summation of two number
2. Difference of two number
3. Product of two number
4. Quotient of two number
5. Reminder of two number
Floor Division
a =int(input())
b = int(input())
floor_div = a // b
print(floor_div)
a = float(input())
b = float(input())
floor_div = a // b
print(floor_div)
a = 5
b = 2
quo = a/b
= 5/2
= 2.5
quo = floor(a/b)
= floor(5/2)
= floor(2.5)
= 2
19.
Find Exponent (a^b).[1]
a = int(input())
b = int(input())
# Exponent with Arithmetic Operator
# Syntax: base ** exponent
exp = a ** b
print(exp)
20.
Find Exponent (a^b).[2]
a = int(input())
b = int(input())
# Exponent with Built-in Function
# Syntax: pow(base, exponent)
exp = pow(a,b)
print(exp)
21.
Find Exponent (a^b).[3]
a = int (input())
b = int(input())
# Return Modulo for Exponent with Built-in Function
# Syntax: pow(base, exponent, modulo)
exp = pow(a,b,2)
print(exp)
a = 2
b = 4
ans = (a^b)%6
= (2^4)%6
= 16%6
= 4
22.
Find Exponent (a^b).[4]
a = int(input())
b = int(input())
# Using Math Module
import math
exp = math.pow(a,b)
print(exp)
23.
Find absolute differenceof two number. [1]
a = int(input())
b = int(input())
abs_dif = abs(a - b)
print(abs_dif)
a = 4
b = 2
ans1 = abs(a-b)
= abs(4-2)
= abs(2)
= 2
ans2 = abs(b-a)
= abs(2-4)
= abs(-2)
= 2
24.
Find absolute differenceof two number. [2]
import math
a = float(input())
b = float(input())
fabs_dif = math.fabs(a - b)
print(fabs_dif)
Practice Problem 1.2
Inputtwo number from user and calculate the followings: (use
necessary date type)
1. Floor Division with Integer Number & Float Number
2. Find Exponential (a^b) using Exponent Operator, Built-in
pow function & Math Module pow function
3. Find Exponent with modulo (a^b%c)
4. Find absolute difference of two number using Built-in
abs function & Math Module fabs function
Average of threenumbers.
a = float(input())
b = float(input())
c = float(input())
sum = a + b + c
avg = sum/3
print(avg)
31.
Area of Triangleusing Base and Height.
b = float(input())
h = float(input())
area = (1/2) * b * h
print(area)
32.
Area of Triangleusing Length of 3 sides.
import math
a = float(input())
b = float(input())
c = float(input())
s = (a+b+c) / 2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print(area)
𝑎𝑟𝑒𝑎 = 𝑠 ∗ (s−a)∗(s−b)∗(s−c)
𝑠 =
𝑎 + 𝑏 + 𝑐
2
33.
Area of Circleusing Radius.
import math
r = float(input())
pi = math.pi
area = pi * r**2
# area = pi * pow(r,2)
print(area)
𝑎𝑟𝑒𝑎 = 3.1416 ∗ 𝑟2
Practice Problem 1.3
1.Average of three numbers.
2. Area of Triangle using Base and Height.
3. Area of Triangle using Length of 3 sides.
4. Area of Circle using Radius.
5. Convert Temperature Celsius to Fahrenheit.
6. Convert Temperature Fahrenheit to Celsius.
7. Convert Second to HH:MM:SS.