1번
a = 10
def test1():
b = 20
def test2():
print(a)
print(b)
test1()
test2()
# 실행이 잘 될까요? 아니요
# 실행이 되지 않는다면 어떤 에러가 발생할까요? b는 지역변수이기 때문에 다른 함수에서 불러오지 못한다
* 지역변수를 전역변수처럼 사용하고 싶다면? - global 키워드를 사용해 선언
2번
mutable (수정 가능한 객체)
- list
- dict
immutable (수정 불가능한 객체)
- int, float
- str, tuple
3번
def get_even_numbers(numbers):
result = [x for x in range(1,10001) if x % 2 == 0]
return result
def get_some_numbers(numbers):
result = [10 * x for x in range(1,10001) if x % 3 == 0 and x % 15 != 0]
return result
def main():
numbers = [] # 1 ~ 10000 # 사용 안함 ㅋㅋ
even_numbers = get_even_numbers(numbers)
some_numbers = get_some_numbers(numbers)
print(even_numbers) # [2, 4, 6, ...]
print(some_numbers) # [30, 60, 90, 120, 180, ...]
main()
result를 건드리지 않고 문제에서 요구하는대로 써보자면
def get_even_numbers(numbers):
result = []
#some code
filter_nums = list(filter(lambda x: x % 2 == 0, numbers))
for i in filter_nums:
result.append(i)
return result
def get_some_numbers(numbers):
result = []
#some code
filter_nums = list(filter(lambda x: x % 3 == 0 and x % 15 != 0, numbers))
for i in filter_nums:
result.append(10 * i)
return result
def main():
numbers = list(range(1,10001)) # 1 ~ 10000
even_numbers = get_even_numbers(numbers)
some_numbers = get_some_numbers(numbers)
print(even_numbers) # [2, 4, 6, ...]
print(some_numbers) # [30, 60, 90, 120, 180, ...]
main()
요렇게도 가능
map을 활용해서 써보자면
def get_even_numbers(numbers):
result = []
#some code
result = list(filter(lambda x: x % 2 == 0, numbers))
return result
def get_some_numbers(numbers):
result = []
#some code
result = list(filter(lambda x: x % 3 == 0 and x % 15 != 0, numbers))
result = list(map(lambda x: x*10, result))
return result
def main():
numbers = list(range(1,10001)) # 1 ~ 10000
even_numbers = get_even_numbers(numbers)
some_numbers = get_some_numbers(numbers)
print(even_numbers) # [2, 4, 6, ...]
print(some_numbers) # [30, 60, 90, 120, 180, ...]
main()
굳이 반복문 안돌려도 된다
4번
from pprint import pprint
users = [
{"name": "Ronald", "age": 30, "math_score": 93, "science_score": 65, "english_score": 93, "social_score": 92},
{"name": "Amelia", "age": 24, "math_score": 88, "science_score": 52, "english_score": 78, "social_score": 91},
{"name": "Nathaniel", "age": 28, "math_score": 48, "science_score": 40, "english_score": 49, "social_score": 91},
{"name": "Sally", "age": 29, "math_score": 100, "science_score": 69, "english_score": 67, "social_score": 82},
{"name": "Alexander", "age": 30, "math_score": 69, "science_score": 52, "english_score": 98, "social_score": 44},
{"name": "Madge", "age": 22, "math_score": 52, "science_score": 63, "english_score": 54, "social_score": 47},
{"name": "Trevor", "age": 23, "math_score": 89, "science_score": 88, "english_score": 69, "social_score": 93},
{"name": "Andre", "age": 23, "math_score": 50, "science_score": 56, "english_score": 99, "social_score": 54},
{"name": "Rodney", "age": 16, "math_score": 66, "science_score": 55, "english_score": 58, "social_score": 43},
{"name": "Raymond", "age": 26, "math_score": 49, "science_score": 55, "english_score": 95, "social_score": 82},
{"name": "Scott", "age": 15, "math_score": 85, "science_score": 92, "english_score": 56, "social_score": 85},
{"name": "Jeanette", "age": 28, "math_score": 48, "science_score": 65, "english_score": 77, "social_score": 94},
{"name": "Sallie", "age": 25, "math_score": 42, "science_score": 72, "english_score": 95, "social_score": 44},
{"name": "Richard", "age": 21, "math_score": 71, "science_score": 95, "english_score": 61, "social_score": 59},
{"name": "Callie", "age": 15, "math_score": 98, "science_score": 50, "english_score": 100, "social_score": 74},
]
# some code
users.sort(key = lambda x: (x["math_score"] + x["science_score"] + x["english_score"] + x["social_score"]) / 4, reverse=True)
pprint(users, width=300, sort_dicts=False)
"""
[{'name': 'Ronald', 'age': 30, 'math_score': 93, 'science_score': 65, 'english_score': 93, 'social_score': 92},
{'name': 'Trevor', 'age': 23, 'math_score': 89, 'science_score': 88, 'english_score': 69, 'social_score': 93},
{'name': 'Callie', 'age': 15, 'math_score': 98, 'science_score': 50, 'english_score': 100, 'social_score': 74},
{'name': 'Sally', 'age': 29, 'math_score': 100, 'science_score': 69, 'english_score': 67, 'social_score': 82},
{'name': 'Scott', 'age': 15, 'math_score': 85, 'science_score': 92, 'english_score': 56, 'social_score': 85},
{'name': 'Amelia', 'age': 24, 'math_score': 88, 'science_score': 52, 'english_score': 78, 'social_score': 91},
{'name': 'Richard', 'age': 21, 'math_score': 71, 'science_score': 95, 'english_score': 61, 'social_score': 59},
{'name': 'Jeanette', 'age': 28, 'math_score': 48, 'science_score': 65, 'english_score': 77, 'social_score': 94},
{'name': 'Raymond', 'age': 26, 'math_score': 49, 'science_score': 55, 'english_score': 95, 'social_score': 82},
{'name': 'Alexander', 'age': 30, 'math_score': 69, 'science_score': 52, 'english_score': 98, 'social_score': 44},
{'name': 'Andre', 'age': 23, 'math_score': 50, 'science_score': 56, 'english_score': 99, 'social_score': 54},
{'name': 'Sallie', 'age': 25, 'math_score': 42, 'science_score': 72, 'english_score': 95, 'social_score': 44},
{'name': 'Nathaniel', 'age': 28, 'math_score': 48, 'science_score': 40, 'english_score': 49, 'social_score': 91},
{'name': 'Rodney', 'age': 16, 'math_score': 66, 'science_score': 55, 'english_score': 58, 'social_score': 43},
{'name': 'Madge', 'age': 22, 'math_score': 52, 'science_score': 63, 'english_score': 54, 'social_score': 47}]
"""
굳이 평균값을 쓸 일이 없으니까 필요없는 나누기도 버리고 모양새도 좋게 만들려면
users.sort(key = lambda x: sum([x["math_score"], x["science_score"], x["english_score"], x["social_score"]]), reverse=True)
ㅇㅇ
백준문제가 이렇게 나오면 아주 좋을것을..