Topic ratings are back with the new {{Gradingbox}}. Please read the documentation for how to use it correctly before applying it to pages.
Python/Tutorial
The following is a brief tutorial on the functionality of Python. This tutorial assumes no prior programming knowledge, but still assumes enough technical or mathematical competence to learn the language.
This tutorial assumes Python 3.14 or later. While the tutorial may reference features or concepts in other languages, these will be explained with context.
Hello World[edit | edit source]
The Hello World program in Python is as follows:
print("Hello, world!")
print() is a built-in function, which is used to display text, variables (we will explain what this is later) or output to the screen.
Variables[edit | edit source]
In Python, a variable is a named value which can store data in the computer's memory. You can think of this as a labelled storage box (with a name and contents), and can be re-used later.
A variable is created as soon as something is assigned to it, using the equals sign.
name = "Nate" age = 19
Names can only contain letters, numbers and underscores; they must start with a letter or an underscore. Additionally, names are case-sensitive (so Age, age and AGE are three different names).
Finally, variables cannot have a keyword as a name.
In Python, the type of a variable may be changed at any time by re-assigning it:
x = 10 x = "Hello"
However, this is not good practice to reassign types, and we will avoid this in the tutorial.
Basic types[edit | edit source]
In Python, there are four basic types:
int: a whole number, without any decimal points.- For example:
42,-7,0
- For example:
float: a number with a decimal point.- For example:
3.14,2.71,-0.005,10.0
- For example:
str: a piece of text wrapped in a quote (single or double).- For example:
"Hello",'a',"nusoi","67"
- For example:
bool: a logical value representing a truth state.- Only values:
True,False
- Only values:
Type annotations[edit | edit source]
Python allows the user to explicitly indicate the type of a variable at declaration. For clarity, we will do this in this tutorial.
name: str = "Nate" age: int = 19
Reading input[edit | edit source]
To read input in Python, use the built-in input() function. A text prompt may be passed inside this function, which will be printed. When a line with input() is hit, the program is paused, and waits for the user to enter something into the console.
username: str = input("Input your name: ")
print("Hello, " + username + "!")
Note that input() itself returns a str, and that all user text is always treated as a string.
If we wanted to use input as a number, we can use casting. Python offers the following conversions
int(): converts a value tointfloat(): converts a value tofloatstr(): converts a value tostrbool(): converts a value tobool
This is an example of using the int() cast:
age: int = int(input("Enter your age: "))
print("Next year, you will be: ", age + 1)