All Python programs work with data – numbers, text, true/false values. You need a way to store it and label it to do anything useful with that data. That’s what variables are for.
In this tutorial, you will learn how to create variables in Python, the rules for naming variables and the basic data types that you will use in just about every program you write.
Contents
- What Is a Variable in Python?
- How to Declare a Variable in
- Python Variable Naming Conventions
- Python Data Types Assigning Multiple Values in One Statement
- Numeric Types (int, float, complex) String (str)
- Bool (Boolean)
- None
- Checking a Variable’s Type using type()
- Type Casting (Type Conversion)
- Dynamic Typing of Python
- Common Errors
- Best Practices
- Interview Questions
- Frequently Asked Questions
- Conclusion
What Is a Variable in Python?
A variable is a name for a value stored in memory. Think of it as a box with a label on it — you put something in the box and you find it again with the label.
|
1 |
age = 25 |
Here age is the name of the variable and 25 is the value of the variable. Python differs from languages like C or Java in that you don’t have to declare the type of a variable ahead of time; Python will figure it out for you based on the value you assign to it.
How to Declare a Variable
You make a variable by assigning a value to a name with the = operator:
|
1 2 3 4 |
name = "Alex" age = 25 height = 5.9 is_student = True |
Output:
|
1 |
No output – this just defines the variables in memory. |
You do not need to declare separately. In Python, the variable is created when you assign a value to it.
Python Variable Naming Conventions
Python has a few strict rules, and some conventions to keep your code readable:
Rules (have to follow):
- Names may contain letters, numbers, and underscores (_)
- A name can’t start with a number (1name is not valid, name1 is valid)
- Names are case sensitive (Age and age are different variables)
- Python has reserved keywords (for, class, True, etc.) that you can’t use as variable names.
Conventions (to be followed):
Variable names should be like snake_case (first_name, not firstName).
Use descriptive names (count not c) python
|
1 2 3 |
user_name = "Ravi" # valid and readable _temp = 10 # valid, often used for internal/temporary values 2nd_value = 5 # invalid — SyntaxError |
Output :
|
1 2 3 4 |
File "<stdin>", line 3 2nd_value = 5 ^ SyntaxError: invalid decimal literal |
Multiple Values to Multiple Variables on One Line
Python lets you assign multiple variables in one line, making your setup code more compact.
|
1 2 3 4 5 6 7 |
# Assign different values to different variables x, y, z = 10, 20, 30 print(x, y, z) # Assign the same value to multiple variables a = b = c = 100 print(a, b, c) |
Output:
|
1 2 |
10 20 30 100 100 100 |
Python Types
Python has many built-in data types. Here are the ones you’ll use all the time when you’re a beginner.
|
1 2 3 4 5 6 7 |
whole_number = 42 # int decimal_number = 3.14 # float complex_number = 2 + 3j # complex print(type(whole_number)) print(type(decimal_number)) print(type(complex_number)) |
Output:
|
1 2 3 |
<class 'int'> <class 'float'> <class 'complex'> |
- int — whole numbers, positive or negative, no size limit (Python handles big integers natively)
- float — numbers with a decimal point
- complex — numbers with a real and imaginary part (j denotes the imaginary unit) — rarely needed unless you’re doing scientific computing
String (str) — Single, double or triple quoted text.
|
1 2 3 4 5 6 7 |
greeting = "Hello, World!" name = 'Alex' multiline = """This spans multiple lines""" print(greeting) print(name) |
Output:
|
1 2 |
Hello, World! Alex |
Boolean (bool)
Is one of exactly two values: True or False. Note the capital first letter – this confuses a lot of beginners coming from other languages.
|
1 2 3 4 5 |
is_logged_in = True has_permission = False print(is_logged_in, has_permission) print(type(is_logged_in)) |
Output:
|
1 2 |
True False <class 'bool'> |
None Type
None does not mean that a value is missing, such as null in other languages.
|
1 2 3 |
result = None print(result) print(type(result)) |
Output:
|
1 2 |
None <class 'NoneType'> |
Checking the Type of a Variable with type()
You can check the data type of any variable at runtime using the built-in type() function — very useful when debugging.
|
1 2 3 4 5 |
value = 25 print(type(value)) value = "twenty-five" print(type(value)) |
Output:
|
1 2 |
<class 'int'> <class 'str'> |
This is because the same variable name was re-assigned to a different type – which leads directly into the next section.
Type Conversion (Type Casting)
Sometimes you need to change one data type to another. For example, you might want to change the user input (which is always a string) to a number.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
age_str = "25" age_int = int(age_str) # string to int price = 100 price_float = float(price) # int to float count = 5 count_str = str(count) # int to string print(age_int, type(age_int)) print(price_float, type(price_float)) print(count_str, type(count_str)) |
Output:
|
1 2 3 |
25 <class 'int'> 100.0 <class 'float'> 5 <class 'str'> |
Explanation: Line-by-line
- int(“25”) changes the string “25” to the integer 25. This will raise a ValueError if the string is not a valid number (e.g. int(“abc”)).
- We can turn the integer 100 into a float using float(100) and that will give us 100.0.
- str(5) converts the number 5 into the string “5”.
Python is Dynamically Typed
Python is dynamically typed, which means the type of a variable is determined by the value it is holding, and can change if you assign it a different value.
|
1 2 3 4 5 6 7 8 |
data = 10 # int print(type(data)) data = "ten" # now a string print(type(data)) data = 10.0 # now a float print(type(data)) |
Output:
|
1 2 3 |
<class 'int'> <class 'str'> <class 'float'> |
Think of a statically typed language like C or Java . Once you declare a variable of a certain type , it can never change . With dynamic typing, Python is faster to write, but it also means that type-related bugs don’t surface until runtime, not compile time. This makes testing even more important.
Common Mistakes
- Using a keyword as a variable name — class=”A” throws SyntaxError.
- Confusing = and ==. = means assign something to something, == means compare two things.
- If you forget to capitalize booleans — there is no true/false (lowercase) in Python, it’s True/False.
- Assuming input() returns a number input() always returns a string, even if the user types digits. You need to cast it to int() or float() explicitly.
- Accidentally assigning a variable to an unrelated type which can cause confusing bugs later in long scripts.
Best practice
- Use descriptive variable names in snake_case. Use total_price, not tp.
- Python allows you to change the type of a variable, but you should keep it consistent within a given scope.
- Debug unexpected value errors: use type() or isinstance().
- Explicitly convert user input with int()/float() before doing math on it .
- Use ALL_CAPS constants (by convention) for values that shouldn’t change, e.g. MAX_USERS = 100
Questions for the Interview
Q1. Python is dynamically typed or statically typed?
Dynamically typed – the type of a variable is determined at runtime from the value it is assigned and can change.
Q2. What is the difference between int() and float() for conversion?
int() will convert a value to an integer (dropping any decimal part), whereas float() will convert to a floating point number.
Q3. Why int(“3.5”) throws an error?
Because int() expects to be given a string that represents a whole number. “3.5” is not a valid integer literal you would have to do int(float(“3.5”)) in two steps.
Q4. What data type is always returned by input()?
A string (str), no matter what the user types.
Q5. Can we have multiple variables pointing to the same value in python?
Yes. For example, a = b = 10 assigns the same integer object to both a and b.
FAQs
Q: Do I have to declare variable type in python?
No. You give it the value and Python figures it out .
Q: What’s the difference between None, 0 and False?
None means “no value at all “. 0 and False are real values (a number and a boolean). All three are considered “falsy” when used in a condition, but they are different types and different objects.
Q: Are variable names case sensitive in Python?
Yes. Name, name, and NAME are three different variables.
Q: Can a variable in Python change its data type?
Yes, since Python is dynamically typed, reassigning a variable to a new value can change its type.
Summary
- Python doesn’t require you to declare the type of a variable, you just create them by assigning a value to them.
- The built-in core types are int, float, complex, str, bool, and None.
- You can check a variable’s current data type with type().
- Explicitly convert between types with
int(),float(), andstr(). - Dynamic typing means that a variable can change its type during program execution. Python has dynamic typing. Powerful, but be careful.