owned this note
owned this note
Published
Linked with GitHub
# week10
- [time=Mon, Nov 11 2024]
## Note
### Lists
List is a sequence of elements.
- Operations on list are similar to string.
- We can easily append, remove element in the list, and insert element into the list at any position.
- Lists are mutable. You can change the element in the list.
```python
xy_point = [0, 1]
# we can change the first element to something else
# this means mutable
xy_point[0] = 1
print(xy_point)
```
```python
# Creating a list
idols = ["karina", "taeyon", "winter"]
# Accessing elements
print(idols[0])
print(idols[1])
# Modifying element
idols[1] = "ningning"
print(idols)
# Appeding element
idols.append("taeyon")
print(idols)
# Removing element
idols.remove("taeyon")
print(idols)
idols.append("gissle")
print(idols)
idols.append("yunjin")
print(idols)
# Removing the last one element
idols.pop()
print(idols)
# Iterating through a list
for idol in idols:
print(idol)
# List length
print(len(idols))
# List slicing
print(idols[1:3])
```
Some cool operations on list.
```python
# List comprehension
squares = [x**2 for x in range(10)]
print(squares)
# Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
```
```python
NAMES = ['jone', 'mary', 'potter']
SCORES = [40, 50, 60]
for name, score in zip(NAMES, SCORES):
print(name, score)
```
```python
NAMES = ['jone', 'mary', 'potter']
for i, name in enumerate(NAMES):
print(i, name)
```
```python
NAMES = ['jone', 'mary', 'potter']
to_find = 'jone'
if to_find in NAMES:
print(f'{to_find} is in the list.')
else:
print(f'cant find {to_find} in the list.')
```
### Tuples
Tuples are like lists, but immutable.
You can't change the element in the tuples once created.
```python
xy_point = (0, 1)
# if we want to change the first element in the tuple to something else
xy_point[0] = 1
# this will cause an error
# this means immutable
```
## Midterm Project
### Hints
- solving a real-life problem
- a simple text-based game
- adventure
- story telling
- utilizes these Python concepts:
- condition (if-else)
- loops (while, for)
- function (reusable code block)
> If you're unsure whether your idea is workable,
> feel free to discuss it with the TA!
## Homework
### list01
```python
```
### list02
```python
```
### list03
```python
```
### product01
hint: you dont need to loop through all the elements in the list!
```python
# info we need
# you need to complete the lists
product_ids = []
product_names = []
product_prices = []
# take user's input (which product)
# write your code here
# if the given id is valid
# print the product's name and price
# if not, show useful message
# write your code here
```
```python
# example
# store the information of groups in two lists
group_names = ['ive', 'asepa', 'le sserafim']
group_members = [5, 4, 5]
# ask the user to input the group name
answer = input('which group: ')
if answer in group_names:
# find the index of the given group
i = group_names.index(answer)
# use this index to find the number of members
print(f'group {answer} has {group_members[i]} member.')
else:
print('no such group')
```
### listfunc01
```python
```
### max
```python
```
### remove2min
```python
numbers = []
# ask user for 5 score inputs
# and add them both into the numbers list
# write your code here
print(numbers)
def removeMin(number_list):
'''remove one minimum number from the given list'''
# write your code here (replace the pass)
pass
removeMin(numbers)
removeMin(numbers)
print(numbers)
# show the sum of the list
# write your code here
```