The document contains a series of Python programs demonstrating various functionalities such as checking if a number is even or odd, printing odd numbers, checking for palindromes, generating a pyramid pattern, checking for prime numbers, squaring list elements, calculating factorials, and printing elements divisible by 7. Each program includes the code and sample output. These examples serve as practical applications of basic programming concepts in Python.
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 ratings0% found this document useful (0 votes)
3 views2 pages
Python Holiday Homework
The document contains a series of Python programs demonstrating various functionalities such as checking if a number is even or odd, printing odd numbers, checking for palindromes, generating a pyramid pattern, checking for prime numbers, squaring list elements, calculating factorials, and printing elements divisible by 7. Each program includes the code and sample output. These examples serve as practical applications of basic programming concepts in Python.
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
You are on page 1/ 2
Python Programs and Output
1. Check if a number is even or odd
Code: num = int(input("Enter a number: ")) if num % 2 == 0: print("Even number") else: print("Odd number") Output: Enter a number: 7 Odd number
2. Print odd numbers from 1 to 50 using a for loop
Code: for i in range(1, 51): if i % 2 != 0: print(i, end=" ") Output: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
3. Check for a palindrome number
Code: num = int(input("Enter a number: ")) rev = int(str(num)[::-1]) if num == rev: print("Palindrome number") else: print("Not a palindrome") Output: Enter a number: 121 Palindrome number
4. Print a pyramid pattern using nested loops
Code: for i in range(1, 5): print("*" * i, end=" ") for j in range(1, i + 1): print(j, end="") print() Output: * 1 ** 12 *** 123 **** 1234
5. Check if a number is prime
Code: num = int(input("Enter a number: ")) if num > 1: for i in range(2, num): if num % i == 0: print("Not a prime number") break else: print("Prime number") else: print("Not a prime number") Output: Enter a number: 17 Prime number
6. Replace all list elements with their squares
Code: lst = [1, 2, 3, 4, 5] squared = [x**2 for x in lst] print("Squared list:", squared) Output: Squared list: [1, 4, 9, 16, 25]
7. Calculate and print factorial of a number
Code: num = int(input("Enter a number: ")) fact = 1 for i in range(1, num + 1): fact *= i print("Factorial:", fact) Output: Enter a number: 5 Factorial: 120
8. Print list elements divisible by 7
Code: lst = [7, 14, 21, 25, 30, 35, 49, 50] for num in lst: if num % 7 == 0: print(num, end=" ") Output: 7 14 21 35 49