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 10:45:18 浏览: 18
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 Python function named "is_even" that takes an integer as input and returns True if the number is even, and False otherwise
Sure! Here's the Python code for the `is_even` function that takes an integer as input and returns `True` if the number is even, and `False` otherwise:
```python
def is_even(num):
if num % 2 == 0:
return True
else:
return False
```
You can call this function by passing an integer as an argument like this:
```python
print(is_even(4)) # Output: True
print(is_even(5)) # Output: False
```
In the first call to the function, the input `4` is an even number, so the function returns `True`. In the second call to the function, the input `5` is an odd number, so the function 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 Python code for the is_prime function:
```
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 takes a single integer argument `n` and returns `True` when `n` is a prime number and `False` otherwise. The function first checks if `n` is less than or equal to 1, as 1 is not a prime number. Then it loops through all the integers from 2 to the square root of `n`, checking if any of them divide `n` without a remainder. If any such integer is found, `n` is not a prime number and the function returns `False`. Otherwise, `n` is a prime number and the function returns `True`.