SlideShare a Scribd company logo
Practical of Python
welcome
Take two NumPy arrays having two dimensions. Concatenate the arrays
on axis 1
# Importing the NumPy library
import numpy as np
# Creating the first 2D NumPy array (2 rows, 3 columns)
array1 = np.array([[1, 2, 3],
[4, 5, 6]])
# Creating the second 2D NumPy array (2 rows, 2 columns)
array2 = np.array([[7, 8],
[9, 10]])
# Concatenating the arrays along axis 1 (columns)
# Both arrays must have the same number of rows (axis 0)
result = np.concatenate((array1, array2), axis=1)
# Printing the result
print("Concatenated Array (axis=1):")
print(result)
output –
Concatenated Array (axis=1):
[[ 1 2 3 7 8]
[ 4 5 6 9 10]]
Write a NumPy program to find the most frequent value in an
array.
# Importing NumPy
import numpy as np
# Create a 1D NumPy array with some repeated values
array = np.array([1, 3, 2, 3, 4, 3, 5, 2, 2, 2, 4, 5, 2])
# Use np.unique to find all unique values and their counts
values, counts = np.unique(array, return_counts=True)
# Find the index of the maximum count
max_count_index = np.argmax(counts)
# The most frequent value is the one with the highest count
most_frequent = values[max_count_index]
# Print the result
print("The most frequent value in the array is:", most_frequent)
output –
The most frequent value in the array is: 2
Write a program to replace ‘a’ with ‘b’, ‘b’ with ‘c’,….,’z’ with ‘a’ and
similarly for ‘A’ with ‘B’,’B’ with ‘C’, …., ‘Z’ with ‘A’ in a file. The other
characters should remain unchanged.
# Function to shift letters to the next character in the alphabet
def shift_letter(char):
if 'a' <= char <= 'z':
return 'a' if char == 'z' else chr(ord(char) + 1)
elif 'A' <= char <= 'Z':
return 'A' if char == 'Z' else chr(ord(char) + 1)
else:
return char # Leave other characters unchanged
# Read from input file
with open('input.txt', 'r') as file:
content = file.read()
# Apply the shift to each character
shifted_content = ''.join(shift_letter(c) for c in content)
# Write to output file
with open('output.txt', 'w') as file:
file.write(shifted_content)
print("File transformation complete. Check 'output.txt'.")
input - Hello, World! Zebra.
Output - Ifmmp, Xpsme! Afcsb.
Write a function that reads the contents of the file f3.txt and counts the number
of alphabets, blank spaces, lowercase letters, number of words starting with a
vowel and number of occurrences of a work “hello”.
def analyze_file(filename='f3.txt'):
# Open and read the file
with open(filename, 'r') as file:
content = file.read()
# Count alphabets
alphabet_count = sum(1 for char in content if char.isalpha())
# Count blank spaces
space_count = content.count(' ')
# Count lowercase letters
lowercase_count = sum(1 for char in content if char.islower())
# Split into words
words = content.split()
# Count words starting with a vowel
vowels = 'aeiouAEIOU'
vowel_start_count = sum(1 for word in words if word and word[0] in vowels)
# Count occurrences of the word "hello" (case-insensitive)
hello_count = sum(1 for word in words if word.lower() == 'hello')
# Print results
print("Alphabet characters:", alphabet_count)
print("Blank spaces:", space_count)
print("Lowercase letters:", lowercase_count)
print("Words starting with a vowel:", vowel_start_count)
print("Occurrences of the word 'hello':", hello_count)
# Call the function
analyze_file()
input - Hello there! How are you? hello Hello hi elephant awesome.
Output - Alphabet characters: 46
Blank spaces: 8
Lowercase letters: 34
Words starting with a vowel: 3
Occurrences of the word 'hello': 3

More Related Content

Similar to Write a NumPy program to find the most frequent value in an array. Take two NumPy arrays having two dimensions. Concatenate the arrays on axis 1. (20)

PPTX
NUMPY-2.pptx
MahendraVusa
 
PDF
Numpy - Array.pdf
AnkitaArjunDevkate
 
PDF
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
PDF
III MCS python lab (1).pdf
srxerox
 
PPTX
Python ppt
Anush verma
 
PPTX
ARRAY OPERATIONS.pptx
DarellMuchoko
 
PPTX
第二讲 Python基礎
juzihua1102
 
PPTX
第二讲 预备-Python基礎
anzhong70
 
DOCX
ECE-PYTHON.docx
Chaithanya89350
 
PPTX
object oriented programing in python and pip
LakshmiMarineni
 
PDF
Quantum simulation Mathematics and Python examples
RaviSankar637310
 
PPTX
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
PPTX
NUMPY [Autosaved] .pptx
coolmanbalu123
 
PDF
beginners_python_cheat_sheet_pcc_all (1).pdf
ElNew2
 
PDF
python cheat sheat, Data science, Machine learning
TURAGAVIJAYAAKASH
 
PDF
Beginner's Python Cheat Sheet
Verxus
 
PDF
2. Python Cheat Sheet.pdf
MeghanaDBengalur
 
PPTX
beginners_python_cheat_sheet_pcc_all (3).pptx
HongAnhNguyn285885
 
PDF
Python cheatsheet for beginners
Lahore Garrison University
 
NUMPY-2.pptx
MahendraVusa
 
Numpy - Array.pdf
AnkitaArjunDevkate
 
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
III MCS python lab (1).pdf
srxerox
 
Python ppt
Anush verma
 
ARRAY OPERATIONS.pptx
DarellMuchoko
 
第二讲 Python基礎
juzihua1102
 
第二讲 预备-Python基礎
anzhong70
 
ECE-PYTHON.docx
Chaithanya89350
 
object oriented programing in python and pip
LakshmiMarineni
 
Quantum simulation Mathematics and Python examples
RaviSankar637310
 
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
NUMPY [Autosaved] .pptx
coolmanbalu123
 
beginners_python_cheat_sheet_pcc_all (1).pdf
ElNew2
 
python cheat sheat, Data science, Machine learning
TURAGAVIJAYAAKASH
 
Beginner's Python Cheat Sheet
Verxus
 
2. Python Cheat Sheet.pdf
MeghanaDBengalur
 
beginners_python_cheat_sheet_pcc_all (3).pptx
HongAnhNguyn285885
 
Python cheatsheet for beginners
Lahore Garrison University
 

More from Kritika Chauhan (19)

PPTX
Dalai Lama Succession Explained | History, Politics & China’s Role
Kritika Chauhan
 
PDF
Introduction to programming : flowchart, algorithm
Kritika Chauhan
 
PPTX
Introduction to Python — great for beginners or as a refresher.
Kritika Chauhan
 
PDF
Arduino MCQ Collection: Types, Memory, Applications, and Real-World Uses | Te...
Kritika Chauhan
 
PPTX
Group Captain Shubhanshu Shukla: India’s Return to Space After 41 Years | Ax-...
Kritika Chauhan
 
PDF
50+ Arduino MCQs with Answers | Beginners to Advanced Quiz for Electronics & IoT
Kritika Chauhan
 
PDF
MCQ INTERNET OF THINGS ARDUINO PROGREAMS - EMBEDDED C TYPES OF ARDUINO
Kritika Chauhan
 
PDF
skills-Personality Development Multiple Choice Questions
Kritika Chauhan
 
PPTX
Introduction to Arduino Its components & pins
Kritika Chauhan
 
PDF
Embedded C Programming O Level, C Level students, and diploma holder
Kritika Chauhan
 
PPTX
Introduction to Libre (Beginner Guide) .pptx
Kritika Chauhan
 
PPTX
Presentationonlinebusiness
Kritika Chauhan
 
PPTX
Successpptori
Kritika Chauhan
 
PPTX
Basics Of computer
Kritika Chauhan
 
PPTX
Operator of C language
Kritika Chauhan
 
PPTX
computer types
Kritika Chauhan
 
PPTX
cmd commands project
Kritika Chauhan
 
PPTX
Ppt of blogs
Kritika Chauhan
 
DOCX
Project report on blogs
Kritika Chauhan
 
Dalai Lama Succession Explained | History, Politics & China’s Role
Kritika Chauhan
 
Introduction to programming : flowchart, algorithm
Kritika Chauhan
 
Introduction to Python — great for beginners or as a refresher.
Kritika Chauhan
 
Arduino MCQ Collection: Types, Memory, Applications, and Real-World Uses | Te...
Kritika Chauhan
 
Group Captain Shubhanshu Shukla: India’s Return to Space After 41 Years | Ax-...
Kritika Chauhan
 
50+ Arduino MCQs with Answers | Beginners to Advanced Quiz for Electronics & IoT
Kritika Chauhan
 
MCQ INTERNET OF THINGS ARDUINO PROGREAMS - EMBEDDED C TYPES OF ARDUINO
Kritika Chauhan
 
skills-Personality Development Multiple Choice Questions
Kritika Chauhan
 
Introduction to Arduino Its components & pins
Kritika Chauhan
 
Embedded C Programming O Level, C Level students, and diploma holder
Kritika Chauhan
 
Introduction to Libre (Beginner Guide) .pptx
Kritika Chauhan
 
Presentationonlinebusiness
Kritika Chauhan
 
Successpptori
Kritika Chauhan
 
Basics Of computer
Kritika Chauhan
 
Operator of C language
Kritika Chauhan
 
computer types
Kritika Chauhan
 
cmd commands project
Kritika Chauhan
 
Ppt of blogs
Kritika Chauhan
 
Project report on blogs
Kritika Chauhan
 
Ad

Recently uploaded (20)

PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Various Psychological tests: challenges and contemporary trends in psychologi...
santoshmohalik1
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPTX
Constitutional Design Civics Class 9.pptx
bikesh692
 
PPTX
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
PYLORIC STENOSIS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PDF
Comprehensive Guide to Writing Effective Literature Reviews for Academic Publ...
AJAYI SAMUEL
 
PPTX
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
PPTX
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
ABDOMINAL WALL DEFECTS:GASTROSCHISIS, OMPHALOCELE.pptx
PRADEEP ABOTHU
 
PPTX
HIRSCHSPRUNG'S DISEASE(MEGACOLON): NURSING MANAGMENT.pptx
PRADEEP ABOTHU
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
national medicinal plants board mpharm.pptx
SHAHEEN SHABBIR
 
PPTX
How to Consolidate Subscription Billing in Odoo 18 Sales
Celine George
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Various Psychological tests: challenges and contemporary trends in psychologi...
santoshmohalik1
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
Constitutional Design Civics Class 9.pptx
bikesh692
 
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PYLORIC STENOSIS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Comprehensive Guide to Writing Effective Literature Reviews for Academic Publ...
AJAYI SAMUEL
 
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
digestive system for Pharm d I year HAP
rekhapositivity
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
ABDOMINAL WALL DEFECTS:GASTROSCHISIS, OMPHALOCELE.pptx
PRADEEP ABOTHU
 
HIRSCHSPRUNG'S DISEASE(MEGACOLON): NURSING MANAGMENT.pptx
PRADEEP ABOTHU
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PPT on the Development of Education in the Victorian England
Beena E S
 
national medicinal plants board mpharm.pptx
SHAHEEN SHABBIR
 
How to Consolidate Subscription Billing in Odoo 18 Sales
Celine George
 
Ad

Write a NumPy program to find the most frequent value in an array. Take two NumPy arrays having two dimensions. Concatenate the arrays on axis 1.

  • 1. Practical of Python welcome Take two NumPy arrays having two dimensions. Concatenate the arrays on axis 1 # Importing the NumPy library import numpy as np # Creating the first 2D NumPy array (2 rows, 3 columns) array1 = np.array([[1, 2, 3], [4, 5, 6]]) # Creating the second 2D NumPy array (2 rows, 2 columns) array2 = np.array([[7, 8], [9, 10]]) # Concatenating the arrays along axis 1 (columns) # Both arrays must have the same number of rows (axis 0) result = np.concatenate((array1, array2), axis=1) # Printing the result print("Concatenated Array (axis=1):") print(result) output – Concatenated Array (axis=1): [[ 1 2 3 7 8] [ 4 5 6 9 10]]
  • 2. Write a NumPy program to find the most frequent value in an array. # Importing NumPy import numpy as np # Create a 1D NumPy array with some repeated values array = np.array([1, 3, 2, 3, 4, 3, 5, 2, 2, 2, 4, 5, 2]) # Use np.unique to find all unique values and their counts values, counts = np.unique(array, return_counts=True) # Find the index of the maximum count max_count_index = np.argmax(counts) # The most frequent value is the one with the highest count most_frequent = values[max_count_index] # Print the result print("The most frequent value in the array is:", most_frequent) output – The most frequent value in the array is: 2 Write a program to replace ‘a’ with ‘b’, ‘b’ with ‘c’,….,’z’ with ‘a’ and similarly for ‘A’ with ‘B’,’B’ with ‘C’, …., ‘Z’ with ‘A’ in a file. The other characters should remain unchanged. # Function to shift letters to the next character in the alphabet def shift_letter(char):
  • 3. if 'a' <= char <= 'z': return 'a' if char == 'z' else chr(ord(char) + 1) elif 'A' <= char <= 'Z': return 'A' if char == 'Z' else chr(ord(char) + 1) else: return char # Leave other characters unchanged # Read from input file with open('input.txt', 'r') as file: content = file.read() # Apply the shift to each character shifted_content = ''.join(shift_letter(c) for c in content) # Write to output file with open('output.txt', 'w') as file: file.write(shifted_content) print("File transformation complete. Check 'output.txt'.") input - Hello, World! Zebra. Output - Ifmmp, Xpsme! Afcsb. Write a function that reads the contents of the file f3.txt and counts the number of alphabets, blank spaces, lowercase letters, number of words starting with a vowel and number of occurrences of a work “hello”. def analyze_file(filename='f3.txt'): # Open and read the file with open(filename, 'r') as file: content = file.read() # Count alphabets
  • 4. alphabet_count = sum(1 for char in content if char.isalpha()) # Count blank spaces space_count = content.count(' ') # Count lowercase letters lowercase_count = sum(1 for char in content if char.islower()) # Split into words words = content.split() # Count words starting with a vowel vowels = 'aeiouAEIOU' vowel_start_count = sum(1 for word in words if word and word[0] in vowels) # Count occurrences of the word "hello" (case-insensitive) hello_count = sum(1 for word in words if word.lower() == 'hello') # Print results print("Alphabet characters:", alphabet_count) print("Blank spaces:", space_count) print("Lowercase letters:", lowercase_count) print("Words starting with a vowel:", vowel_start_count) print("Occurrences of the word 'hello':", hello_count) # Call the function analyze_file() input - Hello there! How are you? hello Hello hi elephant awesome. Output - Alphabet characters: 46 Blank spaces: 8 Lowercase letters: 34 Words starting with a vowel: 3 Occurrences of the word 'hello': 3