CodingNic

Getting Started with Python

Variables and Data Types

Getting Started with Python 20 min read

Variables and Data Types

Objectives

By the end of this chapter, you should be able to:

  • Assign and use variables
  • Recognize Python’s core data types
  • Explain why Python is dynamically typed

💡 Why this matters: Variables are how a program remembers anything at all.

What Is a Variable?

A variable is a name that refers to a value.

python
age = 20
price = 19.99
name = "Erin"
is_active = True

That’s already an int, a float, a str, and a bool, without a formal definition of any of them yet.

Naming Rules

Before you assign a variable, know what names are actually allowed. A name must start with a letter or underscore, then continue with letters, numbers, or underscores. Names are case sensitive.

python
# Valid
_cats = "meow"
abc123 = 1970

# Invalid, both raise a SyntaxError
2cats = 1
hey@you = 1

Use lower_snake_case for variables:

python
my_variable = 3

Assigning Variables

= assigns a value to a variable. It doesn’t ask whether two things are equal, that’s a different operator (==), covered in the next lesson.

python
a = 1
b = "YOLO"
nothing = None

None represents the absence of a value. Assign multiple variables in one line:

python
a, b = 1, 2
a  # 1
b  # 2

Checking a Value’s Type

type() returns an object’s type:

python
>>> type(False)
<class 'bool'>
>>> type("nice")
<class 'str'>
>>> type(19.99)
<class 'float'>

A quick reference for the types you’ll use constantly:

Type Example
int 42
float 3.14
str "hello"
bool True
list [1, 2, 3]
dict {"a": 1}
tuple (1, 2)
NoneType None

Every value in Python has a type, and type() will always tell you what it is.

Dynamic Typing

Reassigning a variable to a new value of the same type is ordinary:

python
name = "erin"
name = "jordan"

Python also lets you reassign a variable to a completely different type:

python
value = None
value = 33.5
value = True

This is called dynamic typing. A variable doesn’t have a fixed type. It’s the value that has a type.

Strongly Typed

Python won’t convert between types automatically:

python
x = 3
y = " cats"
x + y  # TypeError

Convert explicitly instead:

python
str(x) + y  # '3 cats'

Python is dynamically typed (a variable’s type can change) and strongly typed (no automatic conversion between types).

Try It

  1. Create a variable of each type: int, float, str, bool, list. Confirm each with type().
  2. Try x + y where x is a number and y is a string. Read the TypeError, then fix it with str().
  3. Reassign one variable across three different types, checking type() after each.

Recap

  • A variable is a name that refers to a value. The value has a type, not the variable.
  • = assigns a value; it isn’t equality (that’s ==).
  • lower_snake_case is the naming convention for variables.
  • Python is dynamically typed but strongly typed: types can change, but never convert automatically.

Next lesson: strings, and the methods you’ll reach for constantly.