Python Full Tutorial — Part 2: Variables & Data Types

By Suraj Ahir December 05, 2025 7 min read

Python — Python Variables & Types
Python — Python Variables & Types
← Part 1 Python Tutorial · Part 2 of 12 Part 3 →

In Part 1, we set up Python and understood what it is. Now we go into the actual foundation of programming: how Python stores and manages information. This is where real programming thinking begins.

What is a Variable?

A variable is a name you give to a piece of data so you can refer to it later. Think of it like a label on a box — the label is the variable name, and whatever is inside the box is the value. In Python, you create a variable simply by writing a name, then an equals sign, then the value:

Creating Variables
age = 25
name = "Suraj"
is_student = True
height = 5.9

No special keywords needed. No type declarations. Just name = value and Python handles everything else. This simplicity is by design.

How Python Actually Stores Variables

Here is something most beginner tutorials skip — and it causes confusion later. In Python, variables do not contain values. They point to values stored in memory. This is called a reference model.

Reference Model Example
a = 10
b = a
a = 20

print(a)  # 20
print(b)  # 10

When you wrote b = a, Python made b point to the same value a was pointing to at that moment, which was 10. When you then changed a to point to 20, b did not change — it was still pointing to 10. Understanding this model will save you from many confusing bugs later.

Python's Core Data Types

Python has several built-in data types. Let us understand each one properly:

1. Integer (int)

Whole numbers — positive, negative, or zero. No decimal point.

Integer Examples
score = 100
temperature = -5
items_in_cart = 0
population = 1400000000
2. Float

Numbers with decimal points. Used when precision matters.

Float Examples
price = 99.99
pi = 3.14159
percentage = 78.5
3. String (str)

Text data. Always wrapped in quotes — single or double, both work the same way.

String Examples
city = "Rajkot"
message = 'Learning Python is powerful'
greeting = "Hello, " + city  # String joining
4. Boolean (bool)

Only two possible values: True or False. Used heavily in conditions and logic.

Boolean Examples
is_logged_in = True
has_paid = False
is_adult = age >= 18  # Evaluates to True or False
5. NoneType

Represents the absence of a value. When a variable has no meaningful value yet, we use None.

None Example
result = None  # No value assigned yet

Checking Data Types

Python gives you a built-in function called type() to check what type a variable holds:

Checking Types
x = 42
y = 3.14
z = "hello"
flag = True

print(type(x))    # <class 'int'>
print(type(y))    # <class 'float'>
print(type(z))    # <class 'str'>
print(type(flag)) # <class 'bool'>

Type Conversion

Sometimes you need to convert data from one type to another. Python makes this straightforward:

Type Conversion Examples
# String to integer
age_text = "25"
age_number = int(age_text)   # Now it's 25 as a number

# Integer to string
count = 100
count_text = str(count)      # Now it's "100" as text

# String to float
price_text = "49.99"
price = float(price_text)    # Now it's 49.99 as a decimal

# Integer to float
whole = 5
decimal = float(whole)       # Now it's 5.0

This matters a lot when reading user input — everything the user types comes in as a string, and you often need to convert it to a number to do calculations.

Naming Rules for Variables

Python has clear rules for what you can name your variables:

Valid and Invalid Names
user_name = "Suraj"    # valid
_temp = 100            # valid
firstName = "Raj"      # valid

2name = "wrong"        # INVALID — starts with number
my-name = "wrong"      # INVALID — hyphen not allowed
for = 5                # INVALID — reserved word

Multiple Assignment in One Line

Python lets you assign multiple variables at once — useful for clean, readable code:

Multiple Assignment
x, y, z = 10, 20, 30
print(x, y, z)  # 10 20 30

# Swap two variables (Python makes this elegant)
a = 5
b = 10
a, b = b, a
print(a, b)  # 10 5

Why This Matters

Variables and data types are not just "basics" to get through. They are the foundation everything else is built on. When you work with APIs, databases, machine learning models, or web applications — you are always working with data stored in variables of specific types. Understanding how Python references values in memory will help you avoid bugs that confuse even experienced developers.

In Part 3, we will learn about operators and how Python performs calculations, comparisons, and logical decisions.

Common Mistakes with Variables

Understanding what to avoid is just as important as knowing the right approach. The most common mistake beginners make is confusing assignment with comparison. A single equals sign (=) assigns a value. A double equals sign (==) checks if two values are equal. Writing if x = 5 instead of if x == 5 is a syntax error in Python. Another frequent mistake is using a variable before assigning it a value — Python will raise a NameError in this case, not silently use a default. Always initialize variables before using them.

Dynamic Typing — A Powerful but Sharp Tool

Python is dynamically typed, meaning a variable can hold any type of value and that type can change during the program's execution. You can write x = 5 and then x = "hello" and Python will not complain. This flexibility is part of what makes Python fast to write, but it also means you need to be aware of what type your variables hold at any given point. In larger programs, using type hints — a Python feature that documents expected types without enforcing them — helps maintain clarity. Understanding dynamic typing is what separates Python from statically typed languages like Java or C++, where you must declare the type of every variable.

Practice Exercise

Create a small Python script that: declares variables for a person's name (string), age (integer), height in meters (float), and whether they are employed (boolean). Print each variable along with its type using type(). Then convert the age to a float and the height to a string, and print the results. This exercise will reinforce variable assignment, the core data types, and type conversion all in one.

Disclaimer: This content is for educational purposes only. SRJahir Tech does not guarantee any specific outcome, job placement, or exam result. Learning requires consistent effort and practical application.