[Go to site: main page, start]

0% found this document useful (0 votes)
4 views5 pages

Python Functions for Math & Strings

The document contains a series of Python code snippets that perform various mathematical and data manipulation tasks. These include calculations for area and perimeter of shapes, checks for prime numbers, operations on lists, and string manipulations. Each snippet is designed to take user input and output the result of the computation or transformation.

Uploaded by

itsmehacker8053
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)
4 views5 pages

Python Functions for Math & Strings

The document contains a series of Python code snippets that perform various mathematical and data manipulation tasks. These include calculations for area and perimeter of shapes, checks for prime numbers, operations on lists, and string manipulations. Each snippet is designed to take user input and output the result of the computation or transformation.

Uploaded by

itsmehacker8053
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

1.

Area & Perimeter of Rectangle


l=float(input("Length: "))
b=float(input("Breadth: "))
print("Area =", l*b)
print("Perimeter =", 2*(l+b))

2. Hypotenuse & Area of Right Triangle


import math
b=float(input("Base: "))
h=float(input("Height: "))
print("Hypotenuse =", [Link](b*b+h*h))
print("Area =", 0.5*b*h)

3. Area of Circle
import math
r=float(input("Radius: "))
print("Area =", [Link]*r*r)

4. Celsius to Fahrenheit
c=float(input("Celsius: "))
print("Fahrenheit =", (c*9/5)+32)

5. HCF of Two Numbers


a=int(input())
b=int(input())
while b:
a,b=b,a%b
print("HCF =", a)

6. Prime Check
n=int(input())
flag=True
if n<2: flag=False
for i in range(2,int(n**0.5)+1):
if n%i==0: flag=False; break
print("Prime" if flag else "Not Prime")

7. Reverse Digits
num=int(input())
rev=0
while num>0:
rev=rev*10+(num%10)
num//=10
print("Reversed =", rev)

8. Sum of Digits
num=int(input())
s=0
while num>0:
s+=num%10
num//=10
print("Sum of digits =", s)
9. Product of Digits
num=int(input())
p=1
while num>0:
p*=num%10
num//=10
print("Product =", p)

10. Leap Year Check


y=int(input())
if (y%400==0) or (y%4==0 and y%100!=0):
print("Leap year")
else:
print("Not leap year")

11. Largest, Smallest & Largest Difference


n=int(input("How many numbers? "))
lst=[int(input()) for _ in range(n)]
print("Largest =", max(lst))
print("Smallest =", min(lst))
print("Largest Difference =", max(lst)-min(lst))

12. Sum of First n Natural Numbers


n=int(input())
print("Sum =", n*(n+1)//2)

13. Sum & Average of n Numbers


n=int(input())
lst=[int(input()) for _ in range(n)]
print("Sum =", sum(lst))
print("Average =", sum(lst)/n)

14. Palindrome String


s=input()
print("Palindrome" if s==s[::-1] else "Not Palindrome")

15. Number Pyramid Pattern


n=9
for i in range(1,n+1):
print("".join(str(x) for x in range(1,i+1)) + "".join(str(x) for x in range(i-1,0,-1)))

16. Star Pyramid Pattern


n=9
for i in range(1,n+1):
print("*"*i + "*"*(i-1))

17. Multiplication Table


n=int(input())
for i in range(1,n+1):
for j in range(1,11):
print(i,"x",j,"=",i*j)
print()
18. Roots of Quadratic Equation
import math
a=float(input("a: "))
b=float(input("b: "))
c=float(input("c: "))
d=b*b-4*a*c
if d>=0:
r1=(-b+[Link](d))/(2*a)
r2=(-[Link](d))/(2*a)
print("Roots =", r1, r2)
else:
print("Complex roots")

19. Percentage & Grade


marks=[int(input()) for _ in range(5)]
t=sum(marks)
p=t/5
print("Total =", t)
print("Percentage =", p)
if p>=75: print("Distinction")
elif p>=60: print("First Division")
elif p>=50: print("Second Division")
elif p>=40: print("Third Division")
else: print("Fail")

20. Factorial & nCr


def fact(n):
f=1
for i in range(1,n+1): f*=i
return f
n=int(input())
r=int(input())
print("nCr =", fact(n)//(fact(r)*fact(n-r)))

21. Vowel & Consonant Count


s=input().lower()
vowels="aeiou"
v=c=0
for ch in s:
if [Link]():
if ch in vowels: v+=1
else: c+=1
print("Vowels =", v, "Consonants =", c)

22. Frequency of a Letter


s=input()
ch=input("Letter: ")
print("Frequency =", [Link](ch))

23. Remove All Occurrences of a Letter


s=input()
ch=input("Letter: ")
print([Link](ch, ""))

24. Capitalize All Vowels


s=input()
vowels="aeiou"
out=""
for ch in s:
out+=[Link]() if [Link]() in vowels else ch
print(out)

25. Bubble Sort


lst=list(map(int,input("List: ").split()))
n=len(lst)
for i in range(n):
for j in range(0,n-i-1):
if lst[j]>lst[j+1]:
lst[j],lst[j+1]=lst[j+1],lst[j]
print("Sorted list =", lst)

26. Slice Operations


lst=[1,2,3,4,5,6,7,8,9]
print(lst[:3])
print(lst[3:])
print(lst[::2])
print(lst[::-1])

27. Deletion Using del


lst=[1,2,3,4,5,6]
del lst[2]
print(lst)
del lst[1:3]
print(lst)

28. List Methods Demo


lst=[1,2,3]
[Link](4)
print(lst)
print([Link](2))
print([Link](3))
[Link](1,9)
print(lst)
[Link]()
[Link](9)
[Link]()
[Link]()
print(lst)

29. Sum & Mean of List


lst=list(map(int,input().split()))
print("Sum =", sum(lst))
print("Mean =", sum(lst)/len(lst))

30. List Comprehension (divisible by 2 or 4)


lst=[x for x in range(1,51) if x%2==0 or x%4==0]
print(lst)

31. Remove Duplicates


lst=list(map(int,input().split()))
print(list([Link](lst)))

You might also like