Monday, June 12, 2023

#PYTHON(DAY46)Example Programs

                                     #PYTHON(DAY46)

Example Programs

1) Python Program to Reverse a Number

num = 1234
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: " + str(reversed_num))

And the output is:

4321

2) Python Program to Compute the Power of a Number

base = 3
exponent = 4

result = 1

while exponent != 0:
result *= base
exponent-=1

print("Answer = " + str(result))

And the output is:

Answer = 81

3) Python Program to Count the Number of Digits Present In a Number

num = 3452
count = 0

while num != 0:
num //= 10
count += 1

print("Number of digits: " + str(count))

And the output is:

Number of digits: 4

Saturday, June 10, 2023

#PYTHON(DAY45)Example Programs

 #PYTHON(DAY45)

Example Programs

1) Python Program to 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)

And the output is:

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

2) Python Program to Slice Lists

my_list = [1, 2, 3, 4, 5]

print(my_list[:])

And the output is:

[1, 2, 3, 4, 5]

3) Access both key and value using items()

dt = {'a': 'juice', 'b': 'grill', 'c': 'corn'}

for key, value in dt.items():
print(key, value)

And the output is:

a juice
b grill
c corn

#PYTHON(DAY44)Example Programs

                                         #PYTHON(DAY44)

Example Programs

1) Python Program to Create Pyramid Patterns

rows = int(input('enter your rows:'))
for i in range(rows):
for j in range(i+1):
print('*',end = " ")
print("\n")

And the output is:

*
* *
* * *
* * * *
* * * * *

2) Python Program to Merge Two Dictionaries

dict_1 = {1:'a',2:'b'}
dict_2 = {2:'c',4:'d'}
dict_3 = dict_2.copy()
dict_3.update(dict_1)
print(dict_3)

And the output is:

{2: 'b', 4: 'd', 1: 'a'}

3) Python Program to Access Index of a List Using for Loop

my_list = [11,22,33]
for index in range(len(my_list)):
value = my_list[index]
print(index,value)

And the output is:

0 21
1 44
2 35
3 11

Thursday, June 8, 2023

#PYTHON(DAY43)Example Programs

                                      #PYTHON(DAY43)

Example Programs

1) Python Program to Sort Words in Alphabetic Order

# Program to sort alphabetically the words form a string provided by the user

my_str = "Hello this Is an Example With cased letters"

# To take input from the user
#my_str = input("Enter a string: ")

# breakdown the string into a list of words
words = [word.lower() for word in my_str.split()]

# sort the list
words.sort()

# display the sorted words

print("The sorted words are:")
for word in words:
print(word)

And the output is:

The sorted words are:
an
cased
example
hello
is
letters
this
with

2) Python Program to Illustrate Different Set Operations

# Program to perform different set operations like in mathematics

# define three sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};

# set union
print("Union of E and N is",E | N)

# set intersection
print("Intersection of E and N is",E & N)

# set difference
print("Difference of E and N is",E - N)

# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}

3) Python Program to Count the Number of Each Vowel

# Program to count the number of each vowels

# string of vowels
vowels = 'aeiou'

ip_str = 'Hello, have you tried our tutorial section yet?'

# make it suitable for caseless comparisions
ip_str = ip_str.casefold()

# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)

# count the vowels
for char in ip_str:
if char in count:
count[char] += 1

print(count)

And the output is:

{'o': 5, 'i': 3, 'a': 2, 'e': 5, 'u': 3}

Wednesday, June 7, 2023

#PYTHON(DAY42)Example Programs

            #PYTHON(DAY42)

Example Programs

1) Python Program to Multiply Two Matrices

# Program to multiply two matrices using nested loops

# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

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

for r in result:
print(r)

And the output is:

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

2) Python Program to Check Whether a String is Palindrome or Not

# Program to check if a string is palindrome or not

my_str = 'aIbohPhoBiA'

# make it suitable for caseless comparison
my_str = my_str.casefold()

# reverse the string
rev_str = reversed(my_str)

# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

And the output is:

The string is a palindrome.

3) Python Program to Remove Punctuations From a String

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)

And the output is:

Hello he said and went

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

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