100 Days Of Python - Day 6
Day 6
Python Functions
A function is a block of code that performs a specific task. It is a reusable piece of code that can be called from anywhere in your program.
Defining a function
To define a function, use the def
keyword followed by the function name and parentheses ()
.
1
2
def my_function():
print("Hello World!")
Calling a function
To call a function, use the function name followed by parentheses ()
.
1
2
3
4
def my_function():
print("Hello World!")
my_function()
Passing arguments to a function
You can pass arguments to a function by putting them inside the parentheses ()
.
1
2
def my_function(name):
print("Hello " + name + "!")
Returning values from a function
You can return values from a function using the return
keyword.
1
2
3
4
def my_function(name):
return "Hello " + name + "!"
print(my_function("World"))
Default arguments
You can set default values for arguments in a function by using the =
sign.
1
2
3
4
def my_function(name="World"):
return "Hello " + name + "!"
print(my_function())
Indentation
Python uses indentation to indicate blocks of code. The indentation level must be consistent throughout the program.
In python, it is recommended to use 4 spaces for indentation.
1
2
def my_function():
print("Hello World!")
- The following code shows how an if else statement is indented:
1
2
3
4
if True:
print("Hello World!")
else:
print("Goodbye World!")
The While Loop
The while loop is used to execute a block of code as long as a condition is true.
Syntax
1
2
while condition:
# code to be executed
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
i = 0
while i < 10:
print(i)
i += 1
print("Done!")
# Output:
# 0
# 1
# 2
# 3
# ...
# 9
# Done!