Understanding Variables and Data Types in Python
What are Variables?
In Python, variables are used to store data that can be referenced and manipulated in your code. Think of variables as containers that hold information that can be used and changed.
How to Create Variables
Creating a variable in Python is straightforward. You just need to assign a value to a name using the equals sign (=
).
# Creating variables
age = 18
name = "Alice"
is_student = True
age
is a variable holding an integer value18
.name
is a variable holding a string value"Alice"
.is_student
is a variable holding a boolean valueTrue
.
Variable Naming Rules
- Must start with a letter or an underscore (_).
- Can contain letters, numbers, and underscores (_).
- Case-sensitive:
myVar
andmyvar
are different variables. - Cannot be a reserved word in Python (e.g.,
and
,if
,else
).
Data Types in Python
Python supports various data types. Here are some of the most common ones:
- Integers (
int
): Whole numbers, positive or negative, without decimals. - Floating-Point Numbers (
float
): Numbers with decimal points. - Strings (
str
): Sequence of characters, enclosed in quotes. - Booleans (
bool
): RepresentsTrue
orFalse
. - Lists (
list
): Ordered collection of items. - Tuples (
tuple
): Ordered, immutable collection of items. - Dictionaries (
dict
): Collection of key-value pairs.
Common Data Types with Examples
Integers (int
)
# Integer variable
age = 18
print(age) # Output: 18
Floating-Point Numbers (float
)
# Float variable
height = 5.9
print(height) # Output: 5.9
Strings (str
)
# String variable
name = "Alice"
print(name) # Output: Alice
Booleans (bool
)
# Boolean variable
is_student = True
print(is_student) # Output: True
Lists (list
)
# List variable
fruits = ["apple", "banana", "cherry"]
print(fruits) # Output: ['apple', 'banana', 'cherry']
Tuples (tuple
)
# Tuple variable
coordinates = (10.0, 20.0)
print(coordinates) # Output: (10.0, 20.0)
Dictionaries (dict
)
# Dictionary variable
person = {
"name": "Alice",
"age": 18,
"is_student": True
}
print(person) # Output: {'name': 'Alice', 'age': 18, 'is_student': True}
Working with Variables and Data Types
Basic Operations
You can perform various operations on variables depending on their data types.
Arithmetic Operations
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
Example:
# Arithmetic operations
a = 10
b = 5
print(a + b) # Output: 15
print(a - b) # Output: 5
print(a * b) # Output: 50
print(a / b) # Output: 2.0
String Operations
- Concatenation: Combining two strings using
+
- Repetition: Repeating a string using
*
Example:
# String operations
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name) # Output: Alice Smith
repeated_name = first_name * 3
print(repeated_name) # Output: AliceAliceAlice
List Operations
- Indexing: Accessing items by their position
- Appending: Adding an item to the end of the list
Example:
# List operations
fruits = ["apple", "banana", "cherry"]
# Indexing
print(fruits[1]) # Output: banana
# Appending
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
Real-Time Example: Simple Profile Information
Let’s create a simple program that stores and prints profile information.
# Profile information
name = "Alice"
age = 18
height = 5.9
is_student = True
# Printing profile information
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Student:", is_student)
Output:
Name: Alice
Age: 18
Height: 5.9
Is Student: True
Exercises
- Create variables to store your favorite book’s title, the author’s name, and the year it was published. Print these details.
- Write a program that creates a list of three favorite movies and prints each one using its index.
- Create a dictionary to store information about a car (make, model, year). Print each piece of information.
Conclusion
Understanding variables and data types is fundamental in Python programming. With this knowledge, you can store and manipulate different kinds of data effectively. In the next article, we will explore basic Python operations and expressions in more detail. Happy coding!