Introduction to Python Variables and Data Types

Learn the basics of Python variables and data types to get started with programming.

When you start programming in Python, one of the first concepts to understand is variables and data types. Variables are like containers that store information, and data types tell Python what kind of information is stored.

In Python, you don't need to declare the type of a variable explicitly. You just assign a value to a variable, and Python figures out the data type automatically. Common data types include integers (whole numbers), floats (decimal numbers), strings (text), and booleans (True or False).

python
age = 25
price = 19.99
name = "Alice"
is_student = True

print(type(age))      # Output: <class 'int'>
print(type(price))    # Output: <class 'float'>
print(type(name))     # Output: <class 'str'>
print(type(is_student))  # Output: <class 'bool'>

Sometimes, you might see an error like 'NameError: name 'x' is not defined'. This means you tried to use a variable before giving it a value. To fix it, make sure you assign a value to the variable before using it. For example, writing 'x = 10' before using 'x' in the code will prevent this error.

Understanding variables and data types is the foundation of programming in Python. After you feel comfortable with these concepts, you'll be able to store, manipulate, and use data efficiently in your programs.