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
Example
while True:
print('You are nice!')
answer = input('Should I stop saying that? Write yes in that case.')
if answer == 'yes':
break
Explanation
break interrupts code that is repeated. For example when while is used.
continue
continue interrupts a repetition and starts the next
Example
i = 0
while i < 100:
if i % 2 == 0: # continue with the next repetition if i is divisible by 2
continue
print(i,'is an odd number')
Explanation
continue interrupts the execution in the current repetition/iteration and continues with the next repetition. Used in while-statements and for-statements.
elif
elif is a combination of else and if
Example #1
temp = int(input('What is the temperature?'))
if temp > 20:
print('Warm and nice!')
elif temp > 0: #if temp <= 20 and temp > 0
print('Not very cold, but not so warm either.')
Example #2
number = int(input('Enter an integer'))
if number == 0:
print('You entered the number 0.')
elif number > 0:
print('You entered a positive number.')
else:
print('You entered a negative number.')
Explanation
elif is only used after if. It is a combination of else and if, meaning ELSE (if the above is false) and IF (if the new condition is true). The condition for elif is only tested if the condition for if is false.
Syntax
elif condition:
condition
Required. Something that is True or False.
else
else can be used last in an if-statement
Example
age = int(input('How old are you?'))
if age >= 18:
print('You are an adult!')
else:
print('You are not an adult.')
Explanation
else is only used at the end of a block that starts with if, and the code under else is only executed if the condition for if is false.
float()
Converts to type float (decimal number)
Example
float('0.01') #converts the string '0.01' to 0.01
float(10) #converts the integer 10 to 10.0
Explanation
The function float() converts the argument to type float (decimal number). Usually, the argument is a string or an integer.
Syntax
float(number)
Argument
number
A number in the form of a string or an integer.
if
if controls the code to do different things depending on a condition
Example #1
number = 10
if number == 10:
print('The number is equal to 10.') #this will be printed
Example #2
number = int(input('Enter an integer'))
if number == 0:
print('You entered the number 0.')
elif number > 0:
print('You entered a positive number.')
else:
print('You entered a negative number.')
Explanation
if is used to control what the program does depending on a condition. If the condition is true, the code in the if-block is executed. To control
what happens if the condition is not true, elif and else can be used.
Syntax
if condition:
condition
Required. Something that is True or False.
input()
Receives input from the user
Example
answer = input('What is your name?') #The user's answer is saved in the variable answer
Explanation
The function input() allows the user to enter input into the program. It is possible to include text that describes what should be entered.
Syntax
input(prompt)
Argument
prompt
An optional text the user sees.
int()
Converts to type int (integer)
Example
int(42) #converts the string '42' to 42
int(3.94) #converts the decimal number 3.94 to 3
Explanation
The function int() converts the argument to type int (integer). Usually, the argument is a string or a decimal number.
Syntax
int(number)
Argument
number
A number in the form of a string or a decimal number.
print()
Prints text or the content of a variable to the screen
Example #1
print('Hello World') #prints "Hello World"
print(1+1) #prints the number 2
print(x) #prints the content of variable x
Example #2
print('Hello World', end='') #prints "Hello World" without a new line
Example #3
print('Hello','to','you') #Prints "Hello to you"
print('Hello','to','you',sep=' | ',end='!') #prints "Hello | to | you!" without a new line at the end.
Explanation
The function print() prints a string to the screen. Automatically tries to convert what is to be printed to text.
Syntax
print(*object, sep=' ', end='\n')
Argument
*object
Required. One or more objects to be printed. Separate objects with commas.
sep
Character to separate the objects being printed. Default is space.
end
Character that ends the text being printed. Default is "\n" - which means new line.
randint()
Generates a random integer
Example
from random import randint
roll = randint(1,6) #a random number 1-6
Explanation
The function randint() generates a random integer between two integer values a and b such that a <= N <= b. randint is in the random module.
Syntax
randint(a,b)
Argument
a
The smallest random number that can be generated.
b
The largest random number that can be generated.
round()
Rounds a number
Example #1
round(7.89) #rounds to 8
round(1.23) #rounds to 1
Example #2
round(1.23456,2) #rounds 1.23
round(1.23456,4) #rounds to 1.2346
Explanation
The function round() rounds a decimal number. By default to an integer, it is possible to set round() to round to an arbitrary number of decimals.
Syntax
round(number, decimalplaces = 0)
Argument
number
Required. The number to be rounded.
decimalplaces
Number of decimals the number should be rounded to. Default is 0.
str()
Converts to type string
Example #1
str(2.71) #converts the decimal number 2.71 to the string '2.71'
str(10) #converts the integer 10 to '10'
Example #2
age = 15
print('You are ' + str(age) + ' years old.')
Explanation
The function str() converts the argument to a string. Often the argument is an integer or a decimal number. It can also be significantly more complex data types that can be converted to strings.
Syntax
str(object)
Argument
object
An object that can be converted to a string.
while
while is used to repeat code
Example
n = int(input('How many numbers do you want to print?'))
i = 1
while i <= n:
print(i)
i = i + 1
Explanation
while repeats the code in the block as long as the condition is true.
Syntax
while condition:
condition
Required. Something that is True or False. Must become false at some point for the while-block to be exited.
Introduction
Here you will learn about
What Python is and what kind of language it is
What indentation is and why it is important in Python
What a comment in the code looks like
How it works
In all sections there are a number of tasks. At the bottom of the page it says how many tasks have been completed and how many there are in the section.
All examples have a green background color. Always try to run the code! Tasks have different background colors depending on the type of task; when you complete a task, it gets a green background color.
Click "Show tips" if you find a task difficult. There is no requirement that all tasks must be completed to continue.
Take your time! Read the text and examples carefully. Programming is not always easy at first!
Python is a programming language that is relatively easy to write and read. It was created in 1991 by Guido van Rossum.
Python 3 was launched in 2008 and it is the version of Python used here at Pythonlab.dev.
Python is an interpreted language, which means that the code is interpreted while the program is running.
There are a number of advantages to this, including that the code can easily be run on different platforms (Windows, Mac, Linux).
A major disadvantage is that interpreted languages are slower than languages that convert the code to machine code (ones and zeros), which are called compiled languages.
Nowadays, Python is one of the world's most popular programming languages. Major areas of use are web apps, AI (artificial intelligence or machine learning) and scientific calculations.
But Python can be used for most things.
Indentation
Indentation means that one or more lines are to the right of the text above. Look through the example below.
Here first two lines of text
that have no indentation.
Here comes indented text andthese green-marked lines belong tothe same block.
Now we are back to text without indentation.
Here is another line.
Here is a block that is indented andthese blue lines are one and the same block.Now we are at the next level of indentation.
This line has no indentation.
All lines indented above are colored. The lines that have the same color have the same indentation and belong to the same block. What a block means you will learn later in this course.
A line is indented with four spaces. Here at Pythonlab.dev you can also use the TAB key, which is located above Caps Lock,
and then four spaces are automatically written. This is also how many other editors used for writing code work.
In many programming languages
braces { } are used to show which lines belong together. In Python, this is done with indentation. In other languages
indentation is just a recommendation and the program works just as well without it. In Python, indentation is an important part of the language and the program
cannot be run if the indentation is incorrect.
Someone has forgotten to indent line 2 one step (one TAB or four spaces), correct the error and then run the program. Note that you do not need to try to understand the code.
-- Output from your program will be here --
In the code above, you can see that some words are blue and the number 3 is green.
It is common in programming to use an editor that highlights keywords, built-in functions, numbers, etc. with different colors.
Color coding is used to make the code easier to read and to find errors faster.
Comments
When programming, it is often important to write code that others can understand.
It is also important for your own sake. It is surprisingly difficult to remember how code you have written yourself works, even a short time afterwards.
A comment is plain text that is ignored by the computer. Using comments makes the code easier and faster to understand.
For example, by briefly describing the purpose of a block of code with a comment.
In Python, comments are written with the symbol #; subsequent text is considered a comment and is not part of the program code.
Example
In the example below, all green text is comments.
# This is a comment
import math #here we import Python's math library.
print(math.pi) #prints pi
# This is a comment
import math #here we import Python's math library.
print(math.pi) #prints pi