1 Answers
📚 Quick Study Guide: Python Variables
- ✍️ Variable Declaration & Assignment: In Python, you don't explicitly "declare" a variable type. You simply assign a value to a name, and Python dynamically determines its type. Example: `x = 10` (integer), `name = "Alice"` (string).
- 🏷️ Naming Rules: Variable names must start with a letter or an underscore (`_`). They can contain letters, numbers, and underscores. They are case-sensitive (`myVar` is different from `myvar`). Keywords (like `if`, `for`, `while`) cannot be used as variable names.
- 🔄 Dynamic Typing: Python is dynamically typed, meaning the type of a variable is determined at runtime and can change during program execution. Example: `my_var = 5` then `my_var = "hello"`.
- 🗃️ Data Types: Common types include integers (`int`), floating-point numbers (`float`), strings (`str`), booleans (`bool`), lists (`list`), tuples (`tuple`), and dictionaries (`dict`). Understanding these is crucial for correct variable usage.
- 🧠 Memory & References: Variables are essentially names (labels) that refer to objects in memory. When you assign `y = x`, both `x` and `y` might refer to the same object if the object is immutable (like numbers or strings). For mutable objects (like lists), `y = x` means both refer to the same list, so changes via `y` affect `x`.
- 🚫 Keywords: Be aware of Python's reserved keywords. You can check them using `import keyword; print(keyword.kwlist)`.
📝 Practice Quiz: Python Variables
What is the primary way to assign a value to a variable in Python?
- A) Using the `declare` keyword
- B) Using the `=` operator
- C) Using the `assign` keyword
- D) Using the `var` keyword
Which of the following is an invalid Python variable name?
- A) `_my_variable`
- B) `myVariable123`
- C) `1st_variable`
- D) `my_variable_name`
If you execute the following Python code, what will be the final value of `x`?
x = 10
x = "hello"
x = 20- A) `10`
- B) `"hello"`
- C) `20`
- D) `Error`
Python is considered a dynamically typed language. What does this mean for variables?
- A) Variable types must be declared before use.
- B) Variable types are fixed once assigned.
- C) Variable types are determined at runtime and can change.
- D) Variable types are irrelevant in Python.
Which of the following data types would be most appropriate for storing a whole number like '500' in Python?
- A) `float`
- B) `str`
- C) `bool`
- D) `int`
Consider the following code:
list1 = [1, 2, 3]
list2 = list1
list2.append(4)
print(list1)What will be the output?
- A) `[1, 2, 3]`
- B) `[1, 2, 3, 4]`
- C) `[4]`
- D) An error will occur.
Which statement about Python variable names is TRUE?
- A) They cannot contain numbers.
- B) They are case-insensitive.
- C) They can start with a number.
- D) They cannot be Python keywords.
Click to see Answers
1. B) Using the `=` operator
2. C) `1st_variable` (cannot start with a number)
3. C) `20` (variables can be reassigned to different types)
4. C) Variable types are determined at runtime and can change.
5. D) `int` (integer type for whole numbers)
6. B) `[1, 2, 3, 4]` (lists are mutable, `list1` and `list2` refer to the same object)
7. D) They cannot be Python keywords.
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! 🚀