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

Python Functions for Math and Logic

The document contains a series of Python code snippets demonstrating various programming concepts such as printing natural numbers, generating Fibonacci series, finding factors, checking for prime numbers, identifying palindromes, and performing basic arithmetic operations. Each code block includes user input and outputs relevant results based on the operations performed. The examples illustrate fundamental programming techniques and logic in Python.
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 views4 pages

Python Functions for Math and Logic

The document contains a series of Python code snippets demonstrating various programming concepts such as printing natural numbers, generating Fibonacci series, finding factors, checking for prime numbers, identifying palindromes, and performing basic arithmetic operations. Each code block includes user input and outputs relevant results based on the operations performed. The examples illustrate fundamental programming techniques and logic in Python.
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

In [1]: def print_n_natural_numbers(n):

for i in range(1, n + 1):


print(i)

print_n_natural_numbers(10)

1
2
3
4
5
6
7
8
9
10

In [2]: def fibonacci_series(n):


a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

n = 10
fibonacci_series(n)

0 1 1 2 3 5 8 13 21 34

In [3]: num = int(input("Enter a number: "))


print(f"Factors of {num} are:")
for i in range(1, num + 1):
if num % i == 0:
print(i)

Factors of 5 are:
1
5

In [4]: def is_prime(num):


if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

def print_first_n_primes(n):
count = 0
num = 2
while count < n:
if is_prime(num):
print(num, end=' ')
count += 1
num += 1

n = 10
print_first_n_primes(n)

2 3 5 7 11 13 17 19 23 29

In [5]: num = input("Enter a number: ")

if num == num[::-1]:
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")

99 is a palindrome.

In [6]: # Get input from the user


num = int(input("Enter a number: "))

# Check if the number is prime


if num <= 1:
print(f"{num} is not a prime number.")
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number.")
break
else:
print(f"{num} is a prime number.")

8 is not a prime number.

In [7]: # Get input from the user


num = int(input("Enter a number: "))

# Check divisibility
if num % 7 == 0 or num % 11 == 0:
print(f"{num} is divisible by 7 or 11.")
else:
print(f"{num} is not divisible by 7 or 11.")

88 is divisible by 7 or 11.

In [8]: # Get input from the user


a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

# Find the largest number


if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print(f"The largest number is: {largest}")

The largest number is: 84.0

In [9]: # Get input from the user


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Ask user to choose an operation


print("Choose operation: + - * /")
choice = input("Enter your choice: ")

# Perform the operation based on user choice


if choice == '+':
result = num1 + num2
print(f"Result: {num1} + {num2} = {result}")
elif choice == '-':
result = num1 - num2
print(f"Result: {num1} - {num2} = {result}")
elif choice == '*':
result = num1 * num2
print(f"Result: {num1} * {num2} = {result}")
elif choice == '/':
if num2 != 0:
result = num1 / num2
print(f"Result: {num1} / {num2} = {result}")
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid operation choice.")

Choose operation: + - * /
Result: 84.0 + 75.0 = 159.0

In [10]: # Get string input from user


s = input("Enter a string: ")

# Prepare the string: remove spaces and convert to lowercase


cleaned = [Link](" ", "").lower()

# Check palindrome
if cleaned == cleaned[::-1]:
print(f'"{s}" is a palindrome.')
else:
print(f'"{s}" is not a palindrome.')

"eva" is not a palindrome.

In [11]: def is_armstrong(number):


num_str = str(number)
num_digits = len(num_str)
total = sum(int(digit) ** num_digits for digit in num_str)
return total == number
num = int(input("Enter a number: "))
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")

85 is not an Armstrong number.

In [12]: num = input("Enter a number: ")

print("The digits are:")


for digit in num:
if [Link](): # Skips negative sign or other symbols
print(digit)

The digits are:


6
5

You might also like