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))