Variables and types
A variable is a name that points at a value. You create one with name = value. The = is *assignment* — "make this name refer to this value" — not the maths equals.
Every value has a type. The four you meet first:
str— text, in quotes:"rungo-api"int— whole numbers, no quotes:1,-40,9000float— numbers with a decimal point:3.14,2.0bool— truth values, the bare wordsTrueandFalse(capital T/F)
Python is *dynamically typed*: you don't declare the type, Python works it out from the value. A variable can even be reassigned to a different type later (though doing that casually is a smell). Use type(x) to ask what type a value is.
Syntax
name = value
service_name = "rungo-api" # str
version = 1 # int
ratio = 0.75 # float
is_live = True # boolWorked examples
Storing three values of different types
service_name = "rungo-api"
version = 1
is_live = True
print(service_name, version, is_live)
# Output:
# rungo-api 1 TrueChecking a value's type
print(type(1)) # <class 'int'>
print(type(1.0)) # <class 'float'>
print(type("1")) # <class 'str'>
print(type(True)) # <class 'bool'>Reassigning a variable (the name re-points)
count = 5
count = count + 1
print(count) # 6Common mistakes & gotchas
true / false are not booleans
Python's booleans are True and False with a capital first letter. Lowercase true is treated as a variable name and raises NameError.
"1" is not 1
The string "1" is text; the int 1 is a number. "1" + "1" gives "11" (joined text), while 1 + 1 gives 2. Mixing them — "1" + 1 — raises TypeError.
= versus ==
A single = assigns a value. Double == compares two values and gives a bool. x = 5 stores 5; x == 5 asks "is x equal to 5?". Using = where you meant == is a classic beginner bug.
Why it matters
Variables are how a program remembers things between steps. Knowing the type of a value tells you what you can do with it — you can add ints, join strings, and test bools — and most early errors come from a value being a different type than you assumed.