Skip to content
Snippets Groups Projects
Commit 0f218300 authored by William Dos Santos's avatar William Dos Santos
Browse files

Added lecture and ended ex7

parent 377a176f
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:41ad2b77 tags:
### 1.1 Namespace basic
Create a function that contains a nested function.
For each space (Global, outer-, and inner Function) define the variable a with different values e.g., 1,2,3 <br>
Show how the namespaces are separated from each other by using the print function in different spaces.
call the outer function ``outer_function`` and the inner function``inner_function``
%% Cell type:code id:aa5b4c1f tags:
``` python
a=1
def outer_function():
a = 2
def inner_function():
a = 3
print("inner_function", a)
inner_function()
print("outer_function", a)
outer_function()
print("global", a)
```
%% Output
inner_function 3
outer_function 2
global 1
%% Cell type:markdown id:2cf2bba7 tags:
### 2 Data collections
Know the differences and possible uses of List, Tuple and Dictionary
#### Differences and possible uses
**List**:
- **Declaration**: `list_name = [element1, element2, ...]`
- **Properties**: Ordered, changeable, allows duplicates
- **Possible uses**: If you need a collection of elements that are frequently changed, such as adding or removing elements.
**Tuple**:
- **Declaration**: `tuple_name = (element1, element2, ...)`
- **Properties**: Ordered, unchangeable, allows duplicates
- **Possible uses**: If you need an immutable collection of elements, such as fixed configuration values.
**Dictionary**:
- **Declaration**: `dict_name = {key1: value1, key2: value2, ...}`
- **Properties**: Unordered, changeable, no duplicates in keys, values can be duplicates
- **Possible uses**: If you need a collection of key-value pairs, such as mapping names to phone numbers.
**set**
%% Cell type:markdown id:ad3e2e48 tags:
### 2.1 Declaration of data collections
Create variables with basic data types with the following names, which can store the corresponding value:
- the verbs swim run dance
- The GPS coordinates: 47.2286, 8.8317
- The definition of a person with name, age, canton of residence
%% Cell type:code id:9f98d24d tags:
``` python
list_verbs = ["swim", "run", "dance"]
print(list_verbs[0])
print(list_verbs[0:2]) #printet alle Angaben bis 2
print(list_verbs[0:3]) #printet alle Angaben bis 3
tuple = [47.2286, 8.8317]
print(tuple[1])
persondefinition = {
"name:" : "William",
"age:" : "26",
"canton of residence:" : "Zürich"
}
print(persondefinition["name:"])
```
%% Output
swim
['swim', 'run']
['swim', 'run', 'dance']
8.8317
William
%% Cell type:markdown id:bfc3bf9b tags:
### 2.2 Add and Remove Items from a Dictionary
Start with the following dictionary:
```python
student = {
"name": "Anna",
"age": 22,
"major": "social work"
}
```
* Add a new key "graduation_year" with the value 2025.
* Remove the "age" key from the dictionary.
* Print the updated dictionary
%% Cell type:code id:c7a26d84 tags:
``` python
student = {
"name": "Anna",
"age": 22,
"major": "social work"
}
student.update({"graduation_year" : "2050"})
student.pop("age")
print(student)
```
%% Output
{'name': 'Anna', 'major': 'social work', 'graduation_year': '2050'}
%% Cell type:markdown id:93970c5b tags:
### 2.3 Loop Through a Dictionary
Given the dictionary:
```python
movie = {
"title": "Inception",
"director": "Christopher Nolan",
"release_year": 2010
}
```
Write a Python program that loops through the dictionary and prints both the keys and their corresponding values.
%% Cell type:code id:61004b7b tags:
``` python
movie = {
"title": "Inception",
"director": "Christopher Nolan",
"release_year": 2010
}
for key, value in movie.items():
print(key, ": ", value)
```
%% Output
title : Inception
director : Christopher Nolan
release_year : 2010
%% Cell type:markdown id:6b25596c tags:
### 2.4 Checking the availability of screws in the warehouse
Define a ``screw_warehouse_manager`` function that checks whether a particular screw exists in the warehouse and whether it is in stock.
### Task
1. **Dictionary**
- Given is a dictionary 'screw warehouse' that contains different screw types and their available quantities.
2. **Implement function**
- Implement the function `Screw_warehouse_manager(screw)`, which performs the following checks:
- If the screw is available in stock and the quantity is greater than 0, the function returns `“in stock”`.
- If the screw is in stock but the quantity is 0, the function returns “out of stock”.
- If the screw is not in stock, the function returns `“does not exist”`.
%% Cell type:code id:1b33ffc5 tags:
``` python
screw_warehouse = {
"M5": 12,
"M3": 0,
"M2.5": 6,
"M2": 20,
}
def screw_warehouse_manager(screw):
if screw in screw_warehouse:
if screw_warehouse[screw] > 0:
return "in Stock" #falls hier mit print gearbeitet wird, wird als antwort die Antwort + "None" gedruckt
else:
return "out of stock"
else:
return "does not exist"
screw_name = input("Which screw?").upper() #upper ersetzt alle klein gedruckte Buchstaben durch gross gedruckte
print(screw_name)
print(screw_warehouse_manager(screw_name))
```
%% Output
M1
does not exist
%% Cell type:markdown id:0fbb59ec tags:
### 2.5 Output of component information from a BOM
Given a BOM with various machine elements, write a Python script that outputs the component information in the following form
The component _______ has the specification: ______ and is made of ________.
%% Cell type:code id:6a7edd07 tags:
``` python
bom_list = [
["Screw", "M6x30", "42CrMo4"],
["Nut", "M6", "42CrMo4"],
["Washer", "M6", "Galvanised steel"],
["Bearing", "6204", "1.3505"],
["Shaft", "Ø20x1000", "C45"],
]
```
%% Cell type:code id:a61db0cf tags:
``` python
for bom in bom_list()
print("The component",bom_list(0)+" has the specification:")
```
%% Cell type:markdown id:5fbc12e7 tags:
Now solve the same exercise this time with a dictionary <br>
**TIP:** With the ``.items()`` method you can access the key and the value of the dictionary at the same time
%% Cell type:code id:758e3e09 tags:
``` python
bom_dict = {
"Screw": ["M6x30", "42CrMo4"],
"Nut": ["M6", "42CrMo4"],
"Washer": ["M6", "Stahl verzinkt"],
"Bearing": ["6204", "1.3505"],
"Shaft": ["Ø20x1000", "C45"],
}
```
%% Cell type:code id:9388a373 tags:
``` python
```
%% Cell type:markdown id:3919aa62 tags:
### 2.6 Extract all numbers from a nested list into a single list
In this task, you are to write a Python script that extracts all numbers from a given matrix and saves them in a single list. <br>
**TIP**: additional elements can be added to a list with extend
```python
list.extend(element)
%% Cell type:code id:ddf65af1 tags:
``` python
matrix = [
[1, 2, 3, 4, 5, 6],
[10, 20, 30, 40, 50, 60],
[100, 200, 300, 400, 500, 600],
]
```
%% Cell type:code id:543433f2 tags:
``` python
```
......
This diff is collapsed.
{
"store": {
"name": "TechGadget Online",
"location": "New York",
"currency": "USD"
},
"products": [
{
"id": 101,
"name": "Wireless Mouse",
"brand": "Logitech",
"price": 29.99,
"stock": {
"quantity": 150,
"availability": "In Stock"
},
"categories": ["Electronics", "Accessories"],
"rating": {
"average": 4.7,
"reviews": 120
}
},
{
"id": 102,
"name": "Bluetooth Speaker",
"brand": "JBL",
"price": 89.99,
"stock": {
"quantity": 85,
"availability": "In Stock"
},
"categories": ["Electronics", "Audio"],
"rating": {
"average": 4.5,
"reviews": 90
}
},
{
"id": 103,
"name": "Gaming Keyboard",
"brand": "Razer",
"price": 119.99,
"stock": {
"quantity": 45,
"availability": "Limited Stock"
},
"categories": ["Electronics", "Accessories", "Gaming"],
"rating": {
"average": 4.8,
"reviews": 150
}
},
{
"id": 104,
"name": "4K Monitor",
"brand": "Dell",
"price": 329.99,
"stock": {
"quantity": 20,
"availability": "Limited Stock"
},
"categories": ["Electronics", "Displays"],
"rating": {
"average": 4.6,
"reviews": 200
}
}
]
}
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment