This article will help you understand what variables are, explore Python's basic data types such as Numbers, Strings, Booleans, and NoneType, and learn how to use type casting and the type() function.
What are Variables in Python?
A variable is simply a name that refers to a value stored in memory. You can think of it as a container that holds information which can be used and modified throughout a program. Example:name = "Alice"
age = 25
is_student = True
Here:- name is a string variable
- age is a number variable
- is_student is a boolean variable
Rules for Naming Variables
Python variable names must follow certain rules:- Variable names can contain letters, numbers, and underscores (_).
- They must begin with a letter or underscore, not a number.
- Variable names are case-sensitive (Name and name are different).
- Avoid using Python keywords like class, for, if, etc.
Valid Examples:
username = "John"
_user_id = 123
total_marks = 95.5
Invalid Examples:2name = "Alex" # โ Cannot start with a number
my-variable = 10 # โ Hyphens not allowed
class = "Python" # โ 'class' is a reserved keyword
Data Types in Python
Every value in Python belongs to a specific data type. These types define how the data is stored and what operations can be performed on it.The most common built-in data types are:
- Numbers
- Strings
- Booleans
- NoneType
1. Numbers
Python supports three main types of numeric values:int โ Integer (whole numbers)
float โ Floating-point numbers (decimal values)
complex โ Complex numbers (a + bj form)
Example:
x = 10 # int
y = 3.14 # float
z = 2 + 5j # complex
print(type(x)) #
print(type(y)) #
print(type(z)) #
You can also perform arithmetic operations on numbers:
a = 10
b = 4
print(a + b) # Addition โ 14
print(a - b) # Subtraction โ 6
print(a * b) # Multiplication โ 40
print(a / b) # Division โ 2.5
print(a // b) # Floor Division โ 2
print(a % b) # Modulus โ 2
print(a ** b) # Exponentiation โ 10000
2. Strings
A string is a sequence of characters enclosed in single (' '), double (" "), or triple quotes (''' ''').Example:
greeting = "Hello, Python!"
print(greeting)
print(type(greeting)) #
You can access characters using indexing and combine strings using concatenation:
text = "Python"
print(text[0]) # P
print(text[-1]) # n
print(text + " Rocks!") # Python Rocks!
Common string methods:
word = "learning"
print(word.upper()) # LEARNING
print(word.capitalize()) # Learning
print(word.replace("ing", "ed")) # learned
3. Booleans
The Boolean data type in Python has only two possible values: True or False. They are often used in conditional statements and logical operations.Example:
is_active = True
is_admin = False
print(type(is_active)) #
print(10 > 5) # True
print(10 == 5) # False
4. NoneType
The NoneType represents the absence of a value. It's often used as a placeholder when a variable is defined but has no value assigned yet.Example:
result = None
print(result)
print(type(result)) #
Type Casting in Python
Type casting means converting a value from one data type to another. Python allows explicit conversion using built-in functions like int(), float(), str(), and bool().Example:
x = 5 # int
y = 2.8 # float
z = "10" # string
# Convert types
print(float(x)) # 5.0
print(int(y)) # 2
print(int(z)) # 10
print(str(x)) # "5"
Boolean Conversion Examples:
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("Hi")) # True
โ ๏ธ Note: Type casting can lead to data loss (e.g., converting float to int removes decimal part).
The type() Function
The type() function is used to check the data type of any variable or object.Example:
a = 100
b = 12.5
c = "Python"
d = True
print(type(a)) #
print(type(b)) #
print(type(c)) #
print(type(d)) #
This function is extremely useful for debugging and understanding the kind of data you're working with.
Summary
Variables and data types are the foundation of every Python program. Understanding how to store, identify, and convert data will help you build more complex programs with ease.In the next article, we'll explore Operators in Python โ where you'll learn how to perform calculations, comparisons, and logical operations effectively.