Home > Backend Development > Python Tutorial > Python Functions 4

Python Functions 4

WBOY
Release: 2024-07-22 08:27:49
Original
1012 people have browsed it

Python Functions 4

Functions

repeated step can be executed with functions

Funtion Structure:

def name(argument):
Statement..
Statement..

return value

def-define function
def functionname(contains a list of values passed to the function)
function body must be indented
this is executed each time the function is called
return values

Function has bulid-in function and user-defined functions

Eg-Calculator prgm
num1=float(input('Enter value for num1 '))
num2=float(input('Enter value for num2 '))
choice=input('Operation to be done:add/sub/mul/div/mod ')

def add(num1,num2):
add=num1+num2
print (add)

def sub(num1,num2):
sub=num1-num2
print(sub)

def mul(num1,num2):
mul=num1*num2
print(mul)

def div(num1,num2):
div=num1//num2
print(div)

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

elif choice=='sub':
sub=num1-num2
print(sub)

elif choice=='mul':
mul=num1*num2
print(mul)

elif choice=='div':
div=num1//num2
print(div)

else:
print('option not available')

The above is the detailed content of Python Functions 4. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template