Saturday, February 10, 2024

Example of Numpy

 import numpy as np


# Creating a one-dimensional array

array1 = np.array([1, 2, 3, 4, 5])

print("One-dimensional array:")

print(array1)


# Accessing elements in the array

print("\nAccessing elements:")

print("First element:", array1[0])  # Accessing the first element

print("Last element:", array1[-1])  # Accessing the last element


# Performing operations on arrays

print("\nPerforming operations:")

array2 = np.array([6, 7, 8, 9, 10])

print("Array 1 + Array 2:", array1 + array2)  # Adding two arrays together

print("Array 1 * 2:", array1 * 2)  # Multiplying array1 by 2


# Creating a two-dimensional array (matrix)

matrix = np.array([[1, 2, 3],

                   [4, 5, 6],

                   [7, 8, 9]])

print("\nTwo-dimensional array (matrix):")

print(matrix)


# Accessing elements in the matrix

print("\nAccessing elements in the matrix:")

print("Element at row 1, column 1:", matrix[0, 0])  # Accessing the element at row 1, column 1

print("Second row:", matrix[1, :])  # Accessing the second row

print("Third column:", matrix[:, 2])  # Accessing the third column


# Transposing the matrix

print("\nTransposed matrix:")

print(np.transpose(matrix))


# Finding the sum of elements in the matrix

print("\nSum of elements in the matrix:", np.sum(matrix))


# Generating a random array

random_array = np.random.randint(1, 100, size=(3, 3))  # Generating a 3x3 array with random integers from 1 to 100

print("\nRandom array:")

print(random_array)


No comments:

Post a Comment

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...