0% found this document useful (0 votes)
6 views5 pages

Function Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Function Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python User Input

Python allows for user input.


That means we are able to ask the user for input.
The following example asks for your name, and when you enter a
name, it gets printed on the screen:

Example

print("Enter your name:")


name = input()
print("hello",name)

Multiple Inputs
name = input("Enter your name:")
print("Hello",name)
fav1 = input("What is your favorite animal:")
fav2 = input("What is your favorite color:")
fav3 = input("What is your favorite number:")
print()
print(fav1 ,fav2 ,fav3)
Python Functions
A function is a block of code which only runs
when it is called.
You can pass data, known as parameters, into
a function.
A function can return data as a result.
Creating a Function
In Python a function is defined using
the def keyword:
Example
def my_function():
print("Hello from a function")
print("Hello from a function")

Calling a Function
To call a function, use the function name
followed by parenthesis:
Example
def my_function():
print("Hello from a function")

my_function()
Arguments
Information can be passed into functions as
arguments.
Example
def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")
def my_function(fname, lname):
print(fname + " " + lname)

my_function("Emil", "Refsnes")

Return Values
To let a function return a value, use
the return statement:

def my_function(x):
return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))

You might also like