Write a Python program using if-else to determine whether a number is positive, negative, or zero.
# Input from user
num = float(input("Enter a number: "))
# Check conditions
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Output:
Enter a number: -3
The number is negative.
2. Write a Python program to print the first 10 Fibonacci numbers
# Initialize first two numbers
a, b = 0, 1
print("First 10 Fibonacci numbers:")
# Loop to print 10 Fibonacci numbers
for i in range(10):
print(a)
a, b = b, a + b
# Python program to print the first 10 Fibonacci numbers
# Initialize first two numbers
a, b = 0, 1
print("First 10 Fibonacci numbers:")
# Loop to print 10 Fibonacci numbers
for i in range(10):
print(a)
a, b = b, a + b
Output:
First 10 Fibonacci numbers:
0
1
1
2
3
5
8
13
21
34
Functions in Python:
Create a Python function to calculate the area of a circle
Method 1:
import math
# Function to calculate area of a circle
def area_of_circle(radius):
area = math.pi * radius ** 2
return area
# Input from user
r = float(input("Enter the radius of the circle: "))
# Call the function and display the result
area = area_of_circle(r)
print(f"The area of the circle with radius {r} is: {area:.2f}")
Output:
Enter the radius of the circle: 5
The area of the circle with radius 5.0 is: 78.54
Method 2:
# Function to calculate area of a circle using π ≈ 3.14
def area_of_circle(radius):
pi = 3.14
area = pi * radius * radius
return area
# Input from user
r = float(input("Enter the radius of the circle: "))
# Call the function and display the result
area = area_of_circle(r)
print("The area of the circle is:", round(area, 2))
Output:
Enter the radius of the circle: 4
The area of the circle is: 50.24
Create a Python function that takes a list of numbers and returns the largest number in the list
# Function to find the largest number in a list
def find_largest_number(numbers):
if not numbers:
return None # Return None if the list is empty
largest = numbers[0]
for num in numbers[1:]:
if num > largest:
largest = num
return largest
# Example usage
num_list = [23, 5, 89, 16, 42]
largest = find_largest_number(num_list)
print("The largest number in the list is:", largest)
Output:
The largest number in the list is: 89
# Function to find the largest number in a list using max()
def find_largest_number(numbers):
if not numbers:
return None # Handle empty list
return max(numbers)
# Example usage
num_list = [12, 45, 67, 89, 34]
largest = find_largest_number(num_list)
print("The largest number in the list is:", largest)
Output:
The largest number in the list is: 89
File Handling
Write a Python program to read a text file and count the number of words in it.
# Function to count words in a file
def count_words_in_file(filename):
try:
with open(filename, 'r') as file:
text = file.read()
words = text.split()
return len(words)
except FileNotFoundError:
print("The file was not found.")
return 0
# Example
file_name = input("Enter the filename (with extension): ")
word_count = count_words_in_file(file_name)
print("Total number of words in the file:", word_count)
Output:
Enter the filename (with extension): sample.txt
Total number of words in the file: 153