Log in to save your progress.
<< Previous
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

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
Basics part 2
Syntax
Short description
abs
Absolute value
append
Adds an element to a list
def
Defines a function
for
for is used to repeat code
len
Returns the number of elements in an object
max
Returns the largest element
min
Returns the smallest element
pop
Removes an element from a list
return
Returns data from a function
sum
Sums the elements in an object

More about print and other things

In this section you will learn about

Multiple arguments

By writing multiple arguments to print we can print several things at once. For example, if we want to print the variables x and y, we can write print(x, y). They will then appear on the same line with a space between them. We have of course been able to do this before with print(str(x) + ' ' + str(y)) but you will surely agree that this is easier to write.

Example

Printing several things at once with multiple arguments to print.

print('One', 'Two')
print('One', 'Two', 'Three')

End with something other than a new line

So far, each print has resulted in a new line in the output. This is the default setting for print, but it can be changed with an argument named end. By default, end='\n' where \n is the new line character. Most commonly, end='' (nothing) or end=' ' (space) is used if we want to change the default behavior. This argument must be at the end of the arguments.

Example

Using the argument end when printing in a loop.

sequence = [0, 1, 1, 2, 3, 5]
for number in sequence:
    print(number, end=' ')

Separate with something other than space

As we have seen, the outputs are separated by spaces when we use multiple arguments to print. We can change this using the argument sep.

Example

The argument sep is used to set what should separate outputs.

print(2021,12,24, sep='-')
print('One', 'Two', 'Three', sep=' / ')

Unpack with the * operator

Printing the sequence in the example above can be done without a loop with the operator *. It "unpacks" the content of the list and the elements become arguments to the function instead.

Example

Unpack the elements in the list with the * operator.

sequence = [0, 1, 1, 2, 3, 5]
print(*sequence)
print(*sequence, sep='->') #sep and end can still be used

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?

Test 1 2 3.
End
Test, 1, 2, 3.
End
Test, 1, 2, 3.End
Test 1 2 3.End

Those were some good things to know about print. Now we will move on to other details in Python that can simplify life and are good to know.

Strings can be written with ' or "

At Pythonlab.dev, ' is used for strings, mostly because it only requires one keyboard press with a Swedish keyboard. In Python, however, it does not matter whether single or double quotes are used for strings.

Example

Two different ways to write a string.

a = 'A string'
#is the same as:
b = "A string"

It is not possible to mix single and double quotes for the same string.

New operators

Several times we have written lines like i = i + 1 to add one to the value of i. It can be written abbreviated i += 1. The operator += simply adds a value without us having to write the variable name twice. In the table below you can see more examples of similar operators.

Operator Example Same as
+= i += 1 i = i + 1
−= i -= 1 i = i - 1
*= i *= 10 i = i * 10
/= i /= 2 i = i / 2
//= i //= 2 i = i // 2
%= i %= 2 i = i % 2
**= i **= 2 i = i ** 2

The strip and split functions on strings

Sometimes a string contains unwanted characters at the beginning and/or end. An example is when we receive text from the user, then it is easy for a space to be included. We can use the string's built-in function strip() to remove these.

Just as append() and pop() are functions for lists, functions like strip() are only for strings. Such functions are called with a dot after the variable name, like this string.strip(). The function does not change the string, to save the result we need to write string = string.strip().

Example

Use strip() to clear spaces at the beginning and end of a string. Click test to see how many characters the string s has before and after.

s = '   pYtHoNlAbBeT    '
s = s.strip()

strip() also removes characters like \n (new line) and \t (tab).

Another function that comes with strings is split(). By default, it splits a string into smaller parts separated by whitespace (spaces, newlines, tabs). The function returns a list of the components. An example is a sentence, split() splits the sentence into words.

Example

Splits a sentence into words with split()

sentence = 'Hope it is fun to learn Python'
words = sentence.split()
#words = ['Hope','it','is','fun','to','learn','Python']

Since strip() returns a string, it is possible to use split() directly after strip() like this: string.strip().split(). However, it is not possible to use the functions in reverse order because split() returns a list and it is not possible to use strip() on a list.

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?

strange_string
strange string
strange
string

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

Create

The program should receive a line of integers separated by spaces as the only input.
For example 1 2 3. Print the sum of the numbers.

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