Here’s a Python example for creating a bar graph using the matplotlib library:
import matplotlib.pyplot as plt
# Data for the bar graph
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 25]
# Create the bar graph
plt.bar(categories, values, color='skyblue')
# Add labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Graph Example')
# Display the graph
plt.show()
Steps to run:
1. Install matplotlib if it’s not already installed:
pip install matplotlib
2. Copy and paste the code into a Python environment or IDE.
3. Run the script, and the bar graph will appear in a pop-up window.
Here’s a Python script that creates both a histogram and a pie chart using the same set of
values:
import matplotlib.pyplot as plt
# Data
values = [10, 20, 15, 25, 20, 15, 10, 30, 25, 20]
categories = ['A', 'B', 'C', 'D'] # For the pie chart
category_counts = [values.count(10), values.count(20), values.count(15), values.count(25)]
# Create a histogram
plt.figure(figsize=(12, 6)) # Set figure size
plt.subplot(1, 2, 1) # 1 row, 2 columns, 1st plot
plt.hist(values, bins=5, color='skyblue', edgecolor='black')
plt.title('Histogram')
plt.xlabel('Value Range')
plt.ylabel('Frequency')
# Create a pie chart
plt.subplot(1, 2, 2) # 1 row, 2 columns, 2nd plot
plt.pie(category_counts, labels=categories, autopct='%1.1f%%', colors=['lightcoral', 'gold',
'lightgreen', 'lightskyblue'])
plt.title('Pie Chart')
# Show the plots
plt.tight_layout()
plt.show()
Explanation:
1. Histogram:
• Shows the frequency distribution of the values.
• bins divides the data into ranges.
2. Pie Chart:
• Represents the proportion of each unique value.
• The category_counts list counts occurrences of specific values.
Steps to run:
1. Install matplotlib if not already installed:
pip install matplotlib
2. Copy and paste the code into a Python script or IDE.
3. Run the script, and you’ll see both the histogram and the pie chart side by side.
Here’s a Python example to create an array of 15 numbers using the numpy library:
import numpy as np
# Create an array of 15 numbers (e.g., from 1 to 15)
array = np.arange(1, 16)
# Display the array
print("Array of 15 numbers:", array)
Explanation:
1. np.arange(start, stop) generates numbers from start to stop-1.
2. The above code creates an array with numbers from 1 to 15.
Steps to run:
1. Install numpy if not already installed:
pip install numpy
2. Run the script, and it will output the array:
Array of 15 numbers: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
If you want random numbers:
import numpy as np
# Create an array of 15 random numbers between 0 and 100
array = np.random.randint(0, 101, 15)
# Display the array
print("Array of 15 random numbers:", array)
Here’s a Python code to calculate Simple Interest and find the sum of the numbers used in the
calculation:
# Function to calculate simple interest
def simple_interest(principal, rate, time):
return (principal * rate * time) / 100
# Input values
principal = 1000 # Principal amount
rate = 5 # Rate of interest
time = 2 # Time period
# Calculate simple interest
si = simple_interest(principal, rate, time)
# Find the sum of the numbers used
numbers = [principal, rate, time, si]
total_sum = sum(numbers)
# Display the results
print(f"Simple Interest: {si}")
print(f"Sum of numbers used: {total_sum}")
Explanation:
1. Simple Interest Formula:
2. Sum of numbers used:
• Includes the principal, rate, time, and calculated simple interest.
Output Example:
If the inputs are:
• Principal = 1000
• Rate = 5
• Time = 2
The output will be:
Simple Interest: 100.0
Sum of numbers used: 1107.0
Here’s a Python code that allows the user to input numbers, multiplies them, and checks if the
result is odd or even:
# Function to check if a number is odd or even
def check_odd_even(number):
if number % 2 == 0:
return "even"
else:
return "odd"
# Input numbers from the user
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Multiply the numbers
result = num1 * num2
# Check if the result is odd or even
odd_even = check_odd_even(result)
# Display the results
print(f"The product of {num1} and {num2} is {result}, which is {odd_even}.")
Example Run:
1. User Input:
Enter the first number: 4
Enter the second number: 5
2. Output:
The product of 4 and 5 is 20, which is even.
This program dynamically works for any two numbers entered by the user.
Here’s a basic Python example of using a for loop:
# Print numbers from 1 to 10 using a for loop
for i in range(1, 11):
print(f"Number: {i}")
Explanation:
1. range(1, 11): Generates numbers from 1 to 10 (the upper limit 11 is exclusive).
2. i: Iterates over each number in the range.
3. The print() statement outputs each number.
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10
You can customize the range, step size, or even iterate over a list of items. Here’s another
example for looping through a list:
# Loop through a list of items
fruits = ['Apple', 'Banana', 'Cherry']
for fruit in fruits:
print(f"I like {fruit}")
Output:
I like Apple
I like Banana
I like Cherry
Here’s a Python code to perform element-wise addition of two arrays using the numpy library:
import numpy as np
# Define two arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([10, 20, 30, 40, 50])
# Perform element-wise addition
result = array1 + array2
# Display the result
print("Array 1:", array1)
print("Array 2:", array2)
print("Sum of the arrays:", result)
Output Example:
Array 1: [1 2 3 4 5]
Array 2: [10 20 30 40 50]
Sum of the arrays: [11 22 33 44 55]
Explanation:
1. np.array: Creates arrays.
2. + Operator: Adds corresponding elements of array1 and array2.
3. The arrays must have the same size for element-wise addition.
If you prefer to do it without numpy:
# Define two lists
list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]
# Perform element-wise addition
result = [x + y for x, y in zip(list1, list2)]
# Display the result
print("List 1:", list1)
print("List 2:", list2)
print("Sum of the lists:", result)
This will give the same result.