0% found this document useful (0 votes)
6 views6 pages

Solved Programs - Python

Uploaded by

panditanu4317
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
6 views6 pages

Solved Programs - Python

Uploaded by

panditanu4317
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

1.

Calculate the Sum of Numbers in a List

# Define a list of numbers

numbers = [1, 2, 3, 4, 5]

# Calculate the sum using the sum() function

total = sum(numbers)

# Print the sum

print("The sum of the numbers is:", total)

Output:

The sum of the numbers is: 15

Explanation: This code defines a list of numbers and then calculates their sum
using the built-in sum() function. Finally, it prints the total sum.

2. Count the Occurrences of an Element in a List

# Define a list of elements

my_list = [1, 2, 2, 3, 4, 2, 5]

# Count the occurrences of '2' in the list

count = my_list.count(2)

# Print the count

print("The count of '2' in the list is:", count)

Output:

The count of '2' in the list is: 3

Explanation: This code defines a list and then uses the count() method
to count the occurrences of a specific element ( 2 in this case) in the list.
Finally, it prints the count.
3. Check if a Number is Even or Odd

# Function to check if a number is even or odd

def check_even_odd(number):

if number % 2 == 0:

return "Even"

else:

return "Odd"

# Test the function with a number

num = 7

result = check_even_odd(num)

print(f"The number {num} is {result}.")

Output:

The number 7 is Odd.

Explanation: This code defines a function check_even_odd() that takes a


number as input and returns "Even" if the number is even, and "Odd"
otherwise. It then tests the function with a specific number and prints the
result.

4. Find the Maximum Element in a List

# Define a list of numbers

numbers = [10, 25, 7, 42, 99]

# Find the maximum element using the max() function

maximum = max(numbers)

# Print the maximum element

print("The maximum element in the list is:", maximum)

Output:
The maximum element in the list is: 99

Explanation: This code defines a list of numbers and then uses the built-
in max() function to find the maximum element in the list. Finally, it prints
the maximum element.

5. Reverse a String

# Function to reverse a string

def reverse_string(string):

return string[::-1]

# Test the function with a string

text = "Hello, World!"

reversed_text = reverse_string(text)

print("Reversed string:", reversed_text)

Output:

Reversed string: !dlroW ,olleH

Explanation: This code defines a function reverse_string() that takes a string


as input and returns its reverse using slicing ( [::-1]). It then tests the function
with a specific string and prints the reversed string.

1. Print "Hello, World!":

print("Hello, World!")

Output:

Hello, World!

2. Addition of two numbers:

num1 = 5

num2 = 3

sum = num1 + num2


print("The sum is:", sum)

Output:

The sum is: 8

3. Check if a number is even or odd:

num = 6

if num % 2 == 0:

print("Even")

else:

print("Odd")

Output:

Even

4. Print numbers from 1 to 5:

for i in range(1, 6):

print(i)

Output:

5. Calculate the factorial of a number:

num = 5

factorial = 1

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

factorial *= i

print("Factorial of", num, "is", factorial)


Output:

Factorial of 5 is 120

6. Reverse a string:

string = "Hello"

reverse_string = string[::-1]

print("Reversed string:", reverse_string)

Output:

Factorial of 5 is 120

6. Reverse a string:

string = "Hello"

reverse_string = string[::-1]

print("Reversed string:", reverse_string)

Output:

Reversed string: olleH

7. Find the maximum of three numbers:

num1 = 10

num2 = 20

num3 = 15

print("Maximum:", max(num1, num2, num3))

Output:

Maximum: 20

8. Check if a number is prime:

num = 17

if num > 1:
for i in range(2, num):

if (num % i) == 0:

print(num, "is not a prime number")

break

else:

print(num, "is a prime number")

else:

print(num, "is not a prime number")

Output:

17 is a prime number

9. Calculate the square of a number using a function:

def square(num):

return num * num

result = square(4)

print("Square of 4 is:", result)

Output:

Square of 4 is: 16

10. Check if a string is a palindrome:

string = "radar"

if string == string[::-1]:

print("Palindrome")

else:

print("Not a palindrome")

Output:

Palindrome

You might also like