wasup

Python) 파이썬 제어문 if! 본문

IT/Python

Python) 파이썬 제어문 if!

wasupup 2021. 5. 23. 19:16
반응형

파이썬 제어문

if문

score = 90

if score >= 90:
    print('A')
elif score >= 80:
    if score >= 85:
        print('B+')
    else:
        print('B0')
elif score >= 70:
    print('C')
else:
    print('기타')if

A

 

pass

if True:
    pass

 

books = ['Java', 'Python', 'JavaScript']

if 'Python' in books:
    print('OK')
else:
    print('NO')

OK

 

books = ['Java', 'Python', 'JavaScript']

if 'Python' not in books:
    print('OK')
else:
    print('NO')

NO

 

 

if 'Python' in ('Java', 'Python', 'JavaScript'):
    print('OK')
else:
    print('NO')

OK

 

 


range, for

a = range(10)
print(a)

for i in a:
    print(i, end=", ")
print()


for i in range(10):
    print(i, end=", ")
print()

for i in range(1, 11, 2):
    print(i, end=", ")

range(0, 10)

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

1, 3, 5, 7, 9,

 

for

sum = 0
for i in range(1, 1001, 3):
    sum += i
print("1~1000까지의 합 : " , sum)

1~1000까지의 합 : 167167

 

while

i = 0
sum = 0
while i <= 100:
    sum += i
    i += 1
print(sum)

5050

 

 


function 함수선언!

def hello():
    print("Hello Function!!")

hello()

Hello Function!!

 

 

def add(a, b):
    return a+b

add(10, 20)

30

 

 

def add(a, b):
    return a, b, a+b

add(10, 20)



x, y, z = add(10, 20)
print(x, y, z)

(10, 20, 30)

10 20 30

 

 

#매개변수갯수를 모를 경우
def many_sum(*args):
    print(type(args))
    print(len(args))
    sum = 0
    for i in args:
        sum += i
    return sum

many_sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

<class 'tuple'>

10

 

55

 

 

def testfunc(**testargs):
    print(type(testargs))
    print(testargs)

testfunc(name='진주', age='45')

<class 'dict'>

{'name': '진주', 'age': '45'}

 

 

#args와 **kwargs를 같이 사용할 경우 
#순서에 맞게 전달해야 에러가 발생하지 않는다.
def mixfunc(*args, **kwargs):
    print(args, kwargs)

mixfunc(1, name="소라")
mixfunc(1,2, 3, 4, 5, 6, 7, 8, name="소진", age=44)

(1,) {'name': '소라'}

(1, 2, 3, 4, 5, 6, 7, 8) {'name': '소진', 'age': 44}

 


반응형
Comments