Basic Python Programs by PST IT SOLUTIONS
Basic Python Programs by PST IT SOLUTIONS
print('Hello, World!)
num1 = 1.5
num2= 5.3
1
Output:
The sum of 1.5 and 5.3 is 6.8
num = 8
Output:
The square root of 8.000 is 2.828.
a=5
b=6
C=7
area = (s*(s-a) *(s-b) * (s-c)) * 0.5 print('the area of the triangle
is %0.2f' % area)
Output:
The area of the triangle is 14.70.
import cmath.
2
a=1
b=5
C=6
d = (b**2)-(4*a*c)
Sol1 = (-b-cmath. sqrt (d))/(2*a)
Sol2 = (-b+cmath.sqrt(d))/(2*a)
print (‘The solution are {o} and {}'.format (sol1, Sol 2))
Output :
Enter a : 1
Enter b: 5
Enter c: 6
The solution are (-3 toj) and (-2+oj)
x=5
y=10
temp = x
x=y
y = temp
Output:
The value of x after swapping : 10
The value of y after swapping:5
import random
print (random.randint(o,g))
3
Output :5
celsius 37.5
fahrenheit = (celsius*1.8) + 32.
print (%0.1f degree celsius is equal to %0.1f fahrenheit 90%
(celsius, fabrenheit)
if num > 01
Print ("positive Number")
elif num == 0:
print("zero)
else:
print "Negative, number')
4
Output: Enter a number :
12 Positive Number.
year = 2000
if(year %4) == 0:
if (year % 100) == 0:
if (year % 400)==0:
print("{0} is a leap year".format (year))
else:
print("{0} is not a leap year". format (year))
else:
print("{0} is a leap year". format (year)).
else:
print("{o} is not a leap year ".format(year)).
5
num 1 = 0
num 2 = 14
num 3 = 12
else:
largest = num 3
print("The largest number is ", largest)
num = 407
if num>1:
for i in range (2, num):
if (num % i) ==0:
print (num, "is not a prime number")
print(i, "times", num //i," is",num)
break
else:
print (num, is a prime number!
Else:
print(num," is not a prime number").
6
lower = goo.
upper = 1000
print("prime numbers between", lower, "and", upper, "are:")
for num in range (lower, upper+1):
if num>1:
for i in range (2, num):
if (num % i) == 0:
break.
else:
print (num)
Output :
num =7
num = int(input("Enter a number: "))
7
Facterial = 1
if num < 0:
num = 12
for i in range (1,11):
print(num, 'x', i, '=', num *1)
Output
12x1 = 12
12x2 = 24
12x3 = 36
12x4 = 48
12x5 = 60
12x6 = 72
12x7 = 84
12x8 = 96
12xg = 108
10x10 120.
8
count =O
if n terms <= 0:
print("Please enter a positive, integer")
elif nterms ==1:
Output:
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
9
temp // = 10
if num == sum:
print (num, "is an armstrong number")
else:
print(num, "is not an armstrong number"
Output {1:’a’,2:’c’,4:’d’}
my-list=[1],[2,3],[4,5,6,7]]
flat-list = []
for sublist in my_list:
for num in sublist:
flat-list. append (num)
print (flat-list)
num =16
if num<ㅇ:
print("Enter a positive number")
else:
10
sum =O
while (num>0):
sum + = um
Num-=1
print("The sum is ", sum).
C = 'p'
print ("The ASCII value of " + c +"""is", ord (c))
11
Break
greater + = 1
return Icm.
num1=54
num2=24
12