-
Hajipur, Bihar, 844101
A variable in Python is a container for storing data values.
Unlike many other programming languages, Python does not require declaring the type of variable. It automatically detects the type when a value is assigned.
In Python, variables are created as soon as you assign a value to them.
x = 5
name = "John"
You do not need to declare the type (like int
, string
, etc.).
Must start with a letter or an underscore (_
)
Cannot start with a number
Can only contain letters, numbers, and underscores
Are case-sensitive (name
and Name
are different)
✅ Valid Names:user_name
, _age
, score1
❌ Invalid Names:1score
, user-name
, class
(reserved keyword)
a = 10 # Integer
name = "Alice" # String
price = 10.99 # Float
is_valid = True # Boolean
You can also assign multiple variables in one line:
x, y, z = 1, 2, 3
Or assign the same value to multiple variables:
a = b = c = 100
Python allows dynamic typing, meaning a variable’s type can be changed after it is set.
x = 5 # integer
x = "five" # now it's a string
To manually convert types:
int("5") # Converts string to int
str(10) # Converts int to string
float("3.14") # Converts string to float
Use the type()
function:
x = "Hello"
print(type(x)) # Output: <class 'str'>
Operation | Example |
---|---|
Create a variable | x = 10 |
Multiple assignment | a, b = 1, 2 |
Same value to many vars | x = y = 50 |
Change type dynamically | x = 5 → x = "five" |
Check variable type | type(x) |
Q1. Write a Python program to create a variable and assign your name to it.
Q2. Write a Python program to assign an integer to a variable and print it.
Q3. Write a Python program to change the value of a variable from a string to a number.
Q4. Write a Python program to assign the same value to three different variables.
Q5. Write a Python program to assign multiple values to multiple variables in one line.
Q6. Write a Python program to use the type()
function to check the data type of a float variable.
Q7. Write a Python program to convert an integer to a string using the str()
function.
Q8. Write a Python program to assign a boolean value to a variable and print it.
Q9. Write a Python program to create a variable with an underscore at the beginning (e.g., _value
).
Q10. Write a Python program to try assigning a value to a variable starting with a number (observe and note the syntax error).
Q1: What is a variable in Python?
Q2: Which of the following is a valid variable name?
Q3: Are Python variable names case-sensitive?
Q4: What will happen if you assign x = "10" and later x = 10?
Q5: Which of the following is true about Python variables?
Q6: What function is used to check the data type of a variable?
Q7: What is the result of assigning a = b = c = 100?
Q8: What is dynamic typing in Python?
Q9: Which type of quotes can be used for strings in Python?
Q10: Which of these is a correct assignment in Python?