Introduction
Variables and Data Types are fundamental concepts in Python programming. Every Python program uses variables to store information and data types to define the nature of that information.
Whether you are building Artificial Intelligence applications, Machine Learning models, websites, automation scripts, or Data Science projects, understanding variables and data types is essential.
In this tutorial, you will learn what variables are, why they are important, different types of data types in Python, and how they are used in real-world programming.
Learning Objectives
After completing this tutorial, you will be able to:
- Understand Python variables.
- Create and use variables in Python.
- Understand Python data types.
- Differentiate between multiple data types.
- Use variables with Python programs.
- Apply variables and data types in real-world examples.
What is a Variable in Python?
A variable is a named storage location used to store data in memory.
In simple terms, a variable acts like a container that holds information.
For example:
name = "John"
Here:
- name → variable name
- “John” → stored value
The variable stores the value so that it can be used later in the program.
Why Variables are Important
Variables are important because they allow programs to store, update, and reuse information.
Without variables, developers would need to repeatedly type the same values throughout a program.
Variables help make programs:
- Flexible
- Reusable
- Dynamic
- Easier to maintain
Example:
student_name = "Alice"
print(student_name)
Output:
Alice
Creating Variables in Python
Python variables are created using the assignment operator (=).
Syntax:
variable_name = value
Example:
age = 25
city = "London"
salary = 50000
Python automatically identifies the data type of the assigned value.
Variable Naming Rules
Python follows certain rules for naming variables.
Allowed Rules
- Must start with a letter or underscore.
- Can contain letters, numbers, and underscores.
- Variable names are case-sensitive.
Valid Examples:
student
student_name
_age
marks1
Invalid Examples
1name
student-name
class
Multiple Variable Assignment
Python allows assigning multiple variables in one line.
Example:
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Output:
10
20
30
What are Data Types in Python?
A data type defines the kind of value stored inside a variable.
Different types of data require different operations.
Python provides several built-in data types.
Main Data Types in Python
1. Integer (int)
Integer data type stores whole numbers.
Examples:
x = 100
y = -25
Output:
100
-25
Integers are commonly used for:
- Age
- Marks
- Quantity
- Counting values
2. Float
Float data type stores decimal numbers.
Example:
price = 99.99
temperature = 36.5
Floats are useful for:
- Measurements
- Scientific calculations
- Machine Learning models
- Financial data
3. String (str)
String data type stores text values.
Strings can be written using single quotes or double quotes.
Example:
name = "Python"
course = 'Artificial Intelligence'
Output:
Python
Artificial Intelligence
Strings are widely used for:
- User names
- Emails
- Messages
- Text processing
4. Boolean (bool)
Boolean data type stores only two values:
- True
- False
Example:
is_logged_in = True
payment_success = False
Booleans are important for:
- Decision making
- Conditions
- Authentication systems
- Program logic
5. List
A list stores multiple values in one variable.
Example:
fruits = ["Apple", "Banana", "Orange"]
Lists are:
- Ordered
- Changeable
- Allow duplicate values
6. Tuple
A tuple is similar to a list but cannot be modified after creation.
Example:
colors = ("Red", "Blue", "Green")
7. Dictionary
A dictionary stores data in key-value pairs.
Example:
student = {
"name":"John",
"age":22
}
Output:
John
22
Checking Data Types in Python
Python provides the type() function to identify a variable’s data type.
Example:
x = 100
print(type(x))
Output:
<class 'int'>
Type Conversion in Python
Type conversion means converting one data type into another.
Example:
x = "100"
y = int(x)
print(y)
Output:
100
Common conversion functions:
- int()
- float()
- str()
- bool()
Real-World Examples
Student Management System
student_name = "David"
age = 21
fees_paid = True
print(student_name)
print(age)
print(fees_paid)
E-Commerce Example
product = "Laptop"
price = 75000
stock = True
Variables store important product information.
Python Example
name = "Alice"
age = 20
percentage = 92.5
passed = True
print(name)
print(age)
print(percentage)
print(passed)
Output:
Alice
20
92.5
True
Interview Questions
1. What is a variable in Python?
A variable is a container used to store data values.
2. What is a data type?
A data type defines the kind of value stored inside a variable.
3. Which function checks data type in Python?
The type() function checks data types.
4. Name four common Python data types.
Integer, Float, String, and Boolean.
Assignment
- Create variables for name, age, and city.
- Create one integer, float, string, and boolean variable.
- Print the data type of each variable.
- Create a list containing five fruits.
- Convert a string number into an integer.
Quiz
Q1. Which operator is used to assign values to variables?
- A. ==
- B. =
- C. !=
- D. +
Answer: B. =
Q2. Which data type stores decimal values?
- A. int
- B. bool
- C. float
- D. str
Answer: C. float
Q3. Which function identifies a variable’s data type?
Answer: type()
Summary
In this tutorial, you learned about Python variables and data types. Variables help store information, while data types define the nature of stored values.
You explored integers, floats, strings, booleans, lists, tuples, and dictionaries, along with variable creation rules and type conversion methods.
These concepts are essential for Python programming, Artificial Intelligence, Data Science, and software development.
Next Tutorial
Tutorial 12: Python Operators
