0% found this document useful (0 votes)
5 views9 pages

Python Basic Programs

Uploaded by

aj1472942
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)
5 views9 pages

Python Basic Programs

Uploaded by

aj1472942
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/ 9

Python Basic Programs

1. Add Two Numbers


Code:

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


b = float(input("Enter second number: "))
s=a+b
print("Sum =", s)

Example Output:
Enter first number: 5
Enter second number: 7
Sum = 12.0

2. Check Even or Odd


Code:

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


if n % 2 == 0:
print(n, "is even")
else:
print(n, "is odd")

Example Output:
Enter a number: 9
9 is odd

3. Maximum Among Three Numbers


Code:

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


b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
m=a
if b > m:
m=b
if c > m:
m=c
print("Maximum =", m)

Example Output:
Enter first number: 3
Enter second number: 9
Enter third number: 7
Maximum = 9.0

4. Area of Triangle
Code:

b = float(input("Enter base: "))


h = float(input("Enter height: "))
area = 0.5 * b * h
print("Area =", area)

Example Output:
Enter base: 10
Enter height: 5
Area = 25.0

5. Simple Interest
Code:

p = float(input("Enter principal: "))


r = float(input("Enter rate: "))
t = float(input("Enter time: "))
si = (p * r * t) / 100
print("Simple Interest =", si)

Example Output:
Enter principal: 1000
Enter rate: 5
Enter time: 2
Simple Interest = 100.0

6. Leap Year
Code:
y = int(input("Enter year: "))
if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
print(y, "is a leap year")
else:
print(y, "is not a leap year")

Reason: Rule of Leap Year

1. If a year is divisible by 4 → Leap Year


Example: 2016 ÷ 4 = 504 ✅

2. If a year is divisible by 100 → Not Leap Year


Example: 1900 ÷ 100 = 19 ❌

3. If a year is divisible by 400 → Leap Year


Example: 2000 ÷ 400 = 5 ✅

Why not only % 400?

 If we check only % 400, then 2016 would be not divisible by 400, so program will
say not leap year ❌ (wrong).

 But in reality, 2016 is a leap year because it satisfies the % 4 rule.

So, % 400 condition is needed only for century years (like 2000, 2400). For normal years
(like 2016, 2024), we must use % 4.
Example Output:
Enter year: 2024
2024 is a leap year

7. Swap Using Third Variable


Code:

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


b = float(input("Enter second number: "))
t=a
a=b
b=t
print("After swap: a =", a, ", b =", b)

Example Output:
Enter first number: 10
Enter second number: 20
After swap: a = 20.0 , b = 10.0

8. Swap Without Third Variable


Code:

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


b = float(input("Enter second number: "))
a=a+b
b=a-b
a=a-b
print("After swap: a =", a, ", b =", b)

Example Output:
Enter first number: 15
Enter second number: 25
After swap: a = 25.0 , b = 15.0

9. Positive or Negative
Code:

n = float(input("Enter a number: "))


if n > 0:
print(n, "is positive")
elif n < 0:
print(n, "is negative")
else:
print(n, "is zero")

Example Output:
Enter a number: -5
-5.0 is negative

10. Celsius to Fahrenheit


Code:

c = float(input("Enter Celsius: "))


f = (c * 9/5) + 32
print(c, "Celsius =", f, "Fahrenheit")

Example Output:
Enter Celsius: 37
37.0 Celsius = 98.6 Fahrenheit

11. Total, Percentage and Division


Code:

m1 = float(input("Enter marks 1: "))


m2 = float(input("Enter marks 2: "))
m3 = float(input("Enter marks 3: "))
m4 = float(input("Enter marks 4: "))
m5 = float(input("Enter marks 5: "))

total = m1 + m2 + m3 + m4 + m5
per = total / 5

if per >= 60:


div = "First Division"
elif per >= 45:
div = "Second Division"
elif per >= 33:
div = "Third Division"
else:
div = "Fail"

print("Total =", total)


print("Percentage =", per, "%")
print("Division =", div)

Example Output:
Enter marks: 80 75 90 85 70
Total = 400.0
Percentage = 80.0 %
Division = First Division

12. Palindrome String


Code:

s = input("Enter a string: ")


if s == s[::-1]:
print(s, "is a palindrome")
else:
print(s, "is not a palindrome")

Example Output:
Enter a string: madam
madam is a palindrome

12b. Palindrome Number


Code:

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


rev = str(n)[::-1]
if str(n) == rev:
print(n, "is a palindrome number")
else:
print(n, "is not a palindrome number")

Example Output:
Enter a number: 121
121 is a palindrome number

13. Reverse a Number


Code:
n = int(input("Enter a number: "))
rev = 0
while n > 0:
d = n % 10
rev = rev * 10 + d
n = n // 10
print("Reversed number =", rev)

Example Output:
Enter a number: 1234
Reversed number = 4321

14. Count Digits


Code:

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


cnt = 0
t = abs(n)
while t > 0:
t //= 10
cnt += 1
if n == 0:
cnt = 1
print("Digits =", cnt)

Example Output:
Enter a number: 4567
Digits = 4

15. List Operations


Code:

lst = []
lst.append(10)
lst.append(20)
lst.append(30)
print("After adding:", lst)

lst[1] = 25
print("After modifying:", lst)
del lst[0]
print("After deleting:", lst)

Example Output:
After adding: [10, 20, 30]
After modifying: [10, 25, 30]
After deleting: [25, 30]

16. Min & Max in List


Code:

nums = [3, 7, 2, 9, 4]
print("Min =", min(nums))
print("Max =", max(nums))

Example Output:
Min = 2
Max = 9

17. Tuple Operations


Code:

t = (1, 2, 3, 4, 5)
print("Index 2 element:", t[2])
print("Slice 1 to 4:", t[1:4])

Example Output:
Index 2 element: 3
Slice 1 to 4: (2, 3, 4)

18. Nested Lists and Tuples


Code:

lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


print("Nested list:", lst)
print("Element [1][2]:", lst[1][2])

tup = ((1, 2), (3, 4), (5, 6))


print("Nested tuple:", tup)
print("Element [2][1]:", tup[2][1])
Example Output:
Nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Element [1][2]: 6
Nested tuple: ((1, 2), (3, 4), (5, 6))
Element [2][1]: 6

You might also like