AnyCompiler

Variables and Data Types

A variable is just a name that points to a value. Unlike Java or C#, Python doesn't need you to declare a type upfront — you just assign a value, and Python figures out the type from what you gave it.

name = "Alice"
age = 30
height = 5.6
is_student = False

print(name, type(name))
print(age, type(age))
print(height, type(height))
print(is_student, type(is_student))
Output
Alice <class 'str'>
30 <class 'int'>
5.6 <class 'float'>
False <class 'bool'>

That's the four types you'll use constantly: str (text), int (whole numbers), float (decimal numbers), and bool (True or False). The type() function tells you what type any value actually is — useful when you're not sure.

Python is dynamically typed

Because Python checks types at runtime rather than requiring them upfront, the same variable name can be reassigned to a completely different type later. This is convenient, but it's also a common source of bugs if you're not paying attention to what a variable actually holds at a given point in your code.

value = 10
print(value, type(value))

value = "now I am a string"
print(value, type(value))
Output
10 <class 'int'>
now I am a string <class 'str'>

Naming variables

  • Names can contain letters, numbers, and underscores, but can't start with a number
  • Python is case-sensitive — age and Age are two different variables
  • By convention, Python variable names use snake_case (student_name), not camelCase

One small convenience worth knowing: Python lets you assign several variables in a single line.

x, y, z = 1, 2, 3
print(x, y, z)
Output
1 2 3

Next: numbers — the actual arithmetic operators and how Python handles int vs. float math.

Practice exercise

Create a variable language set to the string "Python" and a variable version set to the int 3. Print them together with print(language, version).