0% found this document useful (0 votes)
46 views12 pages

Basic Python Programs by PST IT SOLUTIONS

Uploaded by

pawar.sandesh5
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)
46 views12 pages

Basic Python Programs by PST IT SOLUTIONS

Uploaded by

pawar.sandesh5
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/ 12

BASIC PYTHON PROGRAMS

1) Python Program to print Hello World!

print('Hello, World!)

Output: Hello World!

2) Add Two Numbers..

num1 = 1.5

num2= 5.3

sum = num) + num 2

print('The sum of {o} and {1} is {2} :format (num 1, num 2,


sum)).

1
Output:
The sum of 1.5 and 5.3 is 6.8

3) Find the square root.

num = 8

num-sqrt = num. 0.5

print(The square root of %0.3f is 6.3f' % (num, num-sqrt))

Output:
The square root of 8.000 is 2.828.

4) calculate the area of a thangle.

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.

5) solve quadratic equation.

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)

6) Swap two variables.

x=5
y=10
temp = x
x=y
y = temp

print('The value of x after swapping {}'.format(x))


print('The value of y after swapping {}'.format(y))

Output:
The value of x after swapping : 10
The value of y after swapping:5

7) Generate a random number.

import random
print (random.randint(o,g))

3
Output :5

8) Convert kilometers to miles.

kilometers float (input("Enter Value in km: "))


conv-fac = 0.621371
Miles= kilometers *cony_fac
print(400.2f km is equal to%.2f miles'% (kilometers, miles))

Output: Enter value in km: 3.5


3.50 km is equal to 2.17 miles.

9) Convert celsius to fahrenheit

celsius 37.5
fahrenheit = (celsius*1.8) + 32.
print (%0.1f degree celsius is equal to %0.1f fahrenheit 90%
(celsius, fabrenheit)

Output : 37.5 degree celsius is equal 10 99.5 degree fahrenheit.

10) Check if a number is positive, Negatire, or o. num = float


(input("Enter a number: "))

if num > 01
Print ("positive Number")
elif num == 0:
print("zero)
else:
print "Negative, number')

4
Output: Enter a number :
12 Positive Number.

11) Check if a number is odd or even.

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


if (num % 2) == 0:
print(" {0} is Even", format (num))
else:
print("{0} is odd ". Format (num))

output Enter a number: 43


43 is odd.

12) check if a year is leap or not.

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)).

Output : 2000 is a leap year.

13) Find largest number among three.

5
num 1 = 0
num 2 = 14
num 3 = 12

IF (num)>= num 2) and (num1 >= num3):


largest = num 1

elif (num2 >=num1) and (numa >= num3):


largest num 2

else:
largest = num 3
print("The largest number is ", largest)

Output: The largest number is 14.0

14) Check if a number is prime or not.

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").

Output: 407 is not a prime number


11 times 37 is 407.

15) Print all prime number in an interval.

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 :

Prime numbers between 900 and 1000 are:


907
911
919
929
937
941
947
953
907
971
977
983
991
997

16) To find factarial of a number.

num =7
num = int(input("Enter a number: "))

7
Facterial = 1
if num < 0:

print("sorry, factorial does not exists for negative, numbers")


elif num = = 0:
print("The Factorial of o is 1")
else:
for i in range (1, num +1):
facterial = facterial *i
print("The factorial of", num, "is", factorial)

Output: The factorial of 7 is 5040.

17) Display multiplication table.

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.

18) fibonacci sequence.

nterms = int(input("How many terms?"))


n1, n2=0,1

8
count =O
if n terms <= 0:
print("Please enter a positive, integer")
elif nterms ==1:

print("fibonacci sequence upto", nterms, ".")


print (n1)
else:
print("Fibonacci sequence:") While count < nterms:
print (n1)
nth = n1+n2
n1=n2
n2 = nth
count+=1

Output:
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8

19) check Armstrong number.

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


Sum =O
temp = num
while temp>0:
digit = temp % 10
sum += digit **3

9
temp // = 10
if num == sum:
print (num, "is an armstrong number")

else:
print(num, "is not an armstrong number"

output Enter a number: 663


663 is not an armstrong number.

20) Merge two dictionaries.

dict-1 = {1: 'a', 2: 'b'}


dict-2 = $2: c', 4:11}
print(dict_1 | dict_2)

Output {1:’a’,2:’c’,4:’d’}

21) Flatten a nested list.

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)

Output [1,2, 3, 4, 5, 6, 7].

22) find sum of natural numbers.

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).

Output: The sum is 136.

23)Find numbers divisible by another number.


my-list=[12,65, 54, 39, 102, 339, 221]
result = list (filter (lambda x: (x %13 ==0), my-list))

print ("Numbers divisible by 13 are ", result)

Output: Numbers divisible by 13 are [65, 39, 221].

24) Find ASCII value of a character.

C = 'p'
print ("The ASCII value of " + c +"""is", ord (c))

Output: The ASCII value of 'p' is 112.

25) To find LCM

def compute_icm (x,y):


if x>y:
greater =x
else:
greater =y
While (True):
if ((greater %x ==0) and (greater % y ==0):
Icm = greater

11
Break
greater + = 1
return Icm.

num1=54
num2=24

print("The LCM is", compute_icm (num1, num2))

Output:The L.C.M is 216.

12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy