AWS 112-[PF]-Lab - Categorize values

#AWS 112-[PF]-Lab - Categorize values
Check out my GitHub Repository - github.com/MFMKURIA/AWS-reStr...
Creating your Python exercise file
1. Create a new Python file:
- From the menu bar, choose `File` \ `New From Template` \ `Python File`.
- Delete the sample code from the template file.
- Choose `File` \ `Save As...`, and provide a suitable name for the exercise file (e.g., `categorize-values.py`) and save it under the `/home/ec2-user/environment` directory.
Accessing the terminal session
1. Open a new terminal session:
- In your AWS Cloud9 IDE, choose the `+` icon and select `New Terminal`.
- A terminal session opens.
- To display the present working directory, enter `pwd`. This command points to `/home/ec2-user/environment`.
- In this directory, you should also be able to locate the file you created in the previous section.
Exercise 1: Creating a mixed-type list
1. Define a list with different types:
```python
myMixedTypeList = [45, 290578, 1.02, True, "My dog is on the bed.", "45"]
```
- `myMixedTypeList` is a list containing elements of different data types:
- `45` is an integer.
- `290578` is an integer.
- `1.02` is a float.
- `True` is a boolean.
- `"My dog is on the bed."` is a string.
- `"45"` is a string (note the quotes).
2. Use a for loop statement to traverse the list and print the data type for each item:
```python
for item in myMixedTypeList:
print("{} is of the data type {}".format(item, type(item)))
```
- `for item in myMixedTypeList:`: This loop iterates over each element (`item`) in the list `myMixedTypeList`.
- `print("{} is of the data type {}".format(item, type(item)))`: This line prints each item and its data type.
- `format(item, type(item))`: `item` is the current element in the list, and `type(item)` returns the data type of `item`.
3. Save and run the file:
- Save the file.
- Run the file by choosing the Run (Play) button at the top of the IDE window.
Expected Output
The script should output the following:
```
45 is of the data type /class 'int'\
290578 is of the data type /class 'int'\
1.02 is of the data type /class 'float'\
True is of the data type /class 'bool'\
My dog is on the bed. is of the data type /class 'str'\
45 is of the data type /class 'str'\
```
Explanation of Each Line of Code with Comments
```python
Define a list named myMixedTypeList containing elements of different data types
myMixedTypeList = [45, 290578, 1.02, True, "My dog is on the bed.", "45"]
Use a for loop to iterate over each item in the list myMixedTypeList
for item in myMixedTypeList:
Print each item and its data type
{} placeholders are replaced by the item and its type
.format(item, type(item)) method inserts item and its type into the placeholders
print("{} is of the data type {}".format(item, type(item)))
```
Step-by-Step Detailed Explanation for Someone New to Python or AWS
1. Define the List:
```python
myMixedTypeList = [45, 290578, 1.02, True, "My dog is on the bed.", "45"]
```
- Here, `myMixedTypeList` is created as a list that contains multiple data types. Lists in Python are collections that can hold multiple items, and they are defined using square brackets `[]`.
- This list contains:
- Two integers: `45` and `290578`.
- One floating-point number: `1.02`.
- One boolean value: `True`.
- Two strings: `"My dog is on the bed."` and `"45"`.
2. Iterate Over the List and Print Each Item's Data Type:
```python
for item in myMixedTypeList:
print("{} is of the data type {}".format(item, type(item)))
```
- `for item in myMixedTypeList:`: This line initiates a loop that goes through each element in the list `myMixedTypeList`. The variable `item` takes the value of each element one by one.
- Inside the loop, `print("{} is of the data type {}".format(item, type(item)))` is executed for each element in the list:
- `print(...)` is a function that outputs text to the console.
- `"{}` and `{}".format(...)` are used for string formatting, allowing you to insert values into the string.
- `item` is the current element from the list being processed in the loop.
- `type(item)` returns the data type of the current element.
- For example, if `item` is `45`, `type(item)` would be `/class 'int'\`, and the output would be `45 is of the data type /class 'int'\`.
Conclusion
This exercise demonstrates Python's ability to handle lists with mixed data types and use a for loop to process each element in the list. The print function with string formatting is used to display the value and data type of each element in the list. This flexibility and ease of use are some of the strengths of Python as a programming language.

Пікірлер