Python Fundamentals · Units 1–2

Variables, Data Types & I/O

Variables, casting, input() and print()

Variables and values

Build a small marks report

Reveal the program in steps, then edit and run the complete version.

1234
student = "Mira"

Names, literals, and constants

Variable

A name that refers to a value, such as marks.

Literal

A value written directly in code, such as 91 or "Mira".

Constant

A value intended not to change. Python conventionally uses uppercase names like PASS_MARK; it does not enforce constants.

Type conversion
Starting up Python…
marks_text = "91"
marks = int(marks_text)
average = float(marks)
message = str(average)
print(type(marks_text), marks_text)
print(type(marks), marks)
print(type(average), average)
print(type(message), message)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Meet Python’s core data types

Choose a type to see a visual example and run a small operation.

Whole, decimal, and complex numeric values.

082
191.5
22+3j
Number in Python
Starting up Python…
roll_number = 82
average = 91.5
print(type(roll_number))
print(type(average))
print(roll_number + 3)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Input and output

Ask a question, then use the answer

When you run this example, enter a name in the prompt. Python’s input() returns text, so use int() when you need a number.

A greeting that responds to you
Starting up Python…
name = input("What is your name? ")
print("Welcome,", name)
year = int(input("What semester are you in? "))
print("Semester", year, "— keep practising!")
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.