Write a Python function named "is_even" that takes an integer as input and returns True if the number is even, and False otherwise.
时间: 2024-03-10 08:45:18 浏览: 80
Sure! Here's the Python function you requested:
```python
def is_even(num):
if num % 2 == 0:
return True
else:
return False
```
This function takes an integer `num` as input and checks if it is even by using the modulo operator `%`. If `num` is divisible by 2 with no remainder, then it is even and the function returns `True`. Otherwise, it returns `False`.
相关问题
Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.
Sure, here's the code for the is_prime function in Python:
```
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
```
This function checks if the input integer is greater than 1 (since 1 is not a prime number), and then iterates through all integers from 2 to the square root of the input integer. If any of these integers divide the input integer evenly, then it's not a prime and the function returns False. Otherwise, the input integer is a prime and the function returns True.
阅读全文