My journey through learning Python : Day 6 to 8
Day 6: Python Functions & while
loop
This day was really fun! We had to play with a little robot 🤖 named Reeborg. Besides learning two interesting concepts, I mostly played with logic and algorithm. I found this way of learning extra entertaining!
Functions
We already been playing with functions in Python. Yes! The print()
statement? A function! The range()
statement? You know it: a function! But, what if we wanted to make our own functions? Is it possible? YES! That's what this day is partly about.
Creating our own functions
Firstly, what is a function?
A function is a block of reusable code that performs a specific task. Functions allow you to break down your program into smaller, more manageable chunks of code, making your code more modular, readable, and easier to maintain.
A function in Python is defined using the def
keyword, followed by the function name, a set of parentheses containing zero or more parameters (also called arguments), and a colon. The function body is then indented and contains the code that executes when the function is called.
Here's a simple example of a Python function that takes two arguments and returns their sum:
def add_numbers(a, b):
sum = a + b
return sum
To call this function and pass in two numbers, you would write:
result = add_numbers(5, 10)
print(result)
# ↪️ Output:
#
# 15
Functions can have optional parameters, default values, and even be recursive. They are an essential part of writing clean and organized Python code.
But, we will see it later on the course. I know that you are impatient, but bear with me. 🏆
Indentation
A little word about the role and the importance of the indentation in Python.
Python is a high-level programming language and one of the essential features of this language that sets it apart from other programming languages is its use of indentation to define code blocks.
But, what is it?
Indentation refers to the use of white spaces at the beginning of a line to signify the start of a new code block.
Why do we use it?
In Python, indentation plays a vital role in the syntax of the language. It is used to denote the start and end of code blocks such as loops, functions, classes, and conditional statements. The use of indentation instead of curly braces, as in many other programming languages, makes Python code more readable and visually appealing. It also helps to avoid errors caused by missing or mismatched braces.
Python uses a strict indentation scheme, where each line in a code block must be indented by the same number of spaces or tabs. This ensures that the code is easy to read and understand, even for someone who is new to the language.
Is it really that important? Yes, for all of these reasons:
- Readability: Indentation makes the code more readable by clearly indicating the structure of the code. This helps to avoid errors and makes it easier for others to read and understand the code.
- Consistency: Python enforces a strict indentation scheme that ensures that the code is consistent and easy to follow. This helps to avoid errors and ensures that the code is maintainable.
- Debugging: Proper indentation can also help to identify errors in the code. If the code is not indented correctly, it can be difficult to identify the location of an error.
- Best Practices: Indentation is considered a best practice in Python programming. Most Python developers use indentation to structure their code, which makes it easier for others to read and maintain the code.
- Maintainability: Indentation also plays an essential role in the maintainability of the code. Proper indentation makes it easier for developers to modify and enhance the code without introducing new errors.
Here are an example of how indentation is used in Python:
def multiply(x, y):
••••result = x * y
••••return result
Notice the four •
in the code, I use it to emphase the <SPACE>
key I pressed when typing it.
while
loops
Just like for
loops, in Python, a while
loop is a control flow statement that allows you to repeatedly execute a block of code as long as a certain condition remains true. The syntax of a while loop in Python is simple:
while condition:
# code to be executed while condition is true
The condition is an expression that is evaluated at the beginning of each iteration of the loop. If the condition is true, the code inside the loop is executed. After the code is executed, the condition is checked again, and if it is still true, the loop executes again. This process continues until the condition becomes false.
One important thing to keep in mind while using a while loop is to make sure that the condition will eventually become false, otherwise, the loop will run indefinitely, resulting in an infinite loop. To avoid this, you should make sure that the condition is updated within the loop.
Here is an example of a while loop in Python that prints the numbers from 1 to 10:
i = 1
while i <= 10:
print(i)
i += 1
In this example, the variable i
is initialized to 1
, and the loop continues to execute as long as i
is less than or equal to 10
. Inside the loop, the current value of i
is printed, and then i
is incremented by 1
. This process continues until i
becomes greater than 10
, at which point the condition becomes false, and the loop terminates.
Day 7: Hangman
It was a really great day where I learned a lot, not in terms of programming concepts, because we used what we already learned. But in terms of how to construct and think a whole project. Yes, it was a small project, I know. But still!
It was really fun and I was able to demonstrate my program to my family and friends. I was really proud of the result.
So, not so much to talk about new things I have learned. But still, I took a lot of confidence in my programming skills. Indeed, I didn't have to look the solutions, I did it all by myself. So pretty satisfied with myself and it's really important to take confidence on yourself. Because, there will be some days when I'll doubt about my skills. And I'll just have to think about my ease to take this challenge to regain trust in myself. 🤓
Day 8: Function Parameters & Caesar Cipher
Function Parameters
Today, I learned about how to give some input inside a function to modify the behaviour of my function according to the input. This input is called a parameter.
Here is an example of a really silly function that takes the variable name
as a parameter:
def greet(name):
print(f'Hello, {name}')
print(f'How do you do, {name}?')
print('Isn\'t the weather nice today?')
greet('Tiffanie')
# ↪️ Output:
#
# Hello, Tiffanie
# How do you do, Tiffanie
# Isn't the weather nice today?
Difference between a parameter and an argument in Python
- A parameter is the name of the input when we declare a function, it's the name of the data that's being passed in.
- An argument is the name of the input when we call a function, it's the actual value of the data.
Here are the terms used in a function:
def my_function(parameter):
# Do something with _parameter_
my_function(argument)
Positional Arguments
A positional argument is an argument that is passed to a function or method based on its position or order in the argument list.
When you define a function or method, you can specify its parameters by using the parameter name. For example:
def multiply(x, y):
return x * y
Here, x
and y
are a required positional argument. If you call the function with the arguments 2
and 3
, like multiply(2, 3)
, then the value of x
will be 2
, and the value of y
will be 3
.
Keyword Arguments
A keyword argument is an argument that is passed to a function or method with a specific parameter name, as opposed to its position or order in the argument list.
When you define a function or method, you can specify its parameters by using the parameter name. For example:
def multiply(x, y):
return x * y
So, the definition of the function is the same as with Positional Arguments. The difference is when we call this function :
result = multiply(x=2, y=3)
# is the same as:
result = multiply(y=3, x=2)
Here, x=2
and y=3
are keyword arguments. They are passed to the multiply function by specifying their parameter names, rather than their position in the argument list.
Keyword arguments are useful when you want to pass values to a function or method in a specific order that might not match the order of the parameters in the function definition. They also allow you to omit optional parameters by not passing them at all, and just using their default values.
Caesar Cipher
Yeah, another real program! Just like the Hangman problem, this one was really interesting. We had to refactor our code, we had to use the %
modulo operator. It was a great and complete project. I had so much fun programming the solution!
My solution of the course problems
I forget to mention in my previous article: if you want to take a look on the exercises and projects we are doing during this course, you can check my gitlab repository right here. I push MY solution of the problem. Please, be aware that it is my solution, at a certain time, when I didn't learnt everything about Python, yet. 😉
And that's a wrap for today. I'm so excited to see you soon! 🐍