Python Variables and Data Types: Complete Beginner's Guide (Chapter 2)
Vishal Yadav | Course Instructor
Imagine trying to build an application that remembers a user's name, calculates expenses, stores product prices, or checks whether a user is logged in. Every one of these tasks relies on two fundamental programming concepts: variables and data types.
Variables allow us to store information, while data types define what kind of information can be stored and how it can be used. Understanding these concepts is essential because they form the foundation of every Python program you will write.
In this chapter, you will learn how to create variables, follow proper naming conventions, work with numbers, strings, and booleans, check data types, and convert data from one type to another using practical examples.
Key Takeaway: Variables are containers for data, and data types determine the kind of values those containers can hold.
Variables in Python
A variable is a named storage location used to hold data that can be accessed and modified throughout a program.
Think of a variable as a labeled box. The label helps you identify the box, and the contents inside represent the stored value.
Creating Variables
In Python, variables are created automatically when a value is assigned.
name = "John"
age = 25
salary = 50000
Here:
- name stores a string value.
- age stores an integer value.
- salary stores a numeric value.
Displaying Variable Values
name = "John"
age = 25
print(name)
print(age)
Output:
John
25
Changing Variable Values
The value stored in a variable can be updated at any time.
city = "Delhi"
print(city)
city = "Mumbai"
print(city)
Output:
Delhi
Mumbai
Important: Python is a dynamically typed language, meaning you do not need to declare the data type of a variable explicitly.
Naming Conventions
Choosing meaningful variable names makes code easier to read, understand, and maintain.
Rules for Naming Variables
- Variable names can contain letters, numbers, and underscores (_).
- Variable names cannot start with a number.
- Variable names are case-sensitive.
- Python keywords cannot be used as variable names.
Valid Variable Names
first_name = "John"
age = 25
student1 = "David"
_total = 100
Invalid Variable Names
1name = "John" # Starts with a number
first-name = "John" # Contains hyphen
class = "Python" # Reserved keyword
Python Naming Best Practices
Python follows the snake_case naming convention.
student_name = "John"
monthly_salary = 50000
total_marks = 450
Avoid names like:
x = 10
a = 20
abc = 30
Instead use descriptive names:
user_age = 10
product_price = 20
total_amount = 30
Numbers in Python
Python supports multiple numeric data types for performing mathematical operations.
Integer (int)
An integer is a whole number without decimal places.
age = 25
year = 2025
quantity = 100
Example:
num1 = 10
num2 = 20
print(num1 + num2)
Output:
30
Float
A float is a number containing decimal points.
price = 99.99
height = 5.8
temperature = 36.5
Example:
price = 99.99
tax = 10.50
print(price + tax)
Output:
110.49
Complex Numbers
Complex numbers contain both real and imaginary parts and are represented using the letter j.
complex_num = 4 + 5j
print(complex_num)
Output:
(4+5j)
Working with Numbers
a = 20
b = 5
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
Output:
25
15
100
4.0
Strings in Python
A string is a sequence of characters enclosed within single quotes or double quotes.
name = "John"
city = 'Delhi'
Strings are used to store textual information.
Printing Strings
message = "Welcome to Python"
print(message)
Output:
Welcome to Python
String Concatenation
Concatenation means joining strings together.
first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)
Output:
John Smith
String Repetition
word = "Python "
print(word * 3)
Output:
Python Python Python
Accessing Individual Characters
language = "Python"
print(language[0])
print(language[1])
Output:
P
y
Useful String Functions
text = "python programming"
print(text.upper())
print(text.title())
print(len(text))
Output:
PYTHON PROGRAMMING
Python Programming
18
Booleans in Python
A Boolean data type can hold only one of two values:
- True
- False
Booleans are commonly used in decision-making and conditional statements.
is_logged_in = True
is_admin = False
Boolean Example
age = 18
print(age >= 18)
Output:
True
Real-World Example
has_ticket = True
if has_ticket:
print("Entry Allowed")
Output:
Entry Allowed
Booleans are the backbone of decision-making in programming.
Type Checking
Python provides the type() function to determine the data type of a variable.
name = "John"
age = 25
salary = 50000.50
print(type(name))
print(type(age))
print(type(salary))
Output:
Checking Boolean Type
is_active = True
print(type(is_active))
Output:
Type checking is especially useful when debugging programs and validating user input.
Type Conversion
Sometimes data needs to be converted from one type to another. This process is known as type conversion or type casting.
Convert String to Integer
age = "25"
age = int(age)
print(age)
print(type(age))
Output:
25
Convert Integer to Float
num = 100
num = float(num)
print(num)
Output:
100.0
Convert Number to String
score = 95
score = str(score)
print(type(score))
Output:
Convert Float to Integer
price = 99.99
price = int(price)
print(price)
Output:
99
Notice that the decimal part is removed during conversion.
Practical Example: User Input
age = input("Enter your age: ")
age = int(age)
print(age + 5)
Without converting the input to an integer, Python would treat it as a string.
Common Type Conversion Functions
- int() → Converts value to integer.
- float() → Converts value to float.
- str() → Converts value to string.
- bool() → Converts value to boolean.
Mini Project: Student Information System
Let's combine everything learned in this chapter.
student_name = "John"
student_age = 20
student_marks = 89.5
is_passed = True
print("Name:", student_name)
print("Age:", student_age)
print("Marks:", student_marks)
print("Passed:", is_passed)
print(type(student_name))
print(type(student_age))
print(type(student_marks))
print(type(is_passed))
Output:
Name: John
Age: 20
Marks: 89.5
Passed: True
Chapter Summary
In this chapter, you learned the essential building blocks of Python programming.
- Variables store data that can be used throughout a program.
- Meaningful variable names improve code readability.
- Python supports numeric types such as int, float, and complex.
- Strings are used for storing text.
- Booleans represent True and False values.
- The type() function identifies data types.
- Type conversion allows data to be transformed between different formats.
Next Chapter: Operators in Python – Arithmetic, Comparison, Logical, Assignment, and Membership Operators.
Vishal Yadav
A specialist dedicated to publishing high-quality, readable insights on technology, leadership, and digital growth.