Tuesday, June 6, 2023

#PYTHON(DAY41)Example Programs

                                        #PYTHON(DAY41)

Example Programs

1) Python Program to Convert Decimal to Binary Using Recursion

# Function to print binary number using recursion
def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')

# decimal number
dec = 34

convertToBinary(dec)
print()

And the output is:

100010

2) Python Program to Add Two Matrices

# Program to add two matrices using nested loop

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]

for r in result:
print(r)

And the output is:

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]

3) Python Program to Transpose a Matrix

# Program to transpose a matrix using a nested loop

X = [[12,7],
[4 ,5],
[3 ,8]]

result = [[0,0,0],
[0,0,0]]

# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[j][i] = X[i][j]

for r in result:
print(r)

And the output is:

[12, 4, 3]
[7, 5, 8]

Monday, June 5, 2023

#PYTHON(DAY40)Example Programs

 

#PYTHON(DAY40)

Example programs

1) Python Program to Display Fibonacci Sequence Using Recursion

# Python program to display the Fibonacci sequence

def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))

And the output is:

Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

2) Python Program to Find Sum of Natural Numbers Using Recursion

# Python program to find the sum of natural using recursive function

def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n-1)

# change this value for a different result
num = 16

if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))

And the output is:

The sum is 136

3) Python Program to Find Factorial of Number Using Recursion

# Factorial of a number using recursion

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)

num = 7

# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

And the output is:

The factorial of 7 is 5040

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

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