0% found this document useful (0 votes)
138 views

Calculator in Python PPT LAB

Uploaded by

bunny4693827
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Calculator in Python PPT LAB

Uploaded by

bunny4693827
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Make Calculator in Python

By:
G PANDU
247R5A6603
CSM-A
Introduction
• In this presentation, we will build a basic calculator in
Python.

• Topics covered:
• - Setting up the calculator functions
• - Performing basic operations (addition, subtraction, etc.)
• - Implementing the calculator logic
• - Optional: Creating a simple GUI using tkinter

• Let's dive into the code!


Step 1: Addition Function
• Define a function for addition:

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

• This function takes two arguments and returns


their sum.
Step 2: Subtraction Function
• Define a function for subtraction:

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

• This function takes two arguments and returns the


result of subtracting y from x.
Step 3: Multiplication Function
• Define a function for multiplication:

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

• This function takes two arguments and returns


their product.
Step 4: Division Function
• Define a function for division:

```python
def divide(x, y):
if y == 0:
return 'Error: Division by zero'
return x / y
```

• This function checks if the divisor is zero to avoid errors.


Step 5: Calculator Logic
• Define the calculator logic using conditional statements:
```python
def calculator():
print('Select operation:')
print('1. Add')
print('2. Subtract')
print('3. Multiply')
print('4. Divide')
choice = input('Enter choice (1/2/3/4): ')
if choice in ['1', '2', '3', '4']:
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
# Call corresponding functions
else:
print('Invalid Input')
```
Optional: GUI Using Tkinter
• For a graphical interface, we can use tkinter:

```python
import tkinter as tk
root = tk.Tk()
root.title('Calculator')
# Code to add buttons and input fields
root.mainloop()
```

• This code creates a simple window for the calculator.


Conclusion:
• In this presentation, we covered how to:
• - Implement basic operations in Python
• - Build a simple calculator using functions
• - Optionally create a GUI for the calculator

You might also like