: 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:44:55 浏览: 18
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 Python function named "reverse_string" that takes a string as input and returns the reverse of the input string.
Sure! Here is the Python function named "reverse_string" that takes a string as input and returns the reverse of the input string:
```python
def reverse_string(input_string):
return input_string[::-1]
```
This function uses Python slicing to reverse the input string. The `[::]` notation means to start at the end of the string and step backwards by one character until the beginning of the string is reached. This effectively reverses the string.
Question 3.2: Write a Python function named "reverse_string" that takes a string as input and returns the reverse of the input string.
Sure, here's a Python function named "reverse_string" that takes a string as input and returns the reverse of the input string:
```python
def reverse_string(s):
# Use slicing to reverse the string
return s[::-1]
```
This function takes a string `s` as input and returns the reverse of the string using slicing. The `[::-1]` notation is a slice that starts at the end of the string and ends at the beginning of the string, with a step of -1 (i.e., it goes backwards). This effectively reverses the string.
Here's an example of how to use the function:
```python
s = "hello world"
reversed_s = reverse_string(s)
print(reversed_s) # prints "dlrow olleh"
```
In this example, we pass the string "hello world" to the `reverse_string` function and assign the result to the variable `reversed_s`. We then print `reversed_s`, which should be the reverse of the input string.