Learn Python: Variables and Data Types
Variables let you store data in Python so you can reference it later by a meaningful name. Python supports several built-in data types for different kinds of information, such as integers for whole numbers and strings for text. Because Python is dynamically typed, any variable can hold any type of data at any time. This lesson covers creating variables, identifying their data types, and converting from one type to another, all within Visual Studio Code (VS Code).
What will be covered
• How to create and use variables for different data types.
• The basic Python data types: int, float, str, bool, and None.
• Checking a variable’s type with type().
• Simple type conversion (casting) using functions like int() and str().
• Writing small scripts in VS Code that demonstrate these concepts.
Dynamic Typing
In Python, you don’t declare a variable’s type ahead of time. You simply write something like year = 2025, and Python infers the type (int in that example). If you later do year = "Future", Python adjusts the type from int to str. This is known as dynamic typing.
Common Data Types
Python uses a handful of built-in data types for everyday tasks:
Creating Variables and Checking Their Types
Here is a short script that demonstrates different types of variables. Create a file named variables_demo.py in VS Code, then run it using python variables_demo.py:
year = 2023
temperature = 98.6
greeting = "Hello"
is_sunny = True
print(year)
print(temperature)
print(greeting)
print(is_sunny)
print(type(year))
print(type(temperature))
print(type(greeting))
print(type(is_sunny))
When you run this, Python prints the values of each variable and then displays the type. Notice that Python automatically classifies them as int, float, str, or bool. If you change year to a string, then print type(year) again, you see that Python adapts dynamically.
Type Conversion (Casting)
Sometimes you need to convert from one type to another. For instance, if you receive user input via input(), it’s always a string, and you may want to convert it to an integer. You can use int() to get an int from a string that contains a number, or str() to turn other types into strings.
Below is a small script that reads an input string and attempts to convert it into an int:
user_input = input("Enter a whole number: ")
number_value = int(user_input)
print("You entered:", number_value)
print("Type is:", type(number_value))
If the user types something that isn’t a valid integer (like "Hello"), Python raises an error. For now, assume users type a valid integer. Later, you may learn how to handle incorrect inputs more gracefully.
Practice Exercises
- Create a file data_types_test.py. Declare at least one int, one float, one string, and one bool. Print them all, then print out their types. Verify the output in your terminal.
- Make a second file named change_type.py. Set a variable to a numeric value and print its type. Then assign a string to the same variable and print its type again. Observe how Python adjusts.
- Write a script user_age.py that asks the user to enter their birth year using input(). Convert it to an int. Subtract that birth year from 2025 (or any current/future year) to calculate an approximate age. Print the result. Only use basic arithmetic here (no additional concepts).
- Optional: try converting a float string into a float using float(). For instance, ask the user for a temperature ("98.6") and print the converted result with its type.
Summary
Variables store data that your program can reference later, and Python’s dynamic typing simplifies how you declare them. You can switch a variable from one type to another just by assigning a new value, but staying consistent can help avoid confusion. Each data type (int, float, str, bool, and None) serves distinct purposes, and Python includes type() to let you confirm what you’re working with. You also learned how to convert data types (casting) when you need to transform a string to an int or vice versa. These are powerful capabilities that form the basis of all Python programs, setting you up for future topics like user input processing and simple math operations.