10 Exercises on Python Variables
10 Exercises on Python Variables
- Assign the value `10` to a variable named `a` and `20` to a variable named `b`. Print the values of
`a` and `b`.
- Assign values `5` and `10` to variables `x` and `y`, respectively. Swap their values and print the
result.
- Create a variable `name` and assign your name to it. Print a greeting using the `name` variable,
e.g., "Hello, [name]!".
- Assign different types of values (integer, float, string) to variables `num`, `price`, and `message`.
Print the types of these variables using the `type()` function.
- Create two string variables `first_name` and `last_name`, and concatenate them to form a full
name. Print the full name.
- Assign values `15` to a variable `a` and `3` to a variable `b`. Perform addition, subtraction,
multiplication, and division using these variables and print the results.
- Create a variable `count` with an initial value of `0`. Increase the value of `count` by `1` three
times using the `+=` operator. Print the final value of `count`.
- Create a boolean variable `is_student` and assign it the value `True`. Print a message that says
"Student status: True/False" based on the value of `is_student`.
### 9. **User Input**
- Use the `input()` function to ask the user for their age and store it in a variable `age`. Print a
message that says "You are [age] years old".
- Assign a value of `100` to a variable `score`. Then, reassign the variable `score` to `200`. Print the
value of `score` before and after reassignment.
These exercises will give you a good grasp of how to work with variables in Python!
- Assign the following values to variables and use the `type()` function to print their data types:
- `10`
- `10.5`
- `"Hello"`
- `True`
- `[1, 2, 3]`
- Create a string variable `text` with the value `"Python Programming"`. Perform the following
operations:
- Create a list `numbers` with the values `[1, 2, 3, 4, 5]`. Perform the following operations:
- Create a tuple `fruits` with the values `('apple', 'banana', 'cherry')`. Try to modify one of the
elements (e.g., change `'banana'` to `'orange'`). Observe and print what happens.
- Create a dictionary `person` with keys `"name"`, `"age"`, and `"city"` and values `"Alice"`, `30`,
and `"New York"`, respectively. Perform the following operations:
- Create two boolean variables `a` and `b` with values `True` and `False`. Evaluate and print the
results of the following expressions:
- `a and b`
- `a or b`
- `not a`
- Create two sets `set1` with values `{1, 2, 3}` and `set2` with values `{3, 4, 5}`. Perform the
following operations:
- Assign the value `3.14159` to a variable `pi`. Round `pi` to 2 decimal places and print the result.
- Create a variable `empty` and assign it the value `None`. Check if `empty` is `None` using an `if`
statement and print a message confirming its type.