Variables and Data Types
Variables are the building blocks of any program. Think of them as labeled boxes where you can store data to use later.
Assigning Variables
In Python, you create a variable by giving it a name and assigning a value using the = operator.
name = "Radhe Krishna Singh"
age = 25
is_coding = True
Basic Data Types
Python “guesses” the type of data you’re giving it. Here are the four most common types:
- Strings (
str): Text, always inside quotes. E.g.,"Hello". - Integers (
int): Whole numbers. E.g.,42. - Floats (
float): Decimal numbers. E.g.,3.14. - Booleans (
bool): True or False values.
Naming Rules (Pythonic Way)
To write clean Python code, follow these naming conventions:
- Use snake_case (lowercase with underscores):
user_name,total_price. - Do not start with a number.
- Use descriptive names (
user_ageis better thanx).
Dynamic Typing
One of Python’s most powerful features is dynamic typing. You can change the type of a variable at any time (though it’s usually better to be consistent).
data = "Start"
data = 100
print(data) # This works!
In the next module, we’ll see how to perform operations on these variables to build actual logic.