0% found this document useful (0 votes)
1 views10 pages

Python Basic Programs

The document contains a collection of basic Python programs that demonstrate fundamental programming concepts such as checking for prime numbers, even/odd numbers, Fibonacci series, palindromes, and more. Each program includes a brief description, the code, and sample output. The programs cover a range of topics including mathematical operations, string manipulation, and control flow.

Uploaded by

shyambonthu7777
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
1 views10 pages

Python Basic Programs

The document contains a collection of basic Python programs that demonstrate fundamental programming concepts such as checking for prime numbers, even/odd numbers, Fibonacci series, palindromes, and more. Each program includes a brief description, the code, and sample output. The programs cover a range of topics including mathematical operations, string manipulation, and control flow.

Uploaded by

shyambonthu7777
Copyright
© © All Rights Reserved
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/ 10

Python Basic Programs

1.Prime Number:
A whole number greater than 1 that cannot be exactly divided by any whole number other
than itself and 1 (e.g. 2, 3, 5, 7, 11):
Program
num = int(input("Enter a number: "))
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
output:
Enter a number: 17
17 is a prime number
___________________________________________________________________________
2. Even number:
A Python program that checks if a number is even (divisible by 2) or odd (not divisible by 2)
using the modulus operator %.
Program
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")

output:

1
Enter a number: 20
20 is Even
___________________________________________________________________________
3.Fibonacci series
The Fibonacci series is a sequence of numbers where each number is the sum of the two
preceding ones.
The series starts with 0 and 1:
0, 1, 1, 2, 3, 5, 8, 13, ...
Program
n = int(input("Enter number of terms: "))
a, b = 0, 1
print("Fibonacci Series:")
while n > 0:
print(a, end=" ")
a, b = b, a + b
n -= 1
output:
Enter number of terms: 3
Fibonacci Series:
011
___________________________________________________________________________
4.Palindrome number :
A palindrome number is a number that reads the same backward as forward.
Examples: 121, 1331, 444, etc.
Program
num = int(input("Enter a number: "))
original = num
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit

2
num = num // 10
if original == reverse:
print(original, "is a Palindrome")
else:
print(original, "is not a Palindrome")
output
Enter a number: 121
121 is a Palindrome
Enter a number: 123
123 is not a Palindrome
___________________________________________________________________________

5.sum of digits
The sum of digits means adding each digit in a number.
Example: 123 → 1 + 2 + 3 = 6
Program
num = int(input("Enter a number: "))
sum_digits = 0
while num > 0:
digit = num % 10
sum_digits += digit
num = num // 10
print("Sum of digits:", sum_digits)
output:
Enter a number: 220
Sum of digits: 4
___________________________________________________________________________
6. Check if a Number is Positive, Negative, or Zero
Program
num = float(input("Enter a number: "))
if num > 0:

3
print("The number is Positive")
elif num < 0:
print("The number is Negative")
else:
print("The number is Zero")
output
Enter a number: -5
The number is Negative
Enter a number: 0
The number is Zero
Enter a number: 7
The number is Positive
___________________________________________________________________________
7. Swapping
Swapping means exchanging the values of two variables.
Example: If a = 5 and b = 10, after swapping → a = 10, b = 5

Program

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

b = int(input("Enter second number: "))

# Swapping using a temporary variable

temp = a

a=b

b = temp

print("After swapping:")

print("a =", a)

print("b =", b)

Output

Enter first number: 3


Enter second number: 7
After swapping:

4
a=7
b=3
___________________________________________________________________________
8. largest of three numbers
This program compares three numbers and finds which one is the greatest.
Program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
# Comparing the numbers
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print("The largest number is:", largest)
output
Enter first number: 2
Enter second number: 3
Enter third number: 4
The largest number is: 4
___________________________________________________________________________
9.Leap year
A leap year is a year that:
• is divisible by 4,
• but if it is divisible by 100, it must also be divisible by 400.
Example:
• 2000 → Leap year

5
• 1900 → Not a leap year
• 2024 → Leap year
Program
year = int(input("Enter a year: "))
# Leap year conditions
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a Leap Year")
else:
print(year, "is Not a Leap Year")

output
Enter a year: 2002
2002 is Not a Leap Year
___________________________________________________________________________
10. Factorial Number
Factorial of a number n is the product of all positive integers from 1 to n.
It is represented as n!
Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
Program
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
output
Enter a number: 5
Factorial of 5 is 120
___________________________________________________________________________

6
11. Reverse Number
Program
num = int(input("Enter a number: "))
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num //= 10
print("Reversed Number:", reverse)
output
Enter a number: 543
Reversed Number: 345
___________________________________________________________________________
12.Count Number of Digits in a Number
num = int(input("Enter a number: "))
count = 0
while num > 0:
num //= 10
count += 1
print("Number of digits:", count)

output
Enter a number: 240
Number of digits: 3
___________________________________________________________________________
13. Print Numbers from 1 to N
Program
n = int(input("Enter a number: "))
print("Numbers from 1 to", n)
for i in range(1, n + 1):

7
print(i, end=" ")
output
Enter a number: 20
Numbers from 1 to 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
___________________________________________________________________________
14. Sum of N Natural Numbers
Program
n = int(input("Enter a number: "))
total = 0
for i in range(1, n + 1):
total += i
print("Sum of first", n, "natural numbers is:", total)
output
Enter a number: 5
Sum of first 5 natural numbers is: 15
___________________________________________________________________________

15. Armstrong Number


A number that is equal to the sum of cubes of its digits (for 3-digit numbers).
Example: 153 → 1³ + 5³ + 3³ = 153
Program
num = int(input("Enter a number: "))
original = num
sum_cubes = 0
while num > 0:
digit = num % 10
sum_cubes += digit ** 3
num //= 10
if original == sum_cubes:

8
print(original, "is an Armstrong number")
else:
print(original, "is not an Armstrong number")
output:
Enter a number: 122
122 is not an Armstrong number
Enter a number: 153
153 is an Armstrong number
___________________________________________________________________________

16.Print Star Pattern (Right Angle Triangle)


Program
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
print("*" * i)
output
Enter number of rows: 6
*
**
***
****
*****
******
___________________________________________________________________________
17.ASCII value of a character

• ASCII stands for American Standard Code for Information Interchange.


• It is a character encoding standard that assigns a number to each character, so
computers can understand and store text.
• ord stands for "ordinal", which refers to the position or numeric value of a character
in the ASCII or Unicode table.

9
Program
ch = input("Enter a character: ")
print("ASCII value of", ch, "is", ord(ch))
output
Enter a character: a
ASCII value of a is 97
Enter a character: a
ASCII value of a is 97
___________________________________________________________________________
18.Vowel or Consonant Checker
Program
ch = input("Enter a single alphabet: ")
# Check if it's an alphabet
if ch.isalpha() and len(ch) == 1:
if ch.lower() in ['a', 'e', 'i', 'o', 'u']:
print(ch, "is a Vowel")
else:
print(ch, "is a Consonant")
else:
print("Invalid input! Please enter a single alphabet.")
output
Enter a single alphabet: a
a is a Vowel
Enter a single alphabet: b
b is a Consonant

10

You might also like