python basic program


#To find the gcd of the given number
import math;
x=10
y=20
m=math.gcd(x,y)
print(m)

10

#To find the factorial of the given number
n=10;
fact=1;
if n<0:
print("The number has no fact")
elif n==0:
print("The facrorial is zero")
else:
for i in range(1,n+1):
fact=fact*i

print(fact)

3628800

#To find the square of the given number
import math;
x=9
m=9
x=math.sqrt(9)
y=m*m
print(x)
print(y)

3.0

81

#list Add and Delete
fruits = ["banana", "apple", "banana", "mango", "cherry"]
fruits.insert(1,"jerry")
fruits.remove('banana')
print(fruits)
['jerry', 'apple', 'banana', 'mango', 'cherry']

#Lcm of two number in the python
import math
x=8
y=2
m=math.lcm(x,y)
print(m)

8

#cube of the number
import math
x=3
m=xxx
print(m)

27

To check prime number or not

num = 11
if num > 1:

for i in range(2, int(num/2)+1):

if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
11 is a prime number

pythone code to append and extend

fruits = ["apple", "mango", "cherry"]
fruits.append(["banana", "peach"])
print(fruits)
fruits = ["apple", "mango", "cherry"]
fruits.extend(["banana", "peach"])
print(fruits)

['apple', 'mango', 'cherry', ['banana', 'peach']]
['apple', 'mango', 'cherry', 'banana', 'peach']

to print the pattern

n = 5
for i in range(1,n+1):
for j in range(1, i+1):
print(j, end="")
print()

1
12
123
1234
12345

Accpet the 5 element in the program and then find out the maximum

import math
q=1
e=8
h=3
k=2
m=max(q,e,h,k)
print(m)

8

to print the pattern 1 22 33

n = 5
for i in range(1,n+1):
for j in range(1, i+1):
print(i, end="")
print()

1
22
333
4444
55555

calculate the avarage of the given number

a=10
b=23
c=34
m=(a+b+c)/2
print(m)

33.5

#13. Write a python code to calculate area of a rectangle
l=23
b=3
area=l*b

print(area)

69

#14. Write a python code to calculate Fibonacci Series
nterms=14
count=0
n1=1
n2=2
print(0);
print(1)
while count < nterms:
print(n1)
nth = n1 + n2

update values

n1 = n2
n2 = nth
count += 1

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610

#15. Write a python code to calculate mean of given 7 numbers
q=2;
w=32
e=3
r=32
t=23
y=21
u=34
m=(q+w+e+r+t+y+u)/7

print(m)

21.0

#16. Write a program in Python to produce given pattern.
n = 5
for i in range(1,n+1):
for j in range(1, i+1):
print("#", end="")
print()

#17. Write a python code to calculate Celsius to Fahrenheit (°C to °F)
conversion
celsius = 23
fa = (celsius * 1.8) + 32
print(fa)
73.4

18. Write a program in Python to produce given pattern.

#54321
#4321
#321
#21
#1
depth = 5
for i in range(depth, 0, -1):
n = i
for j in range(0, i):
print(j, end=' ')
print("\r")

0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

#19. Write a python code to calculate area of triangle
b=12
h=23
area=(b*h)/2
print(area)

138.0

#21. Write a python code to complete the following Set operations
#1. Membership 2. Indexing 3. Slicing
fruits = {"Apple", "Peach", "Mango"}
b="Mango"
if ( b not in fruits ):
print( "Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")

Line 2 - b is available in the given list

#22. Write a python code to complete the following Set operations
#1. Length 2. Concatenation
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
print(len(set1))
{'a', 1, 'c', 'b', 2, 3}
3