Python Variables


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.


🔑 Creating Variables

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.).


🧠 Variable Naming Rules

  • 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)


📦 Assigning Values

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

🔄 Changing Type

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

🔄 Type Casting

To manually convert types:

int("5")       # Converts string to int
str(10)        # Converts int to string
float("3.14")  # Converts string to float

🔍 Check Variable Type

Use the type() function:

x = "Hello"
print(type(x))  # Output: <class 'str'>

✅ Summary Table

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)

Practice Questions

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).


Python Variables Quiz

Q1: What is a variable in Python?

A. A fixed value
B. A function
C. A container for storing data
D. A reserved keyword

Q2: Which of the following is a valid variable name?

A. 1num
B. user-name
C. _value
D. class

Q3: Are Python variable names case-sensitive?

A. Yes
B. No
C. Only for strings
D. Only for integers

Q4: What will happen if you assign x = "10" and later x = 10?

A. Error
B. x remains a string
C. x becomes an integer
D. Python crashes

Q5: Which of the following is true about Python variables?

A. Must be declared with data types
B. Can store multiple values only
C. Change type automatically based on value
D. Always store text only

Q6: What function is used to check the data type of a variable?

A. datatype()
B. typeof()
C. type()
D. checktype()

Q7: What is the result of assigning a = b = c = 100?

A. Only a gets the value
B. a and b get the value
C. All three get the value 100
D. Syntax error

Q8: What is dynamic typing in Python?

A. Type declared during runtime
B. Type changes at compile time
C. Fixed variable types
D. A built-in function

Q9: Which type of quotes can be used for strings in Python?

A. Single '
B. Double "
C. Triple ''' or """
D. All of the above

Q10: Which of these is a correct assignment in Python?

A. let x = 5
B. x == 5
C. x = 5
D. int x = 5

Go Back Top