Sunday, June 4, 2023

#PYTHON(DAY39)Example Programs

#PYTHON(DAY39)

Example programs

1) Python Program to Make a Simple Calculator

# This function adds two numbers
def add(x, y):
return x + y

# This function subtracts two numbers
def subtract(x, y):
return x - y

# This function multiplies two numbers
def multiply(x, y):
return x * y

# This function divides two numbers
def divide(x, y):
return x / y


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
# take input from the user
choice = input("Enter choice(1/2/3/4): ")

# check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue

if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))

elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))

elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))

elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))

# check if user wants another calculation
# break the while loop if answer is no
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")

And the output is:

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15.0 * 14.0 = 210.0
Let's do next calculation? (yes/no): no

2) Python Program to Shuffle Deck of Cards

# Python program to shuffle a deck of card

# importing modules
import itertools, random

# make a deck of cards
deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))

# shuffle the cards
random.shuffle(deck)

# draw five cards
print("You got:")
for i in range(5):
print(deck[i][0], "of", deck[i][1])

And the output is:

You got:
5 of Heart
1 of Heart
8 of Spade
12 of Spade
4 of Spade

3) Python Program to Display Calendar

# Program to display calendar of the given month and year

# importing calendar module
import calendar

yy = 2014 # year
mm = 11 # month

# To take month and year input from the user
# yy = int(input("Enter year: "))
# mm = int(input("Enter month: "))

# display the calendar
print(calendar.month(yy, mm))

And the output is:

   November 2014
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

 

Saturday, June 3, 2023

#PYTHON(DAY38)Example Programs

  #PYTHON(DAY38)

Example Programs

1) Python Program to Find ASCII Value of Character

# Program to find the ASCII value of the given character

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

And the output is:

The ASCII value of 'p' is 112

2) Python Program to Find HCF or GCD

# Python program to find H.C.F of two numbers

# define a function
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))

And the output is:

The H.C.F. is 6

3) Python Program to Find LCM

# Python Program to find the L.C.M. of two input number

def compute_lcm(x, y):

# choose the greater number
if x > y:
greater = x
else:
greater = y

while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1

return lcm

num1 = 54
num2 = 24

print("The L.C.M. is", compute_lcm(num1, num2))

And the output is:

The L.C.M. is 216

4) Python Program to Find the Factors of a Number

# Python Program to find the factors of a number

# This function computes the factor of the argument passed
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)

num = 320

print_factors(num)

And the output is:

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320

Friday, June 2, 2023

#PYTHON(DAY37)Example Programs

 #PYTHON(DAY37)

Example Programs

1) Python Program to Find the Sum of Natural Numbers

# Sum of natural numbers up to num

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)

And the output is:

The sum is 136

2) Python Program to Display Powers of 2 Using Anonymous Function

# Display the powers of 2 using anonymous function

terms = 10

# Uncomment code below to take input from the user
# terms = int(input("How many terms? "))

# use anonymous function
result = list(map(lambda x: 2 ** x, range(terms)))

print("The total terms are:",terms)
for i in range(terms):
print("2 raised to power",i,"is",result[i])

And the output is:

The total terms are: 10
2 raised to power 0 is 1
2 raised to power 1 is 2
2 raised to power 2 is 4
2 raised to power 3 is 8
2 raised to power 4 is 16
2 raised to power 5 is 32
2 raised to power 6 is 64
2 raised to power 7 is 128
2 raised to power 8 is 256
2 raised to power 9 is 512

3) Python Program to Find Numbers Divisible by Another Number

# Take a list of numbers
my_list = [12, 65, 54, 39, 102, 339, 221,]

# use anonymous function to filter
result = list(filter(lambda x: (x % 13 == 0), my_list))

# display the result
print("Numbers divisible by 13 are",result)

And the output is:

Numbers divisible by 13 are [65, 39, 221]

4) Python Program to Convert Decimal to Binary, Octal and Hexadecimal

# Python program to convert decimal into other number systems
dec = 344

print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")

And the output is:

The decimal value of 344 is:
0b101011000 in binary.
0o530 in octal.
0x158 in hexadecimal.

Thursday, June 1, 2023

#PYTHON(DAY36)Example Programs

      #PYTHON(DAY36)

Example Programs

1) Python Program to Display the multiplication Table

# Multiplication table (from 1 to 10) in Python

num = 12

# To take input from the user
# 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)

And the output is:

12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

2) Python Program to Print the Fibonacci sequence

# Program to display the Fibonacci sequence up to n-th term

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

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

And the output is:

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

3) Python Program to Check Armstrong Number

# Python program to check if the number is an Armstrong number or not

# take input from the user
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")

And the output is:

Enter a number: 663
663 is not an Armstrong number

4) Python Program to Find Armstrong Number in an Interval

# Program to check Armstrong numbers in a certain interval

lower = 100
upper = 2000

for num in range(lower, upper + 1):

# order of number
order = len(str(num))

# initialize sum
sum = 0

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

if num == sum:
print(num)

And the output is:

153
370
371
407
1634

Wednesday, May 31, 2023

#PYTHON(DAY35)Example Programs

            #PYTHON(DAY35)

Example Programs

1) Python Program to Find the Largest Among Three Numbers

# Python program to find the largest number among the three input numbers

# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

And the output is:

The largest number is 14.0

2) Python Program to Check Prime Number

# Program to check if a number is prime or not

num = 29

# To take input from the user
#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")

And the output is:

29 is a prime number

3) Python Program to Print all Prime Numbers in an Interval

# Python program to display all the prime numbers within an interval

lower = 900
upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

And the output is:

Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997

4) Python Program to Find the Factorial of a Number

# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

And the output is:

The factorial of 7 is 5040

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...