Variables and Functions

Variables and Functions Titelbild


Variable

You can imagine a variable like a box in which you can store anything you want. You can choose the name of the box and also choose what you want to put into the box. This, for example, is an easy way to save the nickname of your math teacher:

nameTeacher = “Dr. Franken Number”

Or the number of weeks in since you opened the last bottle of milk:

openMilkBottle = 142

Though this may sound at first like boring math stuff, it definitely isn’t! Variables are really really cool and enable us to create amazing programs while saving time for us by eliminating the need to type and retype frequently used values. It is important to note that we can use a variable at any point in a program after we have created it.

So open your hello world program once again:

nano hello-world.py

And change the line of codes to this:

myName = "Octopus"
print ("Hello " + myName)

You can also replace octopus with your own name when defining the variable myName to create a personal touch for your program. After saving and executing you will see the text “Hello Octopus” (or the same with your name). In the first line you are creating a variable with the name myName and then assigning a value to the variable with the = sign. In the second line you are printing the word “Hello” and with the + you are adding the content of the variable to the “Hello” message. With the + you can combine two different texts in Python. Now it is your turn! Can you write a program write a program that prints your name and your age?

myName = "Octopus"
myAge = 2

print(" My Name is " + myName)
print(" And I’m " + str(myAge)+ " years old")

Until now you only saved text in a Variable. In Python (and a lot of other programming languages) this is called a string. When we are talking about how old someone is, we normally use a number, and just as such, in this case we also want to save the age as a number. It is similar to saving a string except we don’t need any “” to assign the value to the variable. These numbers (not point numbers) are also called integers. In line 4 and 5 we can’t simply mix strings (text) and integer (numbers) so we first need to convert the integer myAge into a string with the function str(). We can also do this another way by using the function int(). For this function we also need a string with a number. We use two different print() functions in our code to keep the program more organized and readable.

Now we have a small Python Tip for you. Sometimes it can be hard to remember what you meant with a certain line of code or why a particular function is really necessary. For this you can use a comment. A comment is line of code that is just for the person who is viewing or writing the code. Python will ignore the comment and it will not interact with the rest of your program. A comment is any line of code that starts with a #.

Functions

A car, a solar panel, a computer. These are all useful inventions and are helping us in our day to day life. We can simply use them without thinking too much about how they are working behind the scenes or how they are built. I mean we probably could explain how a motor is working. With the cylinders and the 4 strokes and so on. But a semiconductor plate which gets hit by photons emitted by a huge burning gas globe (also called the sun) and thus producing energy is a bit out of our league.

This is the principle behind the purpose of functions in programs. Someone a long long time ago created a solution for a certain problem in a program. This person shared the solution and now we and other programmers can use it.

Just as we can use a solar panel for energy production without having the knowledge how this solar panels works, we can use functions in our programming without knowing every detail about how the function works. The basic information behind functions is that they always have a name for the function, and an additional variable(or several) that we can pass onto the function. These variables are also called parameters. We have prepared an example with the print()- function, which we have already used in this book.

print("Hello World")

print() is the name of the function. Between the parentheses() we write the parameters. With the parameters we can pass information to the function. We use the print() function to display information in the Terminal. For this to work, the function does not only need the command to print something in the terminal, but also the knowledge of what the text or information is that should get printed. There are also other functions which we will use. For example, this imaginary function that can calculate the volume of a cake:

cakeVolume()

The name of the function is cakeVolume and between the parentheses() we need to enter the parameters. To calculate the volume of a piece of chocolate cake, we need the height, the depth, and the width of the cake. The taste or other information of the cake is not necessary for this. So for our function we pass the following:

cakeVolume(5, 8, 4)

It looks the same like as time, except this time we have 3 different parameters between the parentheses. The parameters are separated from each other by a comma. The function would normally take all the parameters and then use it for its purpose. We don’t need to know any more about the function and how it works. This makes our lives so much easier because there are a lot functions for common problems that we can use. You will get to know more functions and their uses in the rest of the book.

Noch Fragen oder Feedback?

Bevor du eine Frage stellen kannst musst du dich zuerst Anmelden oder Regestrieren!