Tuesday, June 13, 2023

#PYTHON(DAY48)Example Programs

                                     #PYTHON(DAY48)

Example Programs

1) Python Program to Randomly Select an Element From the List

import random

my_list = [1, 'a', 32, 'c', 'd', 31]
print(random.choice(my_list))

And the output is:

31

2) Python Program to Print Output Without a Newline

# print each statement on a new line
print("Python")
print("is easy to learn.")

# new line
print()

# print both the statements on a single line
print("Python", end=" ")
print("is easy to learn.")

And the output is:

Python
is easy to learn.

Python is easy to learn.

3) Python Program to Get a Substring of a String

my_string = "I love python."

# prints "love"
print(my_string[2:6])

# prints "love python."
print(my_string[2:])

# prints "I love python"
print(my_string[:-1])

And the output is:

love
love python.
I love python

Monday, June 12, 2023

#PYTHON(DAY47)Example Programs

                                         #PYTHON(DAY47)

Example programs

1) Python Program to Capitalize the First Character of a String

my_string = "programiz is Lit"

print(my_string[0].upper() + my_string[1:])

And the output is:

Programiz is Lit

2) Python Program to Count the Number of Occurrence of a Character in String

count = 0

my_string = "Programiz"
my_char = "r"

for i in my_string:
if i == my_char:
count += 1

print(count)

And the output is:

2

3) Python Program to Trim Whitespace From a String

my_string = " Python "

print(my_string.strip())

And the output is:

Python

#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

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