a = "Life is too short, you need python"
if "wife" in a: print("wife")
elif "python" in a and "you" not in a: print("python")
elif "shirt" not in a: print("shirt")
elif "need" in a: print("need")
else: print("none")
#예상결과: shirt
실행결과
PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe c:/doit/marks2.py
shirt
result = 0
i = 1
while i <= 1000:
if i % 3 == 0:
result += i
i += 1
print(result)
실행결과
PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe c:/doit/marks2.py
166833
*
**
***
****
*****
i = 0
while True:
i += 1
if i > 5: break
print('*' * i)
실행결과
PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe c:/doit/marks2.py
*
**
***
****
*****
>>> for i in range(1,101):
... print(i)
실행결과
1
2
3
...
98
99
100
>>> for i in range(100):
... print(i+1, end=' ')
...
실행결과
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
# 시험 점수
[70, 60, 55, 75, 95, 90, 80, 80, 85, 100]
for 문을 사용하여 A학급의 평균 점수를 구해 보자.
A = [70, 60, 55, 75, 95, 90, 80, 80, 85, 100]
total = 0
for score in A:
total += score
average = total / len(A)
print(average)
실행결과
PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe c:/doit/marks2.py
79.0
numbers = [1,2,3,4,5]
result = []
for n in numbers:
if n % 2 == 1 :
result.append(n*2)
print(result)
실행결과
PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe c:/doit/marks2.py
[2, 6, 10]
위 코드를 리스트 내포(list comprehesion)를 사용하여 표현해 보자.
# 리스트 내포 사용
numbers = [1,2,3,4,5]
result = [number * 2 for number in numbers if number % 2 == 1]
print(result)
실행결과
PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe c:/doit/marks2.py
[2, 6, 10]