[go: up one dir, main page]

0% found this document useful (0 votes)
4 views7 pages

Faith

The document contains a series of Python code snippets that demonstrate various programming concepts, including data types, functions for mathematical operations, and input validation. It includes examples for checking even/odd numbers, calculating sums and averages, reversing strings, and finding GCDs. Additionally, it covers recursion and conditions for checking prime numbers and palindromes.

Uploaded by

meriaddislenovo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Faith

The document contains a series of Python code snippets that demonstrate various programming concepts, including data types, functions for mathematical operations, and input validation. It includes examples for checking even/odd numbers, calculating sums and averages, reversing strings, and finding GCDs. Additionally, it covers recursion and conditions for checking prime numbers and palindromes.

Uploaded by

meriaddislenovo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Answers

1. False is a boolean data type while “False” is a string scince it is under qoutation.
2.
num = int(input("Enter the number to be checked : "))
if num%2==0 :
print(f"{num} is Even")
else :
print(f"{num} is Odd")
3.
def sum_of_digits(number):
s = 0
for digit in str(number):
s += int(digit)
return s

def reverse_number(number):
sign = -1 if number < 0 else 1
number = abs(number)
reversed_num = 0
while number > 0:
digit = number % 10
reversed_num = reversed_num * 10 + digit
number //= 10
return reversed_num * sign

def is_palindrome(number):
return number == reverse_number(number)

if __name__ == "__main__":
try:
num = int(input("Enter an integer: "))

print(f"Sum of digits: {sum_of_digits(num)}")


print(f"Reversed number: {reverse_number(num)}")

if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")

except ValueError:
print("Invalid input. Please enter a valid integer.")
4.
def is_prime(number):
if number <= 1:
return False
# Check for factors from 2 up to number - 1
for i in range(2, number):
if number % i == 0:
return False
return True

if __name__ == "__main__":
try:
num = int(input("Enter an integer to check if it's prime:
"))

if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")

except ValueError:
print("Invalid input. Please enter a valid integer.")
5. 2
6. too hot
7. 200
8. 10 times
9. 5
3
1
-1
10. 20, 17, 14
11. a.
9
8
7
6
5
4
b. infinite number of times
12.
def get_sum(numbers):
return sum(numbers)

def get_average(total_sum, count):


if count == 0:
return 0.0
return total_sum / count

if __name__ == "__main__":
data = [10, 20, 30, 40, 50]
total = get_sum(data)
average = get_average(total, len(data))

print(f"Numbers: {data}")
print(f"Sum: {total}")
print(f"Average: {average}")

empty_data = []
empty_total = get_sum(empty_data)
empty_average = get_average(empty_total, len(empty_data))
print(f"\nNumbers (empty list): {empty_data}")
print(f"Sum (empty list): {empty_total}")
print(f"Average (empty list): {empty_average}")

13.
Test
Tes
Te
T

14.
def sum_natural_numbers_recursive(n):
if n < 0:
return "Please enter a non-negative integer."
if n == 0:
return 0
else:
return n + sum_natural_numbers_recursive(n - 1)

if __name__ == "__main__":
try:
num = int(input("Enter a non-negative integer (n): "))
result = sum_natural_numbers_recursive(num)
print(f"The sum of the first {num} natural numbers is:
{result}")
except ValueError:
print("Invalid input. Please enter an integer.")

15.
def power(x, y):
if y < 0:
return "Exponent must be a non-negative integer."
if y == 0:
return 1
else:
return x * power(x, y - 1)

if __name__ == "__main__":
try:
base = float(input("Enter the base (x): "))
exponent = int(input("Enter the exponent (y, a non-
negative integer): "))

result = power(base, exponent)


print(f"{base} raised to the power of {exponent} is:
{result}")

except ValueError:
print("Invalid input. Please enter valid numbers.")

16.
def find_gcd(a, b):
a = abs(a)
b = abs(b)
while b:
a, b = b, a % b
return a

if __name__ == "__main__":
try:
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
gcd_result = find_gcd(num1, num2)
print(f"The GCD of {num1} and {num2} is: {gcd_result}")
except ValueError:
print("Invalid input. Please enter valid integers.")

17.
def countdown_by_n(count_from, count_by):
while count_from >= 0:
print(count_from)
count_from -= count_by
if __name__ == "__main__":
try:
start_num = int(input("Enter the starting number for
countdown: "))
step_num = int(input("Enter the step number to count by:
"))

print("\nStarting countdown:")
countdown_by_n(start_num, step_num)

except ValueError:
print("Invalid input. Please enter valid integers.")

18.
10
1
2
3
6
1
2
3
6
1
2
3
4
7
1
2
3
4
8
7
1
2
3
4
8
19.
def near_and_far(a, b, c):
cond1 = abs(a - b) <= 1 and abs(b - c) >= 2 and abs(a - c)
>= 2
cond2 = abs(a - c) <= 1 and abs(a - b) >= 2 and abs(c - b)
>= 2
return cond1 or cond2
20.
def next_produc(n):
if n <= 0:
return "Please enter a positive integer for the sequence
index."
if n == 1:
return 2
if n == 2:
return 3
else:
return next_produc(n - 1) * next_produc(n - 2)

if __name__ == "__main__":
try:
index = int(input("Enter the position (n) in the sequence:
"))
result = next_produc(index)
print(f"The {index}th number in the sequence is:
{result}")
except ValueError:
print("Invalid input. Please enter an integer.")
21.
def reverse(s):
if len(s) <= 1:
return s
else:
return s[-1] + reverse(s[:-1])

if __name__ == "__main__":
user_input = input("Enter a string to reverse: ")
reversed_string = reverse(user_input)
print(f"The reversed string is: {reversed_string}")

You might also like