1 Answers
📖 Quick Study Guide: Python Variables
- 🤔 A variable is like a container or a box where you can store information (data).
- 🏷️ In Python, you don't need to declare the type of a variable. Python figures it out automatically!
- ✍️ To assign a value to a variable, you use the equals sign (`=`). For example: `age = 12`.
- 🔤 Variable names must start with a letter or an underscore (`_`). They cannot start with a number.
- 🔢 Variable names can only contain alpha-numeric characters (A-z, 0-9) and underscores (`_`).
- 🚫 Variable names are case-sensitive. `myVar` is different from `myvar`.
- 🛑 Avoid using Python keywords (like `if`, `for`, `while`, `print`) as variable names.
- 💡 Common data types for variables include: `int` (whole numbers), `float` (decimal numbers), `str` (text), and `bool` (True/False).
📝 Practice Quiz: Python Variables
1. What is a variable in Python?
- A. A special command that makes Python run faster.
- B. A container for storing data values.
- C. A type of error message in Python.
- D. A function used to print text on the screen.
2. Which of the following is a VALID variable name in Python?
- A. `7_days`
- B. `my-name`
- C. `_score`
- D. `for`
3. How do you assign the value `10` to a variable named `x` in Python?
- A. `x == 10`
- B. `assign x = 10`
- C. `x = 10`
- D. `set x to 10`
4. Which data type would be most suitable for storing someone's age (e.g., 13)?
- A. `str`
- B. `float`
- C. `int`
- D. `bool`
5. Are Python variable names case-sensitive?
- A. No, `apple` and `Apple` are considered the same.
- B. Yes, `fruit` and `Fruit` are considered different.
- C. Only when they start with a capital letter.
- D. Only when they are used in mathematical operations.
6. What will be the value of `message` after these lines of code run?
`message = "Hello"`
`message = "World"`
- A. `"Hello"`
- B. `"World"`
- C. `"HelloWorld"`
- D. `Error`
7. Which of these is NOT a rule for naming Python variables?
- A. They must start with a letter or an underscore.
- B. They can contain spaces.
- C. They cannot be Python keywords.
- D. They can contain numbers (but not at the start).
Click to see Answers
1. B. A container for storing data values.
2. C. `_score`
3. C. `x = 10`
4. C. `int`
5. B. Yes, `fruit` and `Fruit` are considered different.
6. B. `"World"`
7. B. They can contain spaces.
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀