AI-generated Code Suggestions Python, AI
👤 Sharing: AI
```python
# Example: A simple function to add two numbers
def add_numbers(x, y):
"""
This function adds two numbers and returns the result.
Args:
x: The first number.
y: The second number.
Returns:
The sum of x and y.
"""
return x + y
# Example usage
num1 = 5
num2 = 3
sum_result = add_numbers(num1, num2)
print(f"The sum of {num1} and {num2} is: {sum_result}")
# Example: A more complex example - a function to calculate the factorial of a number
def factorial(n):
"""
Calculates the factorial of a non-negative integer.
Args:
n: A non-negative integer.
Returns:
The factorial of n (n!), or 1 if n is 0. Returns None if n is negative.
"""
if n < 0:
return None # Factorial is not defined for negative numbers
elif n == 0:
return 1
else:
result = 1
for i in range(1, n + 1):
result *= i
return result
# Example usage of factorial function
number = 6
fact = factorial(number)
if fact is not None:
print(f"The factorial of {number} is: {fact}")
else:
print(f"Factorial is not defined for negative numbers. Input: {number}")
# Example: A function to check if a number is prime
def is_prime(n):
"""
Checks if a number is prime.
Args:
n: An integer to check for primality.
Returns:
True if n is prime, False otherwise.
"""
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Example usage of is_prime function
num_to_check = 29
if is_prime(num_to_check):
print(f"{num_to_check} is a prime number.")
else:
print(f"{num_to_check} is not a prime number.")
```
👁️ Viewed: 9
Comments