0% found this document useful (0 votes)
6 views21 pages

Python Programs for Basic Data Operations

The document contains a series of Python programs that perform various tasks such as checking number types, finding the largest number, determining grades, and performing string operations. It also includes examples of using lists, tuples, and NumPy arrays for mathematical operations and data visualization with Matplotlib. Additionally, it features bar graphs and line charts to represent data visually.

Uploaded by

ujwalrajsoni7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

Python Programs for Basic Data Operations

The document contains a series of Python programs that perform various tasks such as checking number types, finding the largest number, determining grades, and performing string operations. It also includes examples of using lists, tuples, and NumPy arrays for mathematical operations and data visualization with Matplotlib. Additionally, it features bar graphs and line charts to represent data visually.

Uploaded by

ujwalrajsoni7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# Input a number

num = float(input("Enter a number: "))

# Check the number type

if num > 0:

print("Positive number")

elif num < 0:

print("Negative number")

else:

print("Zero")

# Input three numbers

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

# Compare the numbers

if a >= b and a >= c:

print("Largest number is:", a)

elif b >= a and b >= c:

print("Largest number is:", b)

else:

print("Largest number is:", c)

# Input a number

num = int(input("Enter a number: "))

# Check even or odd


if num % 2 == 0:

print("Even number")

else:

print("Odd number")

# Input marks

marks = float(input("Enter marks: "))

# Check pass or fail

if marks >= 40:

print("Result: Pass")

else:

print("Result: Fail")

# Input age

age = int(input("Enter your age: "))

# Check eligibility

if age >= 18:

print("You are eligible to vote.")

else:

print("You are not eligible to vote.")

# Input percentage

percent = float(input("Enter your percentage: "))

# Determine grade

if percent >= 90:


print("Grade: A+")

elif percent >= 80:

print("Grade: A")

elif percent >= 70:

print("Grade: B")

elif percent >= 60:

print("Grade: C")

elif percent >= 50:

print("Grade: D")

else:

print("Grade: F")

1. Program to find the largest and smallest number in a list

# Create a list of numbers

numbers = [10, 25, 5, 40, 15]

# Find largest and smallest

largest = max(numbers)

smallest = min(numbers)

# Display results

print("Numbers:", numbers)

print("Largest number:", largest)

print("Smallest number:", smallest)

2. Program to calculate the product of all numbers in a list

# Create a list of numbers

numbers = [2, 3, 4, 5, 6]
# Initialize product

product = 1

for num in numbers:

product *= num

# Display result

print("Numbers:", numbers)

print("Product of all numbers:", product)

3. Program to count even and odd numbers in a list

# Create a list of numbers

numbers = [10, 21, 4, 45, 66, 93]

even = 0

odd = 0

# Count even and odd

for num in numbers:

if num % 2 == 0:

even += 1

else:

odd += 1

# Display result

print("Total even numbers:", even)

print("Total odd numbers:", odd)


4. Program to find the average of positive numbers in a list

# Create a list of numbers

numbers = [-3, 4, 7, -1, 9, 0, 5]

# Filter positive numbers

positive = [num for num in numbers if num > 0]

# Calculate average

average = sum(positive) / len(positive)

# Display result

print("Positive numbers:", positive)

print("Average of positive numbers:", average)

5. Program to display all elements greater than 10

# Create a list of numbers

numbers = [5, 12, 7, 18, 3, 25, 10]

# Display numbers greater than 10

print("Numbers greater than 10:")

for num in numbers:

if num > 10:

print(num)

6. Program to square each element in a list

# Create a list of numbers

numbers = [1, 2, 3, 4, 5]
# Square each element

squares = [num ** 2 for num in numbers]

# Display result

print("Original list:", numbers)

print("Squared list:", squares)

1. Program to create a tuple of fruits and add one more fruit

# Create a tuple of fruits

fruits = ("Apple", "Banana", "Mango", "Orange", "Grapes")

# Convert tuple to list

fruit_list = list(fruits)

# Add one more fruit

fruit_list.append("Pineapple")

# Convert back to tuple

fruits = tuple(fruit_list)

# Display final tuple

print("Updated tuple of fruits:", fruits)

2. Program to create a tuple of subjects and remove one subject

# Create a tuple of subjects

subjects = ("Maths", "Science", "English", "History", "Geography")

# Convert tuple to list


subject_list = list(subjects)

# Remove one subject

subject_list.remove("History")

# Convert back to tuple

subjects = tuple(subject_list)

# Display final tuple

print("Updated tuple of subjects:", subjects)

3. Program to create a tuple of colors and replace one color

# Create a tuple of colors

colors = ("Red", "Blue", "Green", "Yellow", "White")

# Convert tuple to list

color_list = list(colors)

# Replace one color

color_list[2] = "Purple"

# Convert back to tuple

colors = tuple(color_list)

# Display final tuple

print("Updated tuple of colors:", colors)

4. Program to create a tuple of countries and insert one at a specific position


# Create a tuple of countries

countries = ("India", "USA", "Japan", "Germany", "France")

# Convert tuple to list

country_list = list(countries)

# Insert a new country

country_list.insert(2, "Australia")

# Convert back to tuple

countries = tuple(country_list)

# Display final tuple

print("Updated tuple of countries:", countries)

5. Program to join two tuples

# Create two tuples

tuple1 = ("Delhi", "Mumbai", "Kolkata")

tuple2 = ("Chennai", "Hyderabad")

# Join both tuples

final_tuple = tuple1 + tuple2

# Display result

print("Combined tuple of cities:", final_tuple)

1. String operations on "Artificial Intelligence"

# 1. Create a string
text = "Artificial Intelligence"

# 2. Print in uppercase

print("Uppercase:", [Link]())

# 3. Find and print the index of "Intel"

print("Index of 'Intel':", [Link]("Intel"))

# 4. Replace "Artificial" with "Advanced"

new_text = [Link]("Artificial", "Advanced")

print("After replacement:", new_text)

# 5. Check if the string starts with "Art"

print("Starts with 'Art':", [Link]("Art"))

2. String operations on "Python Programming"

# 1. Create a string

s = "Python Programming"

# 2. Print the string in lowercase

print("Lowercase:", [Link]())

# 3. Count how many times 'm' appears

print("Count of 'm':", [Link]('m'))

# 4. Replace "Python" with "Advanced Python"

new_s = [Link]("Python", "Advanced Python")

print("Replaced string:", new_s)


# 5. Check if the string contains "Code"

print("Contains 'Code':", "Code" in s)

3. String operations on "Machine Learning"

# 1. Create a string

text = "Machine Learning"

# 2. Print in title case

print("Title case:", [Link]())

# 3. Find index of "Learning"

print("Index of 'Learning':", [Link]("Learning"))

# 4. Replace "Machine" with "Deep"

new_text = [Link]("Machine", "Deep")

print("After replacement:", new_text)

# 5. Check if string ends with "ing"

print("Ends with 'ing':", [Link]("ing"))

4. String operations on "Computer Science"

# 1. Create a string

s = "Computer Science"

# 2. Print in uppercase

print("Uppercase:", [Link]())
# 3. Find index of 'Science'

print("Index of 'Science':", [Link]('Science'))

# 4. Replace "Computer" with "Information"

new_s = [Link]("Computer", "Information")

print("After replacement:", new_s)

# 5. Check if string starts with "Comp"

print("Starts with 'Comp':", [Link]('Comp'))

5. String operations on "Internet of Things"

# 1. Create a string

text = "Internet of Things"

# 2. Print in lowercase

print("Lowercase:", [Link]())

# 3. Find index of "Things"

print("Index of 'Things':", [Link]("Things"))

# 4. Replace "Internet" with "Network"

new_text = [Link]("Internet", "Network")

print("After replacement:", new_text)

# 5. Check if it ends with "Things"

print("Ends with 'Things':", [Link]("Things"))


1️⃣ Example 1:

import numpy as np

arr = [Link]([5, 15, 25, 35, 45])

print(arr[1] + arr[4])

2️⃣ Example 2:

import numpy as np

arr = [Link]([2, 4, 6, 8, 10])

print(arr[2] * arr[3])

3️⃣ Example 3:

import numpy as np

arr = [Link]([12, 24, 36, 48])

print(arr[1] - arr[0])

4️⃣ Example 4:

import numpy as np

arr = [Link]([100, 200, 300, 400])

print(arr[-1] / arr[0])

5️⃣ Example 5:

import numpy as np

arr = [Link]([3, 6, 9, 12])

print(arr[0] + arr[1] + arr[2])


1️⃣ Example 1:

list1 = [2, 4, 6, 8]

print(list1[0] + list1[2])

2️⃣ Example 2:

list2 = [10, 20, 30, 40, 50]

print(list2[-2] + list2[1])

3️⃣ Example 3:

nums = [3, 6, 9, 12]

print(nums[0] + nums[-1])

4️⃣ Example 4:

values = [7, 14, 21, 28, 35]

print(values[2] + values[3])

5️⃣ Example 5:

marks = [45, 55, 65, 75]

print(marks[-3] + marks[-1])

✅ Program 1: Plotting Monthly Sales Over 6 Months

import [Link] as plt

# Lists for months and sales

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

sales = [12000, 15000, 17000, 16000, 18000, 20000]

# Plotting the line chart

[Link](months, sales)
# Labels and title

[Link]("Months")

[Link]("Sales (in ₹)")

[Link]("Monthly Sales Growth")

# Display chart

[Link]()

✅ Program 2: Plotting Temperature Change Over a Week

import [Link] as plt

# Lists for days and temperature

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

temperature = [28, 30, 29, 31, 32, 33, 34]

# Line chart

[Link](days, temperature)

# Labels and title

[Link]("Days")

[Link]("Temperature (°C)")

[Link]("Temperature Variation Over a Week")

# Show chart

[Link]()

✅ Program 3: Student’s Marks in 5 Subjects


import [Link] as plt

subjects = ["Maths", "Science", "English", "SST", "Computer"]

marks = [88, 92, 85, 90, 95]

[Link](subjects, marks)

[Link]("Subjects")

[Link]("Marks")

[Link]("Student Performance in Different Subjects")

[Link]()

✅ Program 4: Plot Population Growth of a City (5 Years)

import [Link] as plt

years = [2019, 2020, 2021, 2022, 2023]

population = [4.5, 4.7, 4.9, 5.2, 5.4] # in lakhs

[Link](years, population)

[Link]("Year")

[Link]("Population (in lakhs)")

[Link]("City Population Growth")

[Link]()

1. Bar Graph: Students’ Marks in 5 Subjects


import [Link] as plt

subjects = ["Maths", "Science", "English", "SST", "Computer"]

marks = [88, 92, 85, 90, 95]

[Link](subjects, marks)

[Link]("Subjects")

[Link]("Marks")

[Link]("Student Performance in Different Subjects")

[Link]()

✅ 2. Bar Graph: Sales Data for 6 Months

import [Link] as plt

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

sales = [12000, 15000, 17000, 16000, 18000, 20000]

[Link](months, sales)

[Link]("Months")

[Link]("Sales (₹)")

[Link]("Half-Year Sales Report")

[Link]()

✅ 3. Bar Graph: Population of Cities


import [Link] as plt

cities = ["Delhi", "Mumbai", "Kolkata", "Chennai", "Bengaluru"]

population = [19, 18, 14, 11, 13] # in millions

[Link](cities, population)

[Link]("Cities")

[Link]("Population (in millions)")

[Link]("Population Comparison of Major Cities")

[Link]()

✅ 4. Bar Graph: Company Revenue for 5 Years

import [Link] as plt

years = [2019, 2020, 2021, 2022, 2023]

revenue = [45, 50, 55, 60, 70] # in crores

[Link](years, revenue)

[Link]("Year")

[Link]("Revenue (in crores)")

[Link]("Company Revenue Growth")

[Link]()

✅ 5. Bar Graph: Number of Students in Each Class


import [Link] as plt

classes = ["Class 6", "Class 7", "Class 8", "Class 9", "Class 10"]

students = [40, 42, 38, 45, 43]

[Link](classes, students)

[Link]("Classes")

[Link]("Number of Students")

[Link]("Students Strength in School")

[Link]()

You might also like