TIL (Today I Learned) 날짜
2023-05-24(수)
오늘 TIL 3줄 요약
공부한 내용 쓰기
in
이라는 단어를 사용하여 서로 연결(예시1)# 예시1 - List[]
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
for planet in planets:
print(planet, end=' ') # print all on same line
# 예시2 - tuple()
multiplicands = (2, 2, 2, 3, 3, 5)
product = 1
for mult in multiplicands:
product = product * mult
product
# 예시3 - isupper()
s = 'steganograpHy is the practicE of conceaLing a file, message, image, or video within another fiLe, message, image, Or video.'
msg = ''
# print all the uppercase letters in s, one at a time
for char in s:
if char.isupper():
print(char, end='')
# 예시4 - range() 작업을 5번 반복하기
for i in range(5):
print(i)
# 예시1
i = 0
while i < 10:
print(i, end=' ')
i += 1 # increase the value of i by 1
가독성이 중요합니다. 명시적인 것이 암시적인 것보다 낫습니다.
# 예시1 List comprehensions
squares = [n**2 for n in range(10)] # 제곱 List 문
# 예시1 기본문
squares = []
for n in range(10):
squares.append(n**2)
# 예시2 if문 추가
short_planets = [planet for planet in planets if len(planet) < 6]
# 예시3
# str.upper() returns an all-caps version of a string
loud_short_planets = [planet.upper() + '!' for planet in planets if len(planet) < 6]
# 예시4 - 예시3의 세줄화
[
planet.upper() + '!'
for planet in planets
if len(planet) < 6
]
# 예시5
def count_negatives(nums):
"""Return the number of negative numbers in the given list.
>>> count_negatives([5, -1, -2, 0, 3])
2
"""
n_negative = 0
for num in nums:
if num < 0:
n_negative = n_negative + 1
return n_negative
# 예시5 한줄화
def count_negatives(nums):
return len([num for num in nums if num < 0])
# 예시5 다른 한줄화
def count_negatives(nums):
# Reminder: in the "booleans and conditionals" exercises, we learned about a quirk of
# Python where it calculates something like True + True + False + True to be equal to 3.
return sum([num < 0 for num in nums])
오늘 공부한 소감은?
궁금한 내용이 있거나, 잘 이해되지 않는 내용이 있다면?
출처