Featured

HOW TO MAKE CALCULATOR USING PYTHON



 

Python is a high-level, interpreted programming language. It is widely used for various applications such as web development, scientific computing, data analysis, artificial intelligence, and more. It is known for its clear and concise syntax, making it easy to learn and use. Python also has a large and active community that creates and maintains many libraries and modules, allowing for efficient and fast development of complex applications.

 

 

 

How to code simple calculator in PYTHON:

 

 

def add(x, y):
    return x + y

def subtract(x, y):
    return x -
y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

print("Select operation.")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

# Take input from the user
choice = input("Enter choice(1/2/3/4): ")

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
    print(num1, "+", num2, "=", add(num1,num2))

elif choice == '2':
    print(num1, "-", num2, "=", subtract(num1,num2))

elif choice == '3':
    print(num1, "*", num2, "=", multiply(num1,num2))

elif choice == '4':
    print(num1, "/", num2, "=", divide(num1,num2))
else:
    print("Invalid input")

 

This code implements a basic calculator in Python. The four functions add, subtract, multiply, and divide perform basic arithmetic operations of adding, subtracting, multiplying, and dividing two numbers, respectively.

The user is presented with a menu of four operations, and the user's choice is taken as input. Then, the user is prompted to enter two numbers, num1 and num2.

Based on the user's choice, the corresponding arithmetic operation is performed on the two numbers and the result is printed. If the user's choice is not among the four operations, the code prints an "Invalid input" message.

 

HOW IF, ELSE AND ELIF WORKS? :

if and else are control statements in Python used to conditionally execute code.

The if statement is used to check if a certain condition is met, and if it is, the code inside the if block is executed. The syntax for an if statement is:

 



 

 

 if condition:
    # Code to be executed if condition is True

The else statement is used in conjunction with if and it's executed when the if condition is not met. The syntax for an else statement is:


 

 

 

 

if condition:
    # Code to be executed if condition is True
else:
    # Code to be executed if condition is False

 

elif is a shorthand for "else if". It's used in conjunction with if and else to check multiple conditions.

When multiple conditions need to be checked, you can use elif to provide additional conditions to test. If the if condition is not met, the code checks the next elif condition, and so on. If none of the conditions are met, the code inside the else block is executed.

 

The syntax for elif is:

 


 

 

 

 

if condition1:
    # Code to be executed if condition1 is True
elif condition2:
    # Code to be executed if condition1 is False and condition2 is True
...
else:
    # Code to be executed if all conditions are False
 

 

For example, consider the following code:






x = 5

if x < 0:
    print("x is negative")
elif x == 0:
    print("x is zero")
else:
    print("x is positive")

In this example, x is 5, which is greater than 0, so the code inside the if block is executed, and the output is x is positive.

 

HAPPY CODING!

Comments

Popular Posts