0% found this document useful (0 votes)
12 views

10 Exercises on Python Variables

The document contains 10 exercises each for practicing Python variables and data types. Exercises include basic variable assignments, swapping values, string manipulation, arithmetic operations, and working with data structures like lists, tuples, dictionaries, and sets. These exercises aim to enhance understanding of variable handling and data type operations in Python.

Uploaded by

Flavien Dzogne
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

10 Exercises on Python Variables

The document contains 10 exercises each for practicing Python variables and data types. Exercises include basic variable assignments, swapping values, string manipulation, arithmetic operations, and working with data structures like lists, tuples, dictionaries, and sets. These exercises aim to enhance understanding of variable handling and data type operations in Python.

Uploaded by

Flavien Dzogne
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

10 exercises on Python variables

### 1. **Basic Variable Assignment**

- Assign the value `10` to a variable named `a` and `20` to a variable named `b`. Print the values of
`a` and `b`.

### 2. **Swapping Variables**

- Assign values `5` and `10` to variables `x` and `y`, respectively. Swap their values and print the
result.

### 3. **String Variables**

- Create a variable `name` and assign your name to it. Print a greeting using the `name` variable,
e.g., "Hello, [name]!".

### 4. **Variable Types**

- Assign different types of values (integer, float, string) to variables `num`, `price`, and `message`.
Print the types of these variables using the `type()` function.

### 5. **Concatenation of Strings**

- Create two string variables `first_name` and `last_name`, and concatenate them to form a full
name. Print the full name.

### 6. **Arithmetic Operations**

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

### 7. **Updating Variables**

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

### 8. **Boolean Variables**

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

### 10. **Variable Reassignment**

- 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!

10 exercises on Python data

### 1. **Identifying Data Types**

- Assign the following values to variables and use the `type()` function to print their data types:

- `10`

- `10.5`

- `"Hello"`

- `True`

- `[1, 2, 3]`

### 2. **Type Conversion**

- Convert the following values and print the results:

- `100` (integer) to a float. = 100.0

- `3.14` (float) to an integer. = 3

- `"25"` (string) to an integer. = 25

- `True` (boolean) to an integer. = 1

### 3. **String Manipulation**

- Create a string variable `text` with the value `"Python Programming"`. Perform the following
operations:

- Convert the string to all uppercase letters.


- Extract and print the substring `"Python"`.

- Replace `"Python"` with `"Java"` and print the result.

### 4. **List Operations**

- Create a list `numbers` with the values `[1, 2, 3, 4, 5]`. Perform the following operations:

- Append the number `6` to the list.

- Remove the number `3` from the list.

- Print the length of the list.

### 5. **Tuple vs List**

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

### 6. **Dictionary Operations**

- Create a dictionary `person` with keys `"name"`, `"age"`, and `"city"` and values `"Alice"`, `30`,
and `"New York"`, respectively. Perform the following operations:

- Add a new key-value pair `"job": "Engineer"`.

- Update the `"age"` to `31`.

- Print the value associated with the key `"city"`.

### 7. **Boolean Logic**

- 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`

### 8. **Set Operations**

- Create two sets `set1` with values `{1, 2, 3}` and `set2` with values `{3, 4, 5}`. Perform the
following operations:

- Find the union of the two sets.

- Find the intersection of the two sets.


- Find the difference between `set1` and `set2`.

### 9. **Working with Floats**

- Assign the value `3.14159` to a variable `pi`. Round `pi` to 2 decimal places and print the result.

### 10. **Handling None Type**

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

You might also like