Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Transformer
- python practice
- GNN
- overfitting
- XOR
- Attention
- sigmoid
- machine learning
- Classificaion
- word2vec
- Python
- Self-loop attention
- abstraction
- neural net
- pytorch
- Set Transformer
- sentence embedding
- elif
- deep learning
- NLP
Archives
- Today
- Total
Research Notes
[Python Practice] Determine even/odd number 본문
<Question>
# 짝수인지 홀수인지 판단해주는 is_evenly_divisible 함수를 쓰세요.
# is_evenly_divisible는 number(수)를 파라미터로 받습니다.
# 짝수인 경우에는, 즉 number가 2로 나누어 떨어질 경우에는 True를 리턴해줍니다.
# 홀수인 경우에는, 즉 number가 2로 나누어 떨어지지 않을 경우에는 False를 리턴해줍니다.
# 함수 안에는 print문을 사용하시면 안 되고, return문을 사용하여야 합니다.
<Answer>
def is_evenly_divisible(number):
return bool(number % 2 == 0)
print(is_evenly_divisible(3))
print(is_evenly_divisible(7))
print(is_evenly_divisible(8))
<Explanation>
짝수는 수를 2로 나눴을때의 나머지가 0이고, 홀수는 수를 2로 나눴을때의 나머지가 1이다.
파이썬에서 사용되는 나머지 연산자 (%) 를 사용하여 코드를 작성하면
number % 2 == 0 (= 수를 2로 나눴을때의 나머지가 0 과 같다. ) 이 만들어진다.
또한, 결과값이 True와 False로만 만들어져야 하기 때문에 boolean을 사용하여 이를 묶어준다. 끝!

예제 결과 화면
'Programming Language > Python' 카테고리의 다른 글
[Python Practice] While Statement Example - Print Divisible Numbers (0) | 2023.07.03 |
---|---|
[Python] Control Statement: While, If/Else/Elif (0) | 2023.07.03 |
[Python Practice] Make Change Calculator using Python (0) | 2023.07.03 |
[Python Practice] Variable, Parameter (0) | 2023.07.03 |
[Python] Abstraction - Return, Global/Local, Constant Variable (0) | 2023.07.03 |