Variables are like containers that store data in your Python programs. Think of them as labeled boxes where you can keep different types of information for later use.
Creating Variables
In Python, creating a variable is simple. You just assign a value to a name:
name = "Alice"
age = 25
height = 5.6
Common Data Types
Strings (str): Text data enclosed in quotes
message = "Hello, Python!"
city = 'New York'
Integers (int): Whole numbers
score = 100
year = 2020
Floats (float): Decimal numbers
temperature = 98.6
price = 19.99
Booleans (bool): True or False values
is_sunny = True
is_raining = False
Checking Data Types
Use the type()
function to check what type of data a variable contains:
print(type(name)) #
print(type(age)) #
Python automatically determines the data type based on the value you assign, making it easy to work with different kinds of information.