Python Basics: Variables and Data Types
What is a Variable?
A variable is simply a named location used to store data in memory. In Python, you don’t need to declare a type up front — the interpreter figures it out for you based on the value you assign.
name = "Asha" age = 28 is_student = False Python’s Core Data Types
Every value in Python belongs to a type. The four you’ll use constantly as a beginner are:
- str — text, wrapped in quotes:
"hello" - int — whole numbers:
42 - float — decimal numbers:
3.14 - bool — True or False
Checking a Variable’s Type
Use the built-in type() function whenever you’re unsure what you’re working with:
price = 19.99 print(type(price)) # <class 'float'> Type Conversion
You’ll often need to convert between types, especially when reading user input, which always comes in as a string.
user_input = "25" age = int(user_input) print(age + 5) # 30 Naming Rules
Variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and are case-sensitive. Python convention (PEP 8) favors snake_case for variable names.
Practice Exercise
Create three variables — your name, your age, and whether you’re currently learning Python — and print a sentence that combines all three using an f-string:
name = "Rohan" age = 22 learning = True print(f"{name} is {age} years old and learning Python: {learning}")
Leave a Reply