Python programs
Python programs
import math
Explanation:
1. The program uses the math.pi constant to get the value of π (pi).
2. It defines a function circle_properties() that calculates:
o Area using the formula A=πr2A = \pi r^2A=πr2
o Perimeter (Circumference) using the formula P=2πrP = 2 \pi rP=2πr
3. The user is prompted to input the radius, and the program prints the area and perimeter,
rounded to two decimal places.
2. Python program to generate the Fibonacci series:
Explanation:
Explanation:
Example:
Explanation:
1. The function sum_of_squares(n) computes the sum of the squares of numbers from 1 to
n. It uses a generator expression inside the sum() function to compute the squares.
2. The user is prompted to input a number n which determines how many natural numbers
(starting from 1) to consider.
3. The program then calculates and prints the sum of their squares.
Example:
Output:
Explanation:
1. Input: The program asks the user to input the elements of the array, separated by spaces.
These elements are converted into a list of integers using map() and split().
2. Sum of elements: The sum() function is used to calculate the sum of the elements in the
list.
3. Output: The program prints the sum of the elements in the array.
Example:
This program calculates the sum of the elements in an array and displays it.
7. Python program to find the largest element in an array:
Explanation:
1. find_largest(arr): This function iterates through each element in the array and keeps
track of the largest number found. Initially, it assumes the first element to be the largest
and then compares it with each subsequent element.
2. User input: The program asks for the input of an array, where the user enters the
elements separated by spaces. The map and split functions are used to convert the input
into a list of integers.
3. It checks if the array is empty and handles that case by returning None.
Example:
# Input string
string = input("Enter a string to check if it's a palindrome: ")
Explanation:
1. is_palindrome(s):
o The function first removes spaces (s.replace(" ", "")) and converts the string to
lowercase (.lower()) to ensure case-insensitive and space-insensitive comparison.
o It then checks if the string is equal to its reverse (s[::-1]), which is a common
technique to reverse a string in Python.
Example:
'madam' is a palindrome.
9. Python program that stores a string in a list and prints the elements of
the list:
# Input a string
string = input("Enter a string: ")
Explanation:
Example:
This program stores each character of the string as a separate element in the list and prints the
list.
10.Python program that demonstrates how to find the length of a list,
reverse it, copy it, and clear it:
Explanation:
1. Find the length: We use the len() function to find the number of elements in the list.
2. Reverse the list: We reverse the list using slicing (lst[::-1]), which returns a new list that
is the reverse of the original.
3. Copy the list: We use the copy() method to create a shallow copy of the list.
4. Clear the list: We use the clear() method to remove all elements from the list, leaving it
empty.
Example: