DEV Community

Ranjith Jr
Ranjith Jr

Posted on

4.Operators & Conditionals.py

Python Operators

Operators are used to perform operations on variables and values.

Python language supports various types of operators, which are:
Arithmetic Operators.
Comparison (Relational) Operators.
Assignment Operators.
Logical Operators.
Bitwise Operators.
Membership Operators.
Identity Operators.

*Add Two Numbers '+' *

operand or the values to be added can be integer values or floating-point values. The '+' operator adds the values

Image description

Subtraction '-' ** in Python is as simple as it is in basic arithmetic. To subtract two numbers, **we use the '-' operator.

Image description

*multiply '' **
In Python, the most straightforward way to multiply two numbers is by using the * operator. This operator works with integers, floats,

Image description
Division
1 Integer division is represented by "//" and returns the quotient rounded down to the nearest integer.
2 Floating division is represented by "/" and returns the quotient as a floating-point number.
3Integer division is useful when dealing with quantities that cannot be represented as fractions or decimals.
4.Floating division is useful when dealing with quantities that are not integer values.
5 / 2 #output : 2.5
5 // 2 #output : 2

. *How to get the input from the user *?

In Python, we use the input() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “enter,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.

Image description
]

input data type & type casting

`# Addition

num1 = float(input("Enter Number 1")
num2 = float(input("Enter Number 2 : ")

result = num1 + num2

print("result is", result)

Enter Number 1: 2

Enter Number 2: 4

result is 6.0`

`# subtract

num1 = float(input("Enter Number 1: ")

num2 = float(input("Enter Number 2: ")

result = num1 num2

print("result is", result)

Enter Number 1: 109

Enter Number 2 23

result is 2507.0`

`# division

num1= float(input("Enter Number 1")

num2 = float(input("Enter Number 2")

result = num1 % num2

print("result is", result)

Enter Number 1: 5

Enter Number 2: 2

result is 1.0`

`# remainder

num1 = float(input("Enter Number 1 : "))
num2 = float(input("Enter Number 2 : "))

result = num1 % num2

print("result is ", result)`

2 **3# 2 pow 3
8

If condition:
`The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.

`Elif' stands for 'else if' and is used in Python programming to test multiple conditions. It is written following an if statement in Python to check an alternative condition if the first condition is false. The code block under the elif statement will be executed only if its condition is true.

`# Trip to tirunelveli

if BUS_available:
go by bus
elif Train_aviable:
go by train
else
go by bike

if BUS_available:
go by bus
if Train_aviable:
go by train #
if bike avaialble
go by bike`

Let's create a calculator.

Show list of functionalities available
Get user inputs
based on user input, perform functionality.

print("Simple Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiate")

choice = input("Enter choice(1/2/3/4/5/6): ")
num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))

"2"

if choice == "1": # no : 1 unit of work.
result = num1 + num2
print(result)
if choice == "2": # yes: 1 unit of work
result = num1 - num2
print(result)

if choice == "3": # no : 1 unit of work.

result = num1 * num2

print(result)

if choice == "4": # no : 1 unit of work.

result = num1 / num2

print(result)

if choice == "5": # no : 1 unit of work.

result = num1 ** num2

print(result)

"2" elif = else if.

elif choice == "2":
result = num1 - num2
print(result)

print("Simple Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiate")

choice = input("Enter choice(1/2/3/4/5/6): ")
num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))

"2"

if choice == "1": # no : 1 unit of work.
result = num1 + num2
print(result)
elif choice == "2":
result = num1 - num2
print(result)
elif choice == "3":
result = num1 * num2
print(result)
elif choice == "4":
result = num1 / num2
print(result)
elif choice == "5":
result = num1 % num2
print(result)
elif choice == "6":
result = num1 ** num2
print(result)
else:
print("option not available")
total_times = 2

while (condition) # True

# False -> exit.
Enter fullscreen mode Exit fullscreen mode

2 > 0 = True

1 > 0 = True

0 > 0 = False

while total_times > 0:
# 2 - 1 = 1
# 1 - 1 = 0
total_times = total_times - 1
print("Simple Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiate")
print("7. Close Calculator")

choice = input("Enter choice(1/2/3/4/5/6): ")
num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))

#     "2"
if choice == "1": # no : 1 unit of work.
    result = num1 + num2
    print(result)
elif choice == "2":
    result = num1 - num2
    print(result)
elif choice == "3":
    result = num1 * num2
    print(result)
elif choice == "4":
    result = num1 / num2
    print(result)
elif choice == "5":
    result = num1 % num2
    print(result)
elif choice == "6":
    result = num1 ** num2
    print(result)
elif choice == "7":
    break
else:
    print("option not available")
    break
Enter fullscreen mode Exit fullscreen mode

def calculator():
print("Simple Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiate")

choice = input("Enter choice(1/2/3/4/5/6): ")
num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))

if choice == '1':
    print(f"Result: {add(num1, num2)}")
elif choice == '2':       
Enter fullscreen mode Exit fullscreen mode

print(f"Result: {subtract(num1, num2)}")
elif choice == '3':
print(f"Result: {multiply(num1, num2)}")
elif choice == '4':
print(f"Result: {divide(num1, num2)}")
elif choice == '5':
print(f"Result: {remainder(num1, num2)}")
elif choice == '6':
print(f"Result: {power(num1, num2)}")

Simple Calculator
Select operation:

  1. Add
  2. Subtract
  3. Multiply
  4. Divide
  5. Modulus
  6. Exponentiate Enter choice(1/2/3/4/5/6): 1 Enter first number:2 Enter second number:3 Result: 5.0

Top comments (0)