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

Day1_programs (1)

The document contains a series of Python programming exercises that cover basic concepts such as printing messages, variable manipulation, user input, control structures, and functions. Each exercise includes code snippets demonstrating how to perform tasks like calculating sums, checking for even/odd numbers, and finding palindromes. The exercises are designed to help learners practice and understand fundamental programming skills.

Uploaded by

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

Day1_programs (1)

The document contains a series of Python programming exercises that cover basic concepts such as printing messages, variable manipulation, user input, control structures, and functions. Each exercise includes code snippets demonstrating how to perform tasks like calculating sums, checking for even/odd numbers, and finding palindromes. The exercises are designed to help learners practice and understand fundamental programming skills.

Uploaded by

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

1. Write a program to print "Hello, World!

"

print("Hello, World!")

2. Declare a variable and print its value.

name = "Alice"print(name)

3. Write a program to calculate the sum of


two numbers.

a = 10
b = 20sum = a + bprint("Sum:", sum)

4. Write a program to find the type of a


variable.

x = 42print(type(x)) # Output: <class 'int'>

5. Convert a string into an integer.

num = "123"
converted_num = int(num)print(converted_num)

6. Write a program to find the square of a


number.

num = 5
square = num ** 2print("Square:", square)

7. Take a user’s name as input and greet


them.
name = input("Enter your name: ")print(f"Hello, {name}!")

8. Write a program to check if a number is


even or odd.

num = int(input("Enter a number: "))if num % 2 == 0:


print("Even")else:
print("Odd")

9. Write a program to find the length of a


string.

string = input("Enter a string: ")print("Length of the


string:", len(string))

10. Find the area of a circle given the radius.

radius = float(input("Enter the radius: "))


area = 3.14 * radius ** 2print("Area of circle:",
area)

11. Swap two variables without using a third


variable.

a, b = 5, 10
a, b = b, aprint("a:", a, "b:", b)

12. Write a program to print numbers from 1


to 10 using a loop.

for i in range(1, 11):


print(i)

13. Write a program to check if a number is


positive, negative, or zero.
num = int(input("Enter a number: "))if num > 0:
print("Positive")elif num < 0:
print("Negative")else:
print("Zero")

14. Write a program to reverse a string.

string = input("Enter a string: ")print("Reversed


string:", string[::-1])

15. Write a function to calculate the factorial


of a number.

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5)) # Output: 120

16. Write a program to find the largest of


three numbers.

a, b, c = 5, 12, 7
largest = max(a, b, c)print("Largest:", largest)

17. Write a program to check if a number is a


palindrome.

num = input("Enter a number: ")if num == num[::-1]:


print("Palindrome")else:
print("Not a palindrome")
18. Write a program to count vowels in a
string.

string = input("Enter a string: ")


vowels = "aeiouAEIOU"
count = sum(1 for char in string if char in
vowels)print("Vowel count:", count)

19. Write a program to print Fibonacci series


up to n terms.

n = int(input("Enter number of terms: "))


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

20. Write a program to check if a number is


prime.

def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
print(is_prime(7)) # Output: True

21. Write a program to calculate the sum of


digits of a number.

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


sum_of_digits = sum(int(digit) for digit in
str(num))print("Sum of digits:", sum_of_digits)
22. Write a program to count the number of
words in a string.

string = input("Enter a string: ")


word_count = len(string.split())print("Word
count:", word_count)

23. Write a program to calculate the average


of numbers in a list.

numbers = [10, 20, 30, 40, 50]


average = sum(numbers) /
len(numbers)print("Average:", average)

24. Write a program to find the smallest


number in a list.

numbers = [10, 20, 5, 7, 30]


smallest = min(numbers)print("Smallest number:",
smallest)

25. Write a function to check if a string is a


palindrome.

def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("madam")) # Output: True

26. Write a program to merge two lists.

list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged = list1 + list2print(merged)
27. Write a program to find the sum of
numbers in a list.

numbers = [10, 20, 30]


total = sum(numbers)print("Sum:", total)

28. Write a program to remove duplicates


from a list.

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers =
list(set(numbers))print(unique_numbers)

29. Write a program to sort a list in


ascending order.

numbers = [5, 2, 9, 1, 7]
numbers.sort()print("Sorted list:", numbers)

30. Write a program to check if a string


contains only alphabets.

string = input("Enter a string: ")if


string.isalpha():
print("Contains only alphabets")else:
print("Contains non-alphabetic characters")
Let me know if you'd like explanations or slides for these!

You might also like