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

PPT
2025pylab engineering 2025pylab engineering
DOCX
CS3401- Algorithmto use for data structure.docx
PDF
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
PPTX
Introduction to python programming ( part-3 )
PDF
Concept of Data science and Numpy concept
DOCX
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
PDF
Programming in lua STRING AND ARRAY
PDF
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
2025pylab engineering 2025pylab engineering
CS3401- Algorithmto use for data structure.docx
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
Introduction to python programming ( part-3 )
Concept of Data science and Numpy concept
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
Programming in lua STRING AND ARRAY
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf

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
PDF
Numpy - Array.pdf
PDF
Unit-5-Part1 Array in Python programming.pdf
PDF
Python programming : Arrays
PDF
III MCS python lab (1).pdf
PPTX
Python ppt
PPTX
ARRAY OPERATIONS.pptx
PPTX
第二讲 Python基礎
PPTX
第二讲 预备-Python基礎
DOCX
ECE-PYTHON.docx
PPTX
object oriented programing in python and pip
PDF
Quantum simulation Mathematics and Python examples
PPTX
Python Workshop - Learn Python the Hard Way
PPTX
NUMPY [Autosaved] .pptx
PDF
python cheat sheat, Data science, Machine learning
PDF
2. Python Cheat Sheet.pdf
PDF
Beginner's Python Cheat Sheet
PDF
beginners_python_cheat_sheet_pcc_all (1).pdf
PPTX
beginners_python_cheat_sheet_pcc_all (3).pptx
PDF
1. python
NUMPY-2.pptx
Numpy - Array.pdf
Unit-5-Part1 Array in Python programming.pdf
Python programming : Arrays
III MCS python lab (1).pdf
Python ppt
ARRAY OPERATIONS.pptx
第二讲 Python基礎
第二讲 预备-Python基礎
ECE-PYTHON.docx
object oriented programing in python and pip
Quantum simulation Mathematics and Python examples
Python Workshop - Learn Python the Hard Way
NUMPY [Autosaved] .pptx
python cheat sheat, Data science, Machine learning
2. Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (3).pptx
1. python
Ad

More from Kritika Chauhan (19)

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

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Pre independence Education in Inndia.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
01-Introduction-to-Information-Management.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Pre independence Education in Inndia.pdf
Basic Mud Logging Guide for educational purpose
Module 4: Burden of Disease Tutorial Slides S2 2025
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.
Week 4 Term 3 Study Techniques revisited.pptx
Supply Chain Operations Speaking Notes -ICLT Program
TR - Agricultural Crops Production NC III.pdf

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