Variables And Data Types
A variable is a label you attach to a value so you can refer to it later. This is the smallest idea in programming and everything else sits on top of it.
name = "Priya"
age = 34
salary = 87500.50
is_active = TrueFour labels, four values. No type declared, no keyword in front, no semicolon. Python works out the type from the value.
The types you will actually use
| Type | What it holds | Example |
|---|---|---|
str | text | "Priya" |
int | whole numbers | 34 |
float | decimals | 87500.50 |
bool | true or false | True |
None | nothing | None |
That is very nearly the complete list for agent work. Lists and dictionaries come next, and they are built from these.
Two spellings to get right immediately: True and False are capitalised, and "nothing" is None, not null.
Checking a type
print(type(age))
print(type(name))<class 'int'>
<class 'str'>type(x) is the first thing to reach for when something behaves oddly. A surprising number of bugs are a number that is secretly text.
The quotes matter
age = 34
age_text = "34"
print(age + 1)
print(age_text + 1)The first prints 35. The second raises:
TypeError: can only concatenate str (not "int") to str"34" is text that happens to look like a number. Python will not guess what you meant.
This matters constantly in agent work, because anything read from a file, a form, an environment variable or an API arrives as text. Convert deliberately:
age = int("34")
ratio = float("0.75")
label = str(34)int("abc") raises ValueError, which is the right behaviour — you want to be told.
Naming
member_name = "Priya"
max_retries = 3- lowercase, words separated by underscores. This is called snake_case and it is the Python convention
- names may contain letters, numbers and underscores, and may not start with a number
- they are case-sensitive:
nameandNameare different labels
Constants are written in capitals by convention. Nothing enforces it; it is a signal to the reader.
MAX_ITERATIONS = 8Avoid naming a variable after something built in. list = [1, 2] works, and then breaks the list() function for the rest of the file.
A variable is a label, not a box
The box picture is fine on day one and slightly wrong afterwards. Assignment does not copy the value; it points a label at it.
a = [1, 2, 3]
b = a
b.append(4)
print(a)This prints [1, 2, 3, 4]. There is one list with two labels on it.
Numbers, text and booleans do not have this problem, because they cannot be changed in place. Lists and dictionaries can, so it matters for them. When you want a genuine copy:
b = a.copy()None is a real value
owner = None
if owner is None:
print("unassigned")None means "there is no value here". It is what a function returns when it has no return, and what you get from dict.get() when a key is missing.
Compare it with is, not ==. x is None is the correct and conventional form.
One trap worth knowing now: None, 0, "" and [] are all treated as false in an if. So if owner: is not the same question as if owner is not None: — the first is also false for an empty string. When you specifically mean "was this set", say so.
f-strings
The way to build text containing values.
name = "Priya"
tickets = 3
print(f"{name} has {tickets} open tickets")The f before the opening quote is what makes the braces work. Without it you get the braces printed literally, which is a common early confusion.
Anything can go inside the braces:
print(f"{name.upper()} has {tickets * 2} after doubling")
print(f"Ratio: {0.4567:.2f}"):.2f formats to two decimal places. You will use f-strings constantly, especially for building prompts.
Several at once
name, age, city = "Priya", 34, "Pune"This is called unpacking, and it is how you receive a function that returns more than one value:
def get_bounds():
return 0, 100
low, high = get_bounds()Swapping needs no temporary variable:
a, b = b, aType hints, briefly
You can write down what you intend a variable to hold:
name: str = "Priya"
retries: int = 0Python does not enforce this — it is a note for your editor and your reader. You need it rarely on variables and often on function parameters, where frameworks read it. There is a full page on it later.
Coming from Java or C#
| Java / C# | Python | |
|---|---|---|
| Declaring | int x = 5; | x = 5 |
| Type | fixed at compile time | belongs to the value, not the label |
| Reassigning to another type | not allowed | allowed |
| Null | null | None |
| Booleans | true / false | True / False |
| Constants | final, const | naming convention only |
| String building | String.format | f-string |
| End of statement | ; | end of line |
The adjustment that takes longest: the variable has no type, the value does. This is legal:
x = 5
x = "now text"It feels unsafe coming from a compiled language, and it is looser. Type hints plus a checker give back some of what you are used to.
Common mistakes
- Numbers that are secretly text, from input, JSON or the environment. Check with
type(x). - Forgetting the f on an f-string, and printing the braces.
- Writing true instead of True.
- Using
x == Noneinstead ofx is None. - Shadowing a built-in with a variable named
list,dict,strorid. - Expecting
b = ato copy a list or dictionary.
Practise this
- Create one variable of each of the five types and print each with its
type(). - Read a number from a string with
int(), then try it with"abc"and read the error. - Build a sentence with an f-string containing a calculation inside the braces.
- Reproduce the
aandblist surprise, then fix it with.copy().