Variables and Data Types in Python
In the previous lesson, we introduced Python and explored its key features. Today, we will learn about Variables and Data Types, which are among the most important concepts in Python programming.
Variables are used to store data in a program. They act like containers that hold information which can be used and modified later in the code.
Python makes variable creation simple because you do not need to declare the data type before assigning a value.
What is a Variable?π€·ββοΈπ€·ββοΈ
A variable is a name used to store data in memory.
Example:
name = "Mike"
age = 21
In the example above:
- "name" stores text data
- "age" stores numeric data
Rules for Naming Variables
When naming variables in Python, follow these rules:
- Variable names must start with a letter or underscore "_"
- They cannot start with a number
- Variable names are case-sensitive
- Spaces are not allowed
- Special characters are not allowed except underscore "_"
Valid Variable Names
student_name = "Brian"
_age = 20
totalMarks = 450
Invalid Variable Names
2name = "John"
student-name = "Brian"
my name = "Alex"
Data Types in Python
Data Types define the type of data stored in a variable.
Python has several built-in data types.
1. String (str)
A String is used to store text.
Example
course = "Python Programming"
Strings are written inside quotation marks.
2. Integer (int)
An Integer stores whole numbers.
*Example
*
students = 120
3. Float (float)
A Float stores decimal numbers.
Example
temperature = 36.5
4. Boolean (bool)
A Boolean stores only two values:
- True
- False
*Example
*
is_logged_in = True`
Booleans are commonly used in conditions and decision making.
Checking Data Types
Python provides the "type()" function to check the type of data stored in a variable.
Example
name = "Python"
age = 10
print(type(name))
print(type(age))
Output
<class 'str'>
<class 'int'>
Multiple Variable Assignment
Python allows assigning multiple variables in one line.
Example
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Type Conversion
Type Conversion means changing one data type into another.
Example
age = "21"
converted_age = int(age)
print(converted_age)
In this example, a String is converted into an Integer.
Why Variables are Important
Variables help programmers:
- Store data
- Process information
- Build dynamic applications
- Perform calculations
- Create reusable programs
Without variables, programming would not be possible.
Conclusion
Variables and Data Types form the foundation of Python programming. Understanding how data is stored and managed helps beginners write better and more organized programs.
As you continue learning Python, variables will be used in almost every program you create.
Practice creating variables using different data types and experiment with printing their outputs.

Top comments (0)