Python › Programming Fundamentals

Variables and the types you'll actually use

4 min read Beginner 5 sections

A variable is a named box that holds a value. You make one by assigning with =. Python figures out the type for you — you never have to declare it. That’s the whole concept, and you’ll use it in every line of code you ever write.

You'll learn to

  • Create variables and check their type
  • Tell the five core data types apart
  • Avoid the string-vs-number mistake that crashes scripts

The five types that matter

You don’t need to memorise a long list. Five types carry almost all of security scripting:

target = "example.com"      # str   — a string (text)
port = 443                  # int   — a whole number
ratio = 1.5                 # float — a decimal number
is_up = True                # bool  — True or False
nothing = None              # None  — the "no value" value

print(type(target))         # <class 'str'> — type() tells you what something is

Walk through it. target = "example.com" stores text — the quotes are what make it a string. port = 443 has no quotes, so it’s a number you can do maths with. True and False are booleans (note the capital letters). And None means “nothing here yet” — you’ll see it when a lookup fails or a function returns no result.

The trap that gets everyone

Here’s the one to burn into memory early: the string "443" and the number 443 are not the same thing, and Python treats them differently.

"443" + 1       # CRASHES — you can't add a number to text
443 + 1         # 444     — normal maths
"443" + "1"     # "4431"  — gluing two strings together

When you read a port or an ID out of a file, it arrives as text. If you need to do maths with it, convert it explicitly.

int("443")      # 443  — text to number
str(443)        # "443" — number to text

Choosing good names

Variable names are free — spend them. t = "example.com" saves three keystrokes and costs you clarity every time you reread the code. target = "example.com" tells the next person (usually future-you) exactly what it holds.

# Hard to follow later:
x = requests.get(u).status_code

# Tells the story:
status = requests.get(target_url).status_code

Checkpoint

You read the value '8080' from a file and want to add 1 to it. What do you need to do first, and why?

Try it yourself

In the REPL, create a variable host = "scanme.example" and a variable port = 80. Then build and print a target string with both. (Hint: you’ll learn the clean way — f-strings — in the next lesson, but try gluing with str(port) for now.)

Summary

A variable is a named box; you assign with = and Python infers the type. The five types you’ll lean on are str, int, float, bool, and None. The string "443" and the integer 443 behave differently — convert explicitly with int() and str(). Name variables clearly; the keystrokes you save aren’t worth the confusion they cost.

Key takeaways

  • = assigns a value; Python figures out the type.
  • Quotes make a string; no quotes makes a number.
  • "443" is not 443 — convert with int() / str() when needed.
  • Clear, lowercase, underscore names pay you back every time you reread.

Quick quiz

Next, we go deep on the type you’ll touch more than any other: strings. Almost everything in security work — URLs, tokens, headers, responses — is text.

Was this lesson helpful?