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

Python Basic Programs

The document provides examples of Python programs for printing text, performing basic math operations, finding square roots, calculating area of triangles, swapping variables, converting between units, checking odd/even and positive/negative numbers, finding the largest of three numbers, checking Armstrong and prime numbers, finding sums of natural numbers, finding ASCII values, calculating greatest common divisors, and making a calculator and shuffling cards.

Uploaded by

harshraj31844
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
63 views5 pages

Python Basic Programs

The document provides examples of Python programs for printing text, performing basic math operations, finding square roots, calculating area of triangles, swapping variables, converting between units, checking odd/even and positive/negative numbers, finding the largest of three numbers, checking Armstrong and prime numbers, finding sums of natural numbers, finding ASCII values, calculating greatest common divisors, and making a calculator and shuffling cards.

Uploaded by

harshraj31844
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

1. Python Program to Print Hello world!

print('Hello, world!')

2. Python Program to Add Two Numbers

num1 = 1.5
num2 = 6.3

sum = num1 + num2

print('The sum of ' + str(num1) + ' and ' + str(num2)+ ' is '+ str(sum))
or
print('The sum of {0} and {1} is {2}' .format(num1, num2, sum))
or
print(‘The sum is ‘ , sum)

or (Through Console)

num1= int(input('Enter the first number'))


num2 = int(input('Enter the second number'))

sum=num1 + num2

print('The sum of {0} and {1} is {2}'.format(num1,num2,sum))

3. Python Program to Find the Square Root

import math

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


sqRoot = math.sqrt(num)
print('The square root of {0} is {1}'.format(num,sqRoot))

4. Python Program to Calculate the Area of a Triangle

import math

a = float(input('Enter first side: '))


b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
s = (a + b + c) / 2
area = math.sqrt((s*(s-a)*(s-b)*(s-c)))

print('The area of the triangle is ' , area)


or
print('The area of the triangle is %.3f' %area)

5. Python Program to Swap Two Variables

x = input('Enter value of x: ')


y = input('Enter value of y: ')

temp = x
x=y
y = temp

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


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

6. Python Program to Convert Kilometers to Miles

kilometers = float(input("Enter value in kilometers: "))


# conversion factor
conv_fac = 0.621371
# calculate miles
miles = kilometers * conv_fac
print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))

7. Python Program to Convert Celsius To Fahrenheit

celsius=int(input("Enter the temperature in celcius:"))


f=(celsius*1.8)+32
print("Temperature in farenheit is:",f)

8. Python Program to Check if a Number is Odd or Even

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


if (num % 2) == 0:
print(str(num) + ‘is Even’)
else:
print (str(num) + ‘is Odd’)
9. Python Program to Check if a Number is Positive, Negative or 0

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


if num > 0:
print("It is positive number")
elif num == 0:
print("It is Zero")
else:
print("It is a negative number")

10. Python Program to Find the Largest Among Three Numbers

a = int(input('Enter first number : '))


b = int(input('Enter second number : '))
c = int(input('Enter third number : '))

largest = 0

if a > b and a > c :


largest = a
elif b > c :
largest = b
else :
largest = c

print(largest, "is the largest of three numbers.")

11. Python Program to Check Armstrong Number

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

# initialize sum
sum = 0

# find the sum of the cube of each digit


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

# display the result


if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
12. Python Program to Display the multiplication Table

num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10


for i in range(1, 11):
print(num, 'x', i, '=', num*i)

13. Python Program to Check Prime Number

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

# define a flag variable


flag = False

if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

14. Python Program to Find the Sum of Natural Numbers


num = 16

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)

15. Python Program to Find ASCII Value of Character


Python code using ord function : ord() : It converts the given string of length one, returns an
integer representing the Unicode code point of the character. For example, ord(‘a’) returns
the integer 97.

char = input("Please enter a character: ")


print ("The ASCII value of '" + K + "' is ", ord(char))

16. Python Program to Find HCF or GCD


def compute_hcf(x, y):

# choose the smaller number


if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf

num1 = 54
num2 = 24

print("The H.C.F. is", compute_hcf(num1, num2))

17. Python Program to Make a Simple Calculator


18. Python Program to Shuffle Deck of Cards

You might also like