Core Concepts

Variables, Types, and Input

30 min

Prerequisites

Lesson 1, for Python and your practice folder.

Back to the practice folder

cd ~/programming-basics-python

Variables

A variable is a name for a value. You create one with =:

username = "dewi"
failed_logins = 3

From then on, username means "dewi". Variable names use letters, digits, and underscores, cannot start with a digit, and are case-sensitive: Username and username are different. Pick names that say what the value is.

Types

Every value has a type. The four you need first:

Type Example What it is
str (string) "dewi", '203.0.113.50' Text, in quotes
int (integer) 3, 443 Whole numbers
float 0.75, 99.9 Numbers with a decimal point
bool (boolean) True, False Yes or no

Try them in the REPL (python3, or py on Windows). type() tells you a value’s type:

type("dewi")
type(443)
type(0.75)
type(True)
"443" + "1"
443 + 1

"443" + "1" gives '4431': adding two strings joins them. 443 + 1 gives 444. The quotes change everything. An IP address is a string, not a number, even though it contains digits.

f-strings: putting values into text

An f-string starts with f before the quote, and puts variables inside {}:

username = "dewi"
failed_logins = 3
print(f"{failed_logins} failed logins for user {username}")
3 failed logins for user dewi

Input

input() shows a question, waits for the user to type something and press Enter, and gives back what they typed, always as a string. To do maths with it, convert it with int().

Create logline.py in your practice folder:

username = input("Username: ")
failed = input("How many failed logins? ")
failed = int(failed)

print(f"INFO {failed} failed logins for user {username}")
print(f"That is {failed * 10} seconds of an attacker's time at 10 seconds each.")

Run it (python3 logline.py, or py logline.py on Windows) and answer dewi and 5:

Username: dewi
How many failed logins? 5
INFO 5 failed logins for user dewi
That is 50 seconds of an attacker's time at 10 seconds each.

Now run it again and type five instead of 5. Python stops with an error ending in:

ValueError: invalid literal for int() with base 10: 'five'

What to notice

  • A variable is a name for a value; its type decides what you can do with it.
  • "443" (a string) and 443 (an integer) behave differently. IP addresses are strings.
  • input() always returns a string. Convert with int() before doing maths.
  • Never assume input is what you expected. Unchecked input is where many security bugs begin.
Stuck, or just want to talk it through? Ask in the Discord, no need to get stuck here alone.

Reflect on your own

  • Why does `input()` always give you text, even when someone types a number?
  • A script crashes when a user types "five" instead of 5. Why is that a security question, not just a bug?

Go further

Spotted a mistake, or a command that did not work for you? Report it on GitHub or by email.