티스토리 뷰
728x90
반응형
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
# 동일성 체크
car = 'bmw'
print(car == 'bmw')
car2 = 'audi'
print(car2 == 'bmw')
# 일치하는지 체크할때 대소문자 무시하기
car_name1 = 'Audi'
print(car_name1 == 'audi')
print(car_name1.lower() == 'audi')
# 불일치 체크
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies")
# 숫자 비교
age = 18
print(age == 18)
answer = 17
if answer != 42:
print("That is not the correct answer. Please try again!")
# 여러조건 체크 - 모두 만족해야 하는 and 체크
age_0 = 22
age_1 = 18
age_2 = 22
# 각 테스를 괄호로 감싸 가독성을 올릴 수 있지만 필수는 아님.
print(age_0 >= 21 and age_1 >= 21)
print(age_0 >= 21 and age_2 >= 21)
# 하나만 만족해도 되는 or 체크하기
print(age_0 >= 21 or age_1 >= 21)
print(age_0 <= 21 or age_2 <= 21)
# 값이 리스트에 있는지 체크하기 - 특정 값이 리스트에 이미 존재하는지 체크할 때는 키워드 in을 쓴다.
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
# 값이 있는지 없는지 체크하기
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title()+", you can post a response if you wish.")
# if 문 스타일
# 비교연산자 주위에 공백을 하나 쓰는 것이 좋다.
# good style
# if age < 4:
# bad style
# if age<4:
# if - elif - else문
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print('your admission cost is $'+str(price)+'.')
# elif 블록은 필요한 만큼 쓸수 있다.
# if-elif 문 뒤에 else 블록을 꼭 사용할 필요는 없다.
# else블록 대신에 elif문으로 하나 더 테스트 하는 편이 원하는 조건을 더 찾기 쉬울 수 있다.
# 여러 조건 테스트하기
# if - elif -else 문은 통과 조건이 단 하나일 때만 어울린다. 파이썬은 한 가지 테스트가 통과하는 즉시 다른 테스트는 모두 건너뛴다.
# 원하는 모든 조건을 체크해야 할때는 단순한 if 문을 여러 개를 써야 한다.
# 리스트에서 if문 사용하기
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print('Sorry we are out of green peppers right now')
else:
print('Adding '+requested_topping+'.')
# 리스트가 비어 있지 않은지 확인하기
requested_toppings2 = []
# 리스트 이름을 if문에 사용하면 리스트에 항목이 최소 하나 이상 있을때는 True를 반환.
if requested_toppings2:
for requested_topping in requested_toppings2:
print('Adding '+requested_topping)
else:
print('Are you sure you want a plain pizza?')
728x90
반응형
'Programming language > Python' 카테고리의 다른 글
파이썬 문법 - 사용자 입력 및 while (0) | 2018.07.17 |
---|---|
파이썬 문법5 - 딕셔너리 (0) | 2018.07.10 |
파이썬 문법3-2 / 리스트 (0) | 2018.07.06 |
파이썬 문법3-1 / 리스트 (0) | 2018.07.05 |
파이썬 문법2 / 문자열 (0) | 2018.07.05 |
댓글
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 리프래시토큰
- 반복문
- git
- CSS
- IntrinsicElements
- svg모션
- RefreshToken
- 아이콘 폰트 만들기
- for of 구문
- anime.js
- vue-router
- svg 폰트
- svg icon font
- 태그
- 앵귤러
- icon font
- interceptors
- Vue3
- cordova
- 자바스크립트
- Intrinsic
- 코도바
- Angular
- JsDoc
- react-router-dom
- Aptana
- 내장요소
- react
- React.StrictMode
- git checkout -b
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함