Python
Python 개념 및 실습 - list(3)
developers developing
2022. 9. 6. 09:00
주차장 프로그램
parking_lot = []
top, car_name = 0, "A"
# ord("A") => 65 : 아스키 코드값 돌려주는 함수
# chr(65) => A
print(ord(car_name))
print(ord(car_name) + 1)
print(chr(ord(car_name) + 1))
while True:
no = int(input("[1] 자동차 넣기 | [2] 자동차 빼기 | [3] 종료 : "))
if no <= 3:
if no == 1:
if top >= 5:
print("주차장 꽉 찼음")
else:
parking_lot.appen(car_name)
print("%c 자동차 들어감. 주차장 상태 ==> %s" % (car_name, parking_lot))
top += 1
car_name = chr(ord(car_name) + 1)
elif no == 2:
if top > 0:
out_car = parking_lot.pop()
print("%c 자동차 나감. 주차장 상태 ==> %s" % (out_car, parking_lot))
top -= 1
car_name = chr(ord(car_name) - 1)
else:
print("빠져나갈 자동차가 없음")
else:
print("프로그램 종료")
break
else:
print("번호를 확인해 주세요")
List Comprehension :한줄에 작성
# 리스트 생성 1 ~ 100
numbers = []
for n in range(1, 101):
numbers.append(n)
print(numbers)
numbers = list(range(1, 101))
print(numbers)
numbers2 = [n for n in range(1, 101)]
print(numbers2)

list1 = ["갑", "을", "병", "정"]
# 정 글자를 제외하고 출력. 지우라는건 아님
for x in list1:
if x != "정":
print(x, end=" ")
print()
print([x for x in list1 if x != "정"])

# 1 ~ 100 숫자 중에서 홀수만 출력
list1 = [n for n in range(1, 101) if n % 2 == 1]
print(list1)

# 5글자 이상의 단어만 출력
list2 = ["nice", "study", "python", "anaconda", "abe"]
print([word for word in list2 if len(word) >= 5])

# 소문자만 출력
list3 = ["A", "b", "C", "D", "e", "z"]
for x in list3:
if x.islower():
print(x)
print([x for x in list3 if x.islower()])

# 아래 리스트를 각 요소에 *2 한 후 출력
list4 = [1, 2, 3, 4]
print([x * 2 for x in list4])
print([x * 2 for x in range(5)])
print([x * x for x in range(5)]) # 지수의 개념