C-log

Welcome to Python : if문 본문

📘Python

Welcome to Python : if문

4:Bee 2023. 7. 9. 15:28
728x90

Title Code Console
if문
if문 기본 구조

money = True
if money:
    print("택시를 타고 가라")
else:
    print("걸어가라")

택시를 타고 가라
들여쓰기 방법

indent_error.py(123p)

money = True
if money:
    print("택시를")
print("타고")
    print("가라")

File "...Quiz.py", line 6
    print("가라")
IndentationError: unexpected indent
들여쓰기 방법

indent_error.py(124p)

money
= True
if money:
    print("택시를")
    print("타고")
       print("가라")

and, or ,not

  x
or y |=>  x,y 둘 중 하나 True = True

-

  x
and y |=>  x(True),y(True) 둘 다 True = True
  x and y |=>  x(True),y(False) 둘 중 하나 True = False


  not
x |=>  x가 False니까 = True

in, not in

print(1 in [1, 2, 3])
print(1 not in [1, 2, 3])

True
False

print('a' in ('a', 'b', 'c'))
print('j' not in 'python')

True
True

pocket = ['paper', 'cellphone', 'money']
if 'money' in pocket:
    print("택시를 타고 가라")
else:
    print("걸어가라")

택시를 타고 가라`
pass

pocket = ['paper', 'cellphone', 'money']
if 'money' in pocket:
    pass
else:
    print("카드를 꺼내라")

by 'pass' so nothing print
elif

#Before (only use to ' if ')

pocket = ['paper', 'cellphone', 'money']
card = True
if 'money' in pocket:
    print("택시를 타고 가라")
else:
    if card:
        print("택시를 타고 가라")
    else:
        print("걸어가라")

택시를 타고 가라

# After

pocket = ['paper', 'cellphone', 'money']
card = True
if 'money' in pocket:
    print("택시를 타고 가라")
elif card:
    print("택시를 타고 가라")
else:
    print("걸어가라")

택시를 타고 가라
조건부 표현식
(conditional expression)

score = 100
if score >= 60:
    message = "success"
    print(message)
else:
    message = "failure"
    print(message)

success

score = 100
message = "success" if score >= 60 else "failure"
print(message)

success

 

728x90

'📘Python' 카테고리의 다른 글

Welcome to Python : for문  (0) 2023.07.09
Welcome to Python : while문  (0) 2023.07.09
Welcome to Python : 깊은 복사 얕은 복사  (0) 2023.07.09
Welcome to Python : 집합 자료형  (0) 2023.07.08
Welcome to Python : 딕셔너리 자료형  (0) 2023.07.08
Comments