Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@fault

Python3 偶数判定

pythonで偶数、奇数判定を行う

今更感満載ですが、メモ

def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False

print(is_even(0))
print(is_even(1.5))
print(is_even(2))
print(is_even(3))

True
False
True
False

コメントで指摘いただいているように、もっと簡素化することもできますね!


def is_even(num):
    return num % 2 == 0

range関数で0から9までの数が偶数か調べる


for i in range(10):
    print('{}.is_even:{}'.format(i, is_even(i)))

0.is_even:True
1.is_even:False
2.is_even:True
3.is_even:False
4.is_even:True
5.is_even:False
6.is_even:True
7.is_even:False
8.is_even:True
9.is_even:False

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?