Log in to save your progress.
<< Previous
Home
Next >>
OBS! Fönstret är för smalt för att använda pythonlabbet.se. Använd en enhet med tangentbord!

Reference

Click on a function to read more.
Basics part 1
Syntax
Short description
break
break interrupts code that is repeated
continue
continue interrupts a repetition and starts the next
elif
elif is a combination of else and if
else
else can be used last in an if-statement
float()
Converts to type float (decimal number)
if
if controls the code to do different things depending on a condition
input()
Receives input from the user
int()
Converts to type int (integer)
randint()
Generates a random integer
round()
Rounds a number
str()
Converts to type string
while
while is used to repeat code

Variables and Data Types

In this section you will learn

Variables - storing data

A variable is for storing data and is used everywhere in programming. Often strings or numbers are stored, but it can also be other more complicated data structures and objects.

A variable is for remembering something while the program is running, for example, the result of a calculation so that it can then be reused elsewhere in the program. It is also common to use variables to store user input or data that has been read from a file.

Variables are used everywhere in programming. The name variable comes from the fact that the program can change (vary) the content of a variable during the program's execution. What is placed in a variable is stored in the computer's working memory and disappears when the program is no longer running.

To create a variable, = (equals sign) is used; to the left of the equals sign is the variable name and to the right of the equals sign is the value to be stored in the variable. It is important here to understand the difference between = in mathematics (is equal to) and in Python (assign value to variable). It is fine to use print(my_variable) to print the content of the variable.

Example

The string Hello World is stored in the variable named my_variable.

my_variable = 'Hello World'

It is important to write good variable names. The name should describe the information stored in the variable. With good variable names, the program is easier to understand and it becomes easier to do it right. It takes a long time to learn to write good variable names, so don't despair if you find it difficult! In Python, the convention is to write variables with lowercase letters and with _ to separate words.

Login or create an account to save your progress and your code.

What does the code do?

Read the code below and try to figure out what the program prints. Run the program after you answer and see if you were correct.

-- Output from your program will be here --

Question: What will the program print?

FirstSecond
First Second

Data Types

Computers must distinguish between different types of variables, such as strings (text) and numbers. This is because they are stored in different ways in the computer's memory. For example, the number 1 and the string '1' are represented in different ways in memory.

We have already used three common variable types: strings (eng: string), integers (eng: integer) and floating-point numbers (eng: float). Floating-point numbers (pronounced float-numbers) are the most common way for a computer to represent numbers with decimal points.

When you create a variable, Python figures out what type the variable is. In many programming languages, you have to tell what type a variable is when you create it, and then you cannot change what type the variable is. In Python, for example, you can first store a string in a variable and then later store a number in the same variable.

The built-in function type can be used to see what type a variable has, see the example below.

Example

First, three different variables are created with the types string, integer, and float. Then, three more variables are created that contain the type of the first three variables.

my_text = 'Hi there' #string
my_favorite_number = 1337 #integer
pi = 3.14 #float

my_text_type = type(my_text)
my_favorite_number_type = type(my_favorite_number)
pi_typ = type(pi)

Conversion between different types

The helper functions str, int, and float are available to convert the three types above between each other. For example, int is used to convert a variable to the integer type. Note that it does not always work; for example, the string 'hello' cannot be converted to either an integer or a float.

Example

Some common type conversions.

integer_number = int('42') #the string 42 becomes the integer 42
float_number = float(10) #the integer 10 becomes the float 10.0
text = str(3.14) #the float 3.14 becomes the string 3.14
  

To create a new string that uses, for example, an integer variable, we can perform a conversion.

Example

Concatenating a string with an integer.

x = 12
x_text = 'The value of x is equal to ' + str(x)
  

In the case above, when we want to create a string with variables inside, there is an alternative called f-strings.

Example

Create a string with an integer inside

x = 12
x_text = f'The value of x is equal to {x}'
  

By writing an f before the string, we can use {} around a variable as in the example above. Then the conversion to a string happens automatically.

Login or create an account to save your progress and your code.

Find the issue

The code below contains an error. Run the program and see what the error message is. Then fix the code and run the program again.

-- Output from your program will be here --

The built-in function print can receive different types in its argument (what is inside the parentheses of a function is called an argument). The print function automatically converts the argument to a string. If you write, for example, print(5), the number 5 is automatically converted to a string.

Rounding

If a float is converted to an integer using int, the decimals are simply removed.

Example

Float to integer with int.

x = int(3.99)  #x = 3
  

With the built-in function round, we can instead round a float to an integer.

Example

Float to integer with round.

x = round(3.99) #x = 4
  

With the help of round, it is also possible to round to any number of decimals. Inside the parenthesis, we first write the number to be rounded, followed by a comma , and then how many decimals we want to round to.

Example

Round to two and four decimals.

x = round(1.23456, 2)  #x = 1.23
y = round(1.23456, 4)  #y = 1.2346
  

Login or create an account to save your progress and your code.

Change the code

The code below prints pi with many decimals. Change it so that pi is printed rounded to four decimals.

-- Output from your program will be here --
Status
You have finished all tasks in this section!
🎉