1. 조건문 연습, 그 return 값
# Assign True or False as appropriate on the lines below!
# 17 < 118 % 100
bool_one = (17 < 328)
# 100 == 33 * 3 + 1
bool_two = ( 100 == (2*50) )
# 19 <= 2**4
bool_three = (19 <= 19)
# -22 >= -18
bool_four = (-22 >= -18)
# 99 != 98 + 1
bool_five = ( 99 != (98 + 1) )
# 20 + -10 * 2 > 10 % 3 % 2
bool_one = (20 - 10) > 15
# (10 + 17)**2 == 3**6
bool_two = (10 + 17) == 3**16
# 1**2**3 <= -(-(-1))
bool_three = 1**2 <= -1
# 40 / 20 * 4 >= -4**2
bool_four = 40 * 4 >= -4
# 100**0.5 != 6 + 4
bool_five = 100 != 10**2
#대입 연산자 우선순위는 python에서도 많이 낮은 듯 하다.
2. boolean 연산
"""
Boolean Operators
---------------------------
True and True is True
True and False is False
False and True is False
False and False is False
True or True is True
True or False is True
False or True is True
False or False is False
Not True is False
Not False is True
"""
bool_one = False and False
# -(-(-(-2))) == -2 and 4 >= 16**0.5
bool_two = -(-(-(-2))) == -2 and 4 >= 16**0.5
# 19 % 4 != 300 / 10 / 10 and False
bool_three = 19 % 4 != 300 / 10 / 10 and False
# -(1**2) < 2**0 and 10 % 10 <= 20 - 10 * 2
bool_four = -(1**2) < 2**0 and 10 % 10 <= 20 - 10 * 2
# True and True
bool_five = True and True
# 2**3 == 108 % 100 or 'Cleese' == 'King Arthur'
bool_one = 2**3 == 108 % 100 or 'Cleese' == 'King Arthur'
# True or False
bool_two = True or False
# 100**0.5 >= 50 or False
bool_three = 100**0.5 >= 50 or False
# True or True
bool_four = True or True
# 1**100 == 100**1 or 3 * 2 * 1 != 3 + 2 + 1
bool_five = 1**100 == 100**1 or 3 * 2 * 1 != 3 + 2 + 1
# not True
bool_one = not True
# not 3**4 < 4**3
bool_two = not 3**4 < 4**3
# not 10 % 3 <= 10 % 2
bool_three = not 10 % 3 <= 10 % 2
# not 3**2 + 4**2 != 5**2
bool_four = not 3**2 + 4**2 != 5**2
# not not False
bool_five = not not False
# False or not True and True
bool_one = False or not True and True
# False and not True or True
bool_two = False and not True or True
# True and not (False or False)
bool_three = True and not (False or False)
# not not True or False and not True
bool_four = not not True or False and not True
# False or not (True and True)
bool_five = False or not (True and True)
3. if-else
response = 'Y'
answer = "Left"
if answer == "Left":
print "This is the Verbal Abuse Room, you heap of parrot droppings!"
# Will the above print statement print to the console?
# Set response to 'Y' if you think so, and 'N' if you think not.
4.
def using_control_once():
if True:
return "Success #1"
def using_control_again():
if 1>0:
return "Success #2"
print using_control_once()
print using_control_again()
5.
answer = "'Tis but a scratch!"
def black_knight():
if answer == "'Tis but a scratch!":
return True
else:
return False # Make sure this returns False
def french_soldier():
if answer == "Go away, or I shall taunt you a second time!":
return True
else:
return False # Make sure this returns False
6. if elif else (c언어 case 구문처럼 콜론으로 )
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer <5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
7.
def the_flying_circus():
if True or True: # Start coding here!
# Don't forget to indent
# the code inside this block!
return True
elif 1<0:
# Keep going here.
# You'll want to add the else statement, too!
return True
else :
return True
'Python' 카테고리의 다른 글
functions (0) | 2016.02.08 |
---|---|
Pig Latin (0) | 2016.02.08 |
datetime library (0) | 2016.02.04 |
string (0) | 2016.02.02 |
python 걸음마 시작 (0) | 2016.02.02 |