You are on page 1of 2

36% off Learn to code by doing. Try hands-on Python with Programiz PRO.

Claim Discount Now

Courses Tut orials Examples Search tutorials & examples Try Programiz PRO

Python Examples

Display Powers of 2 Using


Anonymous Function

Find Numbers Divisible by Another


Number
Python Program to Make a Simple
Convert Decimal to Binary, Octal
and Hexadecimal
Calculator
Find ASCII Value of Character To understand this example, you should have the knowledge of the
Find HCF or GCD following Python programming topics:
Find LCM
Python Operators
Find the Factors of a Number
Python Functions
Make a Simple Calculator
Python Function Arguments

Python User-defined Functions

Example: Simple Calculator by Using Functions

# This function adds two numbers


def add(x, y):
return x + y
Python Tutorials
# This function subtracts two numbers
def subtract(x, y):
Python User-def ined Functions
return x - y
Python Looping Techniques
# This function multiplies two numbers
Python Numbers, Type Conversion def multiply(x, y):
and Mathematics return x * y

Python Basic Input and Output # This function divides two numbers
def divide(x, y):
Python input()
return x / y
Python map() Function

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

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

# check if choice is one of the four options


if choice in ('1' '2' '3' '4'):

Run Code

Output

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Your
Enter privacy choices
choice(1/2/3/4): 3
Enter first number: 15
We and 816 TCF partners and other partners process personal data such as
Enter second number: 14
cookies, unique identifiers, and other device data. Some partners may process your
15.0 * 14.0 = 210.0
personal data on the basis of legitimate interest. View a list of partners to see the
Let's do next calculation? (yes/no): no
purposes they believe they have legitimate interest or and how you can object to it.
We and 816 TCF partners and other partners may use your data for the listed
purposes, you can manage
In thisyour choices bywe
program, askManage
clicking Options.
the user You can an operation.
to choose Options 1, 2,
change your settings at any time, including by withdrawing your consent, by clicking
the cog icon at corner3,
or and 4 at
are valid. If any
page.other input is given,
the link bottom of the
Invalid Input is displayed
and the loop continues until a valid option is selected.
Purposes
Two numbers are taken and an if...elif...else branching is used to
Store and/or access information on a device
execute a particular section. User-defined functions add() ,
subtract()
Develop and improve services , multiply() and divide() evaluate respective operations
and display the output.
Personalised advertising and content, advertising and content measurement, and
audience research

Special Features

Use precise geolocation


AlsodataRead:

Manage Options
Python if else Agree and proceed

Python while loop


Powered by UniConsent

Did you f ind this article helpf ul?


Share on:

Related Examples

Python Example Python Example Python Example Python Tutorial

Find the Largest Add Two Numbers Find LCM Python User-defined
Among Three Functions
Numbers

Tutorials Examples Company

Pyt hon 3 Tut orial Pyt hon Examples Change Consent


Join our newsle er for the latest
JavaScript JavaScript About
updates.
Tut orial Examples
Advert ising

Enter Email Address Join SQL Tut orial C Examples


Privacy Policy
HTML Tut orial Java Examples
Terms &
R Tut orial Kot lin Examples Condit ions

C Tut orial C++ Examples Cont act

C++ Tut orial Blog

Java Tut orial Careers

Rust Tut orial Yout ube

Go Tut orial

Kot lin Tut orial

Swi Tut orial

C# Tut orial

DSA Tut orial

Apps

Learn Pyt hon


Learn C
36% off Learn to code by doing. Try hands-on Python with Programiz PRO. Claim Discount Now
Programming

Learn Java Courses Tut orials Examples Search tutorials & examples Try Programiz PRO

© Parewa Labs Pvt . Lt d. All right s reserved.

Your privacy choices


We and 816 TCF partners and other partners process personal data such as
cookies, unique identifiers, and other device data. Some partners may process your
personal data on the basis of legitimate interest. View a list of partners to see the
purposes they believe they have legitimate interest or and how you can object to it.
We and 816 TCF partners and other partners may use your data for the listed
purposes, you can manage your choices by clicking Manage Options. You can
change your settings at any time, including by withdrawing your consent, by clicking
the cog icon at corner or the link at bottom of the page.

Purposes

Store and/or access information on a device

Develop and improve services

Personalised advertising and content, advertising and content measurement, and


audience research

Special Features

Use precise geolocation data

Manage Options Agree and proceed

Powered by UniConsent

You might also like