Python Tutorial for absolute beginners #1

Code Blogging
4 min readJan 30, 2022

Introduction to variables and the print function

A variable in Python works similar to how they work in math. But you can not only assign a single number to it. You can assign a number, word, list etc. to it.

The print function is way you can show anything you wrote inside the print function. This could be a word, a number or even a set of code.

The print() function

If you want to print a word (with the print() function) in Python you need to put it into quotes or duble quotes. If you want to print a number you can write it inside the parenthesis without the quotes or double quotes. Next you find some examples with the print() function.

print('Hello World!')
Output: Hello World!
print(7)
Output: 7
print('banana')
print('25')
Output: banana
25

So let me explain what I just did. The first line of code tells the program it should print the word inside the parenthesis . So the output of the first code is Hello World!.
The second code says the program to print the number 7. Because it is a number we do not have to use quotes.
The third code gives the program two separate lines to print. The program also prints these on two lines.

Variables

With the equal sign you can assign a word/number to its variable. Let me show you how that looks.

a = 3print(a)
Output: 3

By writing that code we can tell the program to assign the number 3 to the letter a. Now every time we use the letter a (without quotes) the program realises it has to take the number 3 instead of the letter a. The same works for the print function. If we write print(a) (without quotes) the program prints the number which is assigned to the variable.

The same does not only work for numbers but also for words. The features are the same, but you will have to put the word in quotes. This would look something like this.

b = 'apple'print(b)
Output: apple

Now the variable does not have to be a letter it could also be a word or some random letters. But it could not be a sign or number.

height = 1.76print(height)
Output: 1.76

If you might want to assign a number and a word to a variable, you have to put both into the same quotes.

adress = '23 Standford street'print(adress)
Output: 23 Standford street

It is also possible to assign a variable to an already existing variable. For that you first have to create the first variable and assign it to something. After that you can assign the second variable to the first variable. The second variable is now assigned to the value the first variable is assigned. This value stays the same even if we reassign the value of the first variable.

a = 10
b = a
print(b)
Output: 10

Swapping two variables

If you want to swap two already existing variables you need to create a third, temporary variable (I will call it temp in this example). Let me show you the code first, after that I will explain how it works.

x = 1
y = 2

temp = x
x = y
y = temp

print(x)
Output: 2
print(y)
Output: 1

First, we assign the value 1 to the variable x and the value 2 to the variable y. As already said before, we now want to swap these two variables, so x is equal to 2 and y is equal to 1.
For that we create a new variable “temp” which is equal to the value of x (1). So now we have a third variable named temp which is equal to 1. Next, we assign x to the value of y (which is 2). And as already stated before the value of the variable temp does not change even if you switch the value of x.
After that we only have to assign the variable y to the value of temp (which is 1). If we now print x and y we see, that the two variables have changed their values.

That’s it for today’s tutorial. I hope you could enjoy it and learn something new. If you are interested in the following tutorials for beginners, be sure to leave a follow on my profile and subscribe to my e-mail subscription so you don’t miss anything new. Thank you very much.

--

--

Code Blogging

Are you interested in learning Python? Perfect, I have a series for absolute beginners to learn Python and have your first coding experiences.