Python Basics

Variables & Types

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 = True

Four 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

TypeWhat it holdsExample
strtext"Priya"
intwhole numbers34
floatdecimals87500.50
booltrue or falseTrue
NonenothingNone

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: name and Name are different labels

Constants are written in capitals by convention. Nothing enforces it; it is a signal to the reader.

MAX_ITERATIONS = 8

Avoid 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, a

Type hints, briefly

You can write down what you intend a variable to hold:

name: str = "Priya"
retries: int = 0

Python 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
Declaringint x = 5;x = 5
Typefixed at compile timebelongs to the value, not the label
Reassigning to another typenot allowedallowed
NullnullNone
Booleanstrue / falseTrue / False
Constantsfinal, constnaming convention only
String buildingString.formatf-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 == None instead of x is None.
  • Shadowing a built-in with a variable named list, dict, str or id.
  • Expecting b = a to 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 a and b list surprise, then fix it with .copy().

Try It Yourself

Copy this into a new Colab notebook and run it. Nothing to install.

example.py
1# String - text data
2model_name: str = "deepseek-chat"
3prompt: str = "Explain quantum computing"
4
5# Integer - whole numbers
6max_tokens: int = 1000
7retry_count: int = 3
8
9# Float - decimal numbers
10temperature: float = 0.7
11top_p: float = 0.9
12
13# Boolean - True/False
14stream: bool = True
15is_complete: bool = False
16
17# None - absence of value
18response = None
19
20# Check types
21print(f"model_name is {type(model_name).__name__}")
22print(f"temperature is {type(temperature).__name__}")
23
24# Type conversion
25tokens_str = "500"
26tokens_int = int(tokens_str)
27print(f"Converted: {tokens_int + 100}")