<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: amiogithub</title>
    <description>The latest articles on DEV Community by amiogithub (@amiogithub).</description>
    <link>https://dev.to/amiogithub</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1161291%2F7fece29e-dda1-4aa6-ad52-516b1e713b61.jpeg</url>
      <title>DEV Community: amiogithub</title>
      <link>https://dev.to/amiogithub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amiogithub"/>
    <language>en</language>
    <item>
      <title>Transforming My To-Do App: From Console to GUI with PySimpleGUI</title>
      <dc:creator>amiogithub</dc:creator>
      <pubDate>Wed, 28 Aug 2024 17:10:45 +0000</pubDate>
      <link>https://dev.to/amiogithub/transforming-my-to-do-app-from-console-to-gui-with-pysimplegui-5gjk</link>
      <guid>https://dev.to/amiogithub/transforming-my-to-do-app-from-console-to-gui-with-pysimplegui-5gjk</guid>
      <description>&lt;p&gt;After building a console-based to-do app, I wanted to elevate it by creating a graphical user interface (GUI). Using PySimpleGUI, I developed a more user-friendly version of the app that not only looks better but also makes managing tasks more intuitive. Here’s a detailed breakdown of the code and the thought process behind it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the Environment&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import functions
import time
import PySimpleGUI as me
import os
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
The first step is importing the necessary modules:&lt;/p&gt;

&lt;p&gt;functions handles reading and writing tasks to a file.&lt;br&gt;
time allows for displaying the current time on the GUI.&lt;br&gt;
PySimpleGUI, imported as me, is the library that provides all the tools to build the GUI.&lt;br&gt;
os is used to check if the todos.txt file exists and create it if it doesn’t.&lt;br&gt;
*&lt;em&gt;Ensuring the Existence of the To-Do File&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
if not os.path.exists("todos.txt"):
    with open("todos.txt", "w") as file:
        pass

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
This block checks whether the todos.txt file exists. If it doesn’t, it creates an empty file. This ensures that the app doesn’t crash due to a missing file, which is crucial for smooth user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Designing the GUI Layout&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;me.theme("DarkPurple4")
clock = me.Text("Type in a to-do")
label = me.Text('', key='clock')
input_box = me.InputText(tooltip="Enter todo", key='todo')
add_button = me.Button(size=2, image_source="004 add.png", mouseover_colors="LightBlue2", tooltip="Add Todo", key="Add")
list_box = me.Listbox(values=functions.get_todos(), key='todos', enable_events=True, size=[45, 10])
edit_button = me.Button("Edit")
complete_button = me.Button("Complete")
exit_button = me.Button("Exit")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Why This?&lt;br&gt;
Here, I designed the layout of the app:&lt;/p&gt;

&lt;p&gt;Theme: I chose a theme called "DarkPurple4" for aesthetics.&lt;br&gt;
Clock: The clock label is initially set with placeholder text, and the actual time is updated later.&lt;br&gt;
Input Box: Users can type their to-do items here.&lt;br&gt;
Buttons: The app features buttons for adding, editing, completing, and exiting tasks. These are designed with tooltips and visual feedback to enhance user interaction.&lt;br&gt;
Listbox: This displays the current to-dos, dynamically updated as users interact with the app.&lt;br&gt;
Arranging the Layout in a Window**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window = me.Window("My To-Do App", layout=[[clock],
                                           [label],
                                           [input_box, add_button],
                                           [list_box, edit_button, complete_button],
                                           [exit_button]],
                   font=('Helvetica', 13))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
The layout is arranged in a window with a specific font. The window’s title is "My To-Do App," and it organizes the elements in a grid-like structure, making the interface intuitive and easy to navigate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main Event Loop: Handling User Interactions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while True:
    event, values = window.read(timeout=200)
    window["clock"].update(value=time.strftime("%b %d, %Y %H:%M:%S"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
This loop keeps the app running, constantly listening for user input. The timeout=200 parameter updates the clock every 200 milliseconds, giving a real-time feel to the app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling the Add Button&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;match event:
    case "Add":
        todos = functions.get_todos()
        new_todo = values['todo'] + "\n"
        todos.append(new_todo)
        functions.write_todos(todos)
        window['todos'].update(values=todos)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;match event:
    case "Add":
        todos = functions.get_todos()
        new_todo = values['todo'] + "\n"
        todos.append(new_todo)
        functions.write_todos(todos)
        window['todos'].update(values=todos)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
When the "Add" button is clicked, this block:&lt;/p&gt;

&lt;p&gt;Retrieves the current to-dos.&lt;br&gt;
Appends the new task entered by the user.&lt;br&gt;
Writes the updated list back to the file.&lt;br&gt;
Updates the listbox in the GUI to reflect the new task.&lt;br&gt;
&lt;strong&gt;Handling the Edit Button&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    case "Edit":
        try:
            todo_to_edit = values['todos'][0]
            new_todo = values['todo']
            todos = functions.get_todos()
            index = todos.index(todo_to_edit)
            todos[index] = new_todo
            functions.write_todos(todos)
            window['todos'].update(values=todos)
        except IndexError:
            me.popup("Select an item first", font=('Helvetica', 15))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
Editing a task is a bit more complex:&lt;/p&gt;

&lt;p&gt;The app checks if a task is selected.&lt;br&gt;
It retrieves the selected task and replaces it with the new input.&lt;br&gt;
If no task is selected, an error message pops up, guiding the user to select an item.&lt;br&gt;
&lt;strong&gt;Handling the Complete Button&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case "Complete":
        try:
            todo_to_complete = values['todos'][0]
            todos = functions.get_todos()
            todos.remove(todo_to_complete)
            functions.write_todos(todos)
            window['todos'].update(values=todos)
            window['todo'].update(value='')
        except IndexError:
            me.popup("Select an item first", font=('Helvetica', 15))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;When a task is marked as complete:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The app removes it from the list and updates the file.&lt;br&gt;
The listbox is updated to reflect the change.&lt;br&gt;
If no task is selected, an error message helps the user correct the mistake.&lt;br&gt;
&lt;strong&gt;Exiting the App&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case "Exit":
        break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
This block simply breaks the loop, closing the app when the "Exit" button is clicked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling Selection from the List&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case 'todos':
        window['todo'].update(value=values['todos'][0])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
When a user clicks on a task in the list, the input box is automatically populated with that task, making it easier to edit or complete.&lt;/p&gt;

&lt;p&gt;Final Cleanup&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    case me.WINDOW_CLOSED:
        break

window.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
If the window is closed using the close button, the loop breaks, and the window is properly closed to release resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connecting with Git and GitHub&lt;/strong&gt;&lt;br&gt;
Version control is essential in any project, so I connected this project with Git and GitHub. Here’s a quick overview:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initialize a Git Repository:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
This sets up a new Git repository in your project folder.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Commit Changes:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Initial commit of to-do app GUI"
The git add . command stages all files, and git commit saves your changes with a descriptive message.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Push to GitHub:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
git remote add origin &amp;lt;repository-URL&amp;gt;
git push -u origin main
This connects your local repository to a GitHub repository and pushes the changes to GitHub.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
Turning my console-based to-do app into a GUI application was an exciting challenge. This project taught me a lot about user interface design, event-driven programming, and the importance of version control. I hope this breakdown helps you understand not just how the app works, but also why certain decisions were made. If you’re looking to build your own version, start small, iterate, and don’t hesitate to explore tools like PySimpleGUI and GitHub to take your projects to the next level.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How I Built a Simple To-Do App in Python: From Concept to Code</title>
      <dc:creator>amiogithub</dc:creator>
      <pubDate>Wed, 28 Aug 2024 08:50:01 +0000</pubDate>
      <link>https://dev.to/amiogithub/how-i-built-a-simple-to-do-app-in-python-from-concept-to-code-56b1</link>
      <guid>https://dev.to/amiogithub/how-i-built-a-simple-to-do-app-in-python-from-concept-to-code-56b1</guid>
      <description>&lt;p&gt;Creating a to-do app in Python was one of my recent projects, and it was an enriching experience that evolved over time. Initially, I started with simpler functions, focusing on getting the app to run smoothly in the console. As days passed, I refined and optimized the code, leading to the final version I'm sharing here. While I started with &lt;strong&gt;PyCharm&lt;/strong&gt;, I encountered performance issues like overheating, so I'd recommend using VS Code instead for a smoother experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Build a To-Do App?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A to-do app might seem like a basic project, but it's a fantastic way to apply and improve your programming skills. It involves working with file handling, loops, conditional statements, and functions—all essential concepts in Python. Plus, the end product is something practical that you can actually use in your daily life.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Breaking Down the Code: A Step-by-Step Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's dive into the code, and I'll walk you through my thought process at each stage.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting Up the Environment:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import functions
import time

now = time.strftime("%b %d, %Y %H:%M:%S")
print("It is", now)
print("Routine checkup")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I wanted the app to feel personal, so I added a timestamp at the start. It gives a real-time feel and serves as a gentle reminder of when you last checked your tasks. Importing the functions module keeps the main file clean and organized by handling file operations separately.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Adding Tasks:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if user_action.startswith('add'):
    todo = user_action[4:]
    todos = functions.get_todos()
    todos.append(todo + '\n')
    functions.write_todos(todos)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
Initially, I experimented with different ways to add tasks. The approach I settled on—using string slicing—ensures that the app is user-friendly. It allows users to type "add " without needing to follow a rigid format. By appending a newline character (\n), the tasks are neatly stored in the text file, ensuring proper formatting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Showing Tasks:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elif user_action.startswith('show'):
    todos = functions.get_todos()
    for index, item in enumerate(todos):
        item = item.strip('\n')
        row = f'{index + 1} - {item}'
        print(row)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
Displaying tasks might seem straightforward, but it's crucial to ensure clarity. I used enumerate() to list tasks with numbers, making it easy to reference them later. Removing newline characters during display ensures the output is clean and readable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Editing Tasks:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elif user_action.startswith('edit'):
    try:
        number = int(user_action[5:]) - 1
        todos = functions.get_todos()
        new_todo = input("Enter new todo: ") + '\n'
        todos[number] = new_todo
        functions.write_todos(todos)
    except ValueError:
        print("Oops! Your command is invalid.")
        continue

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
Editing was one of the trickier features to implement. The main challenge was managing user input and ensuring the correct task was updated. I chose to subtract 1 from the task number to align with Python's zero-based indexing. This choice simplifies coding but might require a brief explanation for users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Completing Tasks:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elif user_action.startswith('complete'):
    try:
        number = int(user_action[9:])
        todos = functions.get_todos()
        todo_to_remove = todos.pop(number - 1).strip('\n')
        functions.write_todos(todos)
        print(f'Todo "{todo_to_remove}" was successfully removed.')
    except IndexError:
        print("The task number is not in your list.")
        continue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
Completing a task involves removing it from the list, which brings up the challenge of managing list indices. By using .pop(), I was able to remove tasks efficiently, and the app confirms the deletion to avoid any confusion. Handling potential errors, like an IndexError, ensures that the app remains robust even with incorrect input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Exiting the App:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elif user_action.startswith('exit'):
    break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This?&lt;/strong&gt;&lt;br&gt;
The exit function is simple but essential. It gracefully terminates the loop, ending the session while ensuring that no tasks are lost in the process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7.Functions File: Keeping It Organized&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FILEPATH='todos.txt'
def get_todos(file_path=FILEPATH):
    with open(file_path, 'r') as file_local:
        todos_local = file_local.readlines()
        return todos_local

def write_todos(todos_arg, file_path=FILEPATH):
    with open(file_path, 'w') as file_local:
        file_local.writelines(todos_arg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why This?&lt;br&gt;
Splitting file operations into a separate functions.py file kept the main script clean and easy to read. It also makes it easier to debug and scale the app. By abstracting these operations, I ensured that the main logic focused on user interaction, while the functions file handled data persistence.&lt;/p&gt;

&lt;p&gt;Final Thoughts&lt;br&gt;
Building this to-do app was a fantastic learning experience. I encourage anyone interested in Python to try building something similar. Start simple, test your code, and gradually add features. Remember, it's not just about writing code—it's about understanding why you choose certain approaches and how they contribute to a more efficient and user-friendly program. And if you're using PyCharm and facing performance issues, give VS Code a try—it might just make your coding journey a little smoother.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw9kpxjtvyaf718pqmot.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw9kpxjtvyaf718pqmot.JPG" alt="Image description" width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>NumPy Ninja: Unleashing the Power of Python for Data Wizards</title>
      <dc:creator>amiogithub</dc:creator>
      <pubDate>Mon, 25 Dec 2023 20:27:22 +0000</pubDate>
      <link>https://dev.to/amiogithub/numpy-ninja-unleashing-the-power-of-python-for-data-wizards-1774</link>
      <guid>https://dev.to/amiogithub/numpy-ninja-unleashing-the-power-of-python-for-data-wizards-1774</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction to NUMPY&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;WHAT IS NUMPY?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ckm9rEVn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwb8appstq2vmfacv07a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ckm9rEVn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwb8appstq2vmfacv07a.png" alt="Image description" width="600" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NumPy is like a superhero for numbers in the world of programming. It stands for "Numerical Python." It's a library, which is like a set of tools or special powers that make it easy to work with numbers in the Python programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use NumPy?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SNkd9p_K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzbecyjehbibl2fru3an.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SNkd9p_K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzbecyjehbibl2fru3an.png" alt="?" width="498" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you have a big box of Lego bricks. Each brick represents a number or a piece of data. NumPy is like a magic glove that helps you organize, manipulate, and play with those Lego bricks in really efficient ways. It's super fast and makes your life easier when dealing with lots of numbers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_EXYKvZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv76d6p3w3xuklxt7c2w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_EXYKvZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv76d6p3w3xuklxt7c2w.jpg" alt="Numpy" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Importance of NumPy in Data Science:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speed: NumPy is super fast because it's written in a language called C, which is like the Flash of programming languages.&lt;/li&gt;
&lt;li&gt;Efficiency: NumPy uses smart techniques to handle large datasets without making your computer feel like it's lifting a mountain.&lt;/li&gt;
&lt;li&gt;Math Magic: Data science involves a lot of math, and NumPy has tons of functions that make mathematical operations easy, like adding, multiplying, or even doing fancy calculus stuff.&lt;/li&gt;
&lt;li&gt;Compatibility: Many other data science tools and libraries in Python are like friends that understand NumPy's language, so they work well together.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How impactful is NumPy?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;NumPy is like the secret sauce in the recipe of data science. Without it, things would be much harder and slower. It's used everywhere, from simple calculations to training complex machine learning models.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How easy is it to use?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pF4N5M-a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m543y4ef1pkcee41nlix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pF4N5M-a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m543y4ef1pkcee41nlix.png" alt="Numpy works like a magic" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you have a magic wand. You say a spell "&lt;em&gt;&lt;strong&gt;Expecto Patronum&lt;/strong&gt;&lt;/em&gt;"(or write a line of code), and NumPy does what you want. It's designed to be user-friendly, with simple functions that do powerful things. With a bit of practice, you'll find yourself saying, "Wow, that was easy!"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What becomes impossible without NumPy?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without NumPy, working with large sets of data would be like trying to build a massive Lego castle without the special glove. It would take forever, and you might even give up because it's just too hard. NumPy makes the impossible possible in the world of data science!&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Let's dive into a simple example to see how NumPy helps a data scientist. *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Without NumPy
math_grades = [90, 85, 88, 92, 78]
physics_grades = [88, 80, 85, 90, 82]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Lets see what happens without using Numpy&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Without NumPy
num_students = len(math_grades)
total_math_grades = sum(math_grades)
total_physics_grades = sum(physics_grades)

average_math_grade = total_math_grades / num_students
average_physics_grade = total_physics_grades / num_students

class_average = (average_math_grade + average_physics_grade) / 2

print("Class Average (Without NumPy):", class_average)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Now see what happens when you use Numpy&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


import numpy as np

# With NumPy
math_grades = np.array([90, 85, 88, 92, 78])
physics_grades = np.array([88, 80, 85, 90, 82])

average_math_grade = np.mean(math_grades)
average_physics_grade = np.mean(physics_grades)

class_average = np.mean([average_math_grade, average_physics_grade])

print("Class Average (With NumPy):", class_average)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, now we all have a basic understanding of why we will use Numpy right? So lets get started!&lt;/p&gt;

&lt;p&gt;Firstly we will learn about Numpy arrays. In python, we are familiar with Lists right? Array is also another datatype which is present in other languages too like C++,JAVA etc.&lt;/p&gt;

&lt;p&gt;There can be many dimensions like 2D,3D,4D etc.&lt;/p&gt;

&lt;p&gt;Numpy arrays can be represented in Vectors slash matrices.&lt;/p&gt;

&lt;p&gt;Now lets create an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.arrange(0,10,1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here arrange function is used to create a numpy array. The syntax will be np.arrange(start,stop,step). It gives an array of length 10 starting from 0 to 9 and the difference between two consecutive numbers will be the step size that is 1.&lt;/p&gt;

&lt;p&gt;But there are other ways to create an array too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.zeros(3) #returns a 1D array of length 3 with all zero elements
np.zeros((5,5))# returns a 2d array of 5 rows and 5 columns
np.zeros((2,3)) 
np.ones((3,4)) # all the elements will be one

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine you have a toy train, and you want it to travel smoothly from one end of your play table to the other. Now, you don't want it to move too fast or too slow – you want it to make exactly 5 stops along the way.&lt;/p&gt;

&lt;p&gt;np.linspace is like your magical train conductor tool in the world of Python and NumPy. It helps you create a list of numbers (like train stops) that are evenly spaced, making your train (or calculations) run smoothly.&lt;/p&gt;

&lt;p&gt;Here's how you would use np.linspace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# Let's say you want your train to start at 0 and end at 10, making 5 stops.
train_stops = np.linspace(0, 10, 5)

print(train_stops)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tips&lt;/strong&gt;&lt;br&gt;
How do you simply understand the dimension of an array by seeing it? Look at the brackets. If there is 1 bracket that means the array is 1D array and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Identity matrix&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Identity matrix is a 2D square matrix where number of rows is equal to the number of columns. The elements in primary diagonals will be 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.eye(4)  # identity matrix of 4*4 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also generate random numbers through numpy libraries with the help of an array. How cool is that ?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;np.random.rand()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This function gives you random numbers between 0 and 1, like picking candies from a jar where each candy is a number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

random_numbers = np.random.rand(3)
print("Random Numbers between 0 and 1:", random_numbers)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;np.random.randint()
It's like a magical dice roll. This function gives you random integers within a specified range.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;random_integer = np.random.randint(1, 7)  # Imagine a 6-sided die
print("Random Integer:", random_integer)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;np.random.randn()
This one is like a mysterious recipe that gives you numbers with a mean of 0 and standard deviation of 1. It's like a magic potion, creating numbers with a special pattern.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;random_standard_numbers = np.random.randn(3)
print("Random Standard Numbers:", random_standard_numbers)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;np.random.choice()
Imagine you have a bag of marbles with numbers on them. This function lets you randomly pick one or more marbles from the bag.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;marbles = np.array([1, 2, 3, 4, 5])
random_pick = np.random.choice(marbles, size=2)
print("Randomly Picked Marbles:", random_pick)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;np.random.shuffle()
It's like shuffling a deck of cards. This function rearranges the order of your numbers randomly.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deck_of_cards = np.array([1, 2, 3, 4, 5])
np.random.shuffle(deck_of_cards)
print("Shuffled Deck of Cards:", deck_of_cards)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5wbklang--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2njli22q90o8bhu97ns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5wbklang--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2njli22q90o8bhu97ns.png" alt="num" width="800" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, these functions are like magic spells for generating randomness in Python with NumPy. They make working with random numbers fun and easy!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RESHAPE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Alright, imagine you have a bunch of building blocks, and you want to rearrange them to make a new shape, like turning a flat square into a tall tower. The reshape function in NumPy is like your magical building block organizer. It helps you change the arrangement of your numbers or data without adding or removing any.&lt;/p&gt;

&lt;p&gt;Here's how you might use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# Imagine you have a flat square of building blocks (numbers 1 to 9)
flat_square = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Now, you want to reshape it into a 3x3 grid, like making a tower with 3 floors and 3 blocks on each floor.
tower = flat_square.reshape(3, 3)

print("Original Flat Square:")
print(flat_square)

print("\nReshaped Tower:")
print(tower)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reshape function is like telling your magical organizer, "Hey, take these building blocks and arrange them in a way that there are 3 rows and 3 columns." And poof! Your flat square turns into a tower with three floors, each having three blocks.&lt;/p&gt;

&lt;p&gt;It's super handy when you want to change the shape of your data without changing the actual data. It's like playing with LEGO blocks and transforming them into different cool structures!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caution&lt;/strong&gt;: Suppose you have an linear array of 25 elements and now you reshape the array into 2D arrays. okay that's fine. simply write new_arr=arr.reshape(5,5). here number of rows times number of column will be 25 which is equivalent to your previous array element number. but if you give (6 times 5) or anything that is greater than 25 it will throw an error due to insufficiency of elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's talk about the max, min, and index-related functions in NumPy, as if we're building with LEGO blocks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;np.max() and np.min()&lt;/strong&gt;&lt;br&gt;
Imagine you have a stack of LEGO towers, each tower representing a set of numbers. Now, you want to find the tallest tower (max) and the shortest tower (min).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

towers = np.array([[2, 5, 8],
                   [3, 9, 1],
                   [7, 4, 6]])

# Finding the tallest tower (max)
tallest_tower = np.max(towers)
print("Tallest Tower:", tallest_tower)

# Finding the shortest tower (min)
shortest_tower = np.min(towers)
print("Shortest Tower:", shortest_tower)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's like looking at your LEGO towers and saying, "Hey, tell me the height of the tallest one and the smallest one." The np.max() and np.min() functions do just that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Index-Related Functions&lt;/strong&gt;&lt;br&gt;
Now, imagine each LEGO tower has numbered floors, and you want to know on which floor is the tallest and the shortest block. NumPy has functions for that too!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
# Finding the index of the tallest block
index_of_tallest = np.argmax(towers)
print("Index of Tallest Block:", index_of_tallest)

# Finding the index of the shortest block
index_of_shortest = np.argmin(towers)
print("Index of Shortest Block:", index_of_shortest)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, it's like saying, "Okay, I found the tallest and shortest towers. Now, tell me on which floor the tallest and the shortest blocks are." The np.argmax() and np.argmin() functions give you the indices (or positions) of the maximum and minimum values in your LEGO towers.&lt;/p&gt;

&lt;p&gt;In summary, these functions are your LEGO tower inspectors. They help you find the tallest and shortest towers and tell you on which floor the special blocks are located!&lt;/p&gt;

&lt;p&gt;Alright, let's dive into the concepts of checking the shape and datatype of your NumPy arrays, imagining we're playing with toys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;np.shape&lt;/strong&gt;&lt;br&gt;
Think of your NumPy array as a toy box with different toys inside. The np.shape function is like asking, "How many rows and columns does my toy box have?"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# Imagine your toy box is a 2x3 grid of toys.
toy_box = np.array([[1, 2, 3],
                    [4, 5, 6]])

# Checking the shape of the toy box
shape_of_toy_box = np.shape(toy_box)

print("Shape of Toy Box:", shape_of_toy_box)

Shape of Toy Box: (2, 3)
It's like saying, "My toy box has 2 rows and 3 columns of toys
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;np.dtype&lt;/strong&gt;&lt;br&gt;
Now, let's talk about the type of toys in your box. The np.dtype function is like asking, "What kind of toys are in my toy box?"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Checking the datatype of the toys in the toy box
datatype_of_toys = toy_box.dtype

print("Datatype of Toys in Toy Box:", datatype_of_toys)
Datatype of Toys in Toy Box: int64

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Alright, let's embark on a magical journey in the world of Harry Potter using NumPy!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mEa9QiZD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq9od5tdbi51qz972d8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mEa9QiZD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq9od5tdbi51qz972d8w.png" alt="code" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Magical Quidditch Match&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine there's a magical Quidditch match happening in the wizarding world. The Quidditch field is represented as a grid, and each cell in the grid corresponds to a magical creature's power level. We want to create a team of wizards and magical creatures to ensure a fair match.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create the Quidditch Field&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# The Quidditch field is a 5x5 grid
quidditch_field = np.zeros((5, 5))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've created a magical field using np.zeros(). Each cell is initialized to zero, symbolizing the absence of magical creatures&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Summon Magical Creatures&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Let's summon magical creatures with random power levels (between 1 and 10)
magical_creatures = np.random.randint(1, 11, size=(5, 5))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have a team of magical creatures with random power levels generated by np.random.randint().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Assign Creatures to the Quidditch Field&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Assign magical creatures to the Quidditch field
quidditch_field = np.maximum(quidditch_field, magical_creatures)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use np.maximum() to ensure that if there's already a magical creature in a cell, we don't replace it with a weaker one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Check the Team's Power&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Find the total power of the magical creatures in each row
&lt;/h2&gt;

&lt;p&gt;row_powers = np.sum(quidditch_field, axis=1)&lt;/p&gt;

&lt;h2&gt;
  
  
  Find the most powerful row
&lt;/h2&gt;

&lt;p&gt;most_powerful_row_index = np.argmax(row_powers)&lt;br&gt;
most_powerful_row_power = np.max(row_powers)&lt;/p&gt;

&lt;p&gt;Here, we use np.sum() and np.argmax() to find the row with the most powerful magical creatures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Shape and Datatype Check&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Check the shape and datatype of the Quidditch field
&lt;/h2&gt;

&lt;p&gt;field_shape = np.shape(quidditch_field)&lt;br&gt;
field_datatype = quidditch_field.dtype&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Summary&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
In this magical scenario, we created a Quidditch field, summoned magical creatures, assigned them to the field, found the most powerful row, and checked the shape and datatype of our magical world.&lt;/p&gt;

&lt;p&gt;Remember, in the wizarding world of NumPy, the arrays are your magical tools, and the functions are your spells!&lt;/p&gt;

&lt;p&gt;Alright, let's imagine you have a magical spell book called NumPy, and it helps you find and modify elements in your magical array using square brackets – just like casting spells!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finding Elements with Bracket Indexing&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# Imagine you have a magical array with numbers
magical_array = np.array([1, 2, 3, 4, 5])

# Let's use bracket indexing to find the second element (remember, we start counting from 0!)
second_element = magical_array[1]

print("Second Element:", second_element)
Second Element: 2

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Slicing with Bracket Indexing&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Now, let's use bracket indexing to get a slice of our magical array
slice_of_array = magical_array[1:4]

print("Slice of Array:", slice_of_array)
Slice of Array: [2 3 4]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Modifying Elements with Bracket Indexing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caution&lt;/strong&gt;: If we try to modify the existing array in numpy, due to memory efficiency, the previous array memory gets lost. so in order to modify multiply or specific elements, we have to create a copy array so both of the arrays remain stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Broadcasting :Numpy arrays differ from a normal Python list because of their ability to broadcast

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Setting a value with index range (Broadcasting)
&lt;/h1&gt;

&lt;p&gt;arr[0:5]=100&lt;/p&gt;

&lt;h1&gt;
  
  
  Show
&lt;/h1&gt;

&lt;p&gt;arr&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


arr=np.random.randint(1,100,10)
arr_copy=arr.copy()
print(arr)


Output: array([66,  4,  1, 40, 97, 12, 97, 39, 88, 84])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(arr_copy[:]=100)

Output: array([100, 100, 100, 100, 100, 100, 100, 100, 100, 100])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(arr)

Output: array([66,  4,  1, 40, 97, 12, 97, 39, 88, 84])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2D Array Indexing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Imagine you have a magical 2D array
magical_2d_array = np.array([[1, 2, 3],
                             [4, 5, 6],
                             [7, 8, 9]])

# Let's use bracket indexing to find an element in this 2D array
element_in_2d_array = magical_2d_array[1, 2]

print("Element in 2D Array:", element_in_2d_array)
Element in 2D Array: 6

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;bracket indexing is like having a magical wand that helps you point to specific elements or ranges in your arrays, and you can even use it to modify them!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;# 2D array slicing&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a=np.random.randint(1,100,50)
a=a.reshape(10,5)
a[4:7,1:4]

print(a)

Output:[[10, 56, 43],
       [ 5, 45, 99],
       [75, 95, 53]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here it means, these are row column representations. 4:7 means from row 4 to row 6, we slice the array and from column 1 to column 3 we slice the array. the elements within this range will show as output.&lt;/p&gt;

&lt;p&gt;Sure thing! Let's explore arithmetic operations in NumPy, imagining we're playing with magical numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NumPy Operations&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np
arr = np.arange(0,10)  [0,1,2,3,4,5,6,7,8,9]

print(arr+arr)  #[ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18]
print(arr*arr)  #[ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81]
print(arr-arr)  #[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print(arr**3)   #[ 0,   1,   8,  27,  64, 125, 216, 343, 512, 729]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But for division there is a caution. in maths, when we divide something by zero we get infinity(error). in python also we get an error. On the bright side, numpy will not throw you an error. rather, it will show the output as a null value and throw you a warning instead. for some negative value divided by zero we will get negative infinity in output with a warning for free.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(arr/arr) 

Output: [ nan,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.]


print(1/arr)

Output: [ inf,  1. ,  0.5,  0.33333333,  0.25 ,0.2 , 0.16666667,  0.14285714,  0.125,  0.11111111]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Universal Array Functions&lt;/strong&gt;&lt;br&gt;
Numpy comes with many universal array functions, which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Taking Square Roots
np.sqrt(arr)
print(arr)

[ 0.  ,  1. ,  1.41421356,  1.73205081,  2. ,2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Calcualting exponential (e^)
np.exp(arr)

#LOg
np.log(arr)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;&lt;u&gt;Numpy Exercises&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks, and then you'll be asked some more complicated questions.&lt;/p&gt;

&lt;p&gt;import numpy as np&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create an array of 10 zeros&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
arr=np.zeros(10,'int')
arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create an array of 10 ones&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr=np.ones(10,'int')
arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;br&gt;
Create an array of 10 fives**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr=np.full(5,10)
arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create an array of all the even integers from 10 to 50&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr=np.arange(10,51,2)
arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a 3x3 matrix with values ranging from 0 to 8&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr=np.arange(0,9).reshape(3,3)
arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**Create a 3x3 identity matrix**
arr
arr=np.eye(3)
arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;**Use NumPy to generate a random number between 0 and 1&lt;/em&gt;*&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.random.rand(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.random.randn(25)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create an array of 20 linearly spaced points between 0 and 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.linspace(0,1,20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;br&gt;
Numpy Indexing and Selection**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])


mat[2:5,1:5]

array([[12, 13, 14, 15],
       [17, 18, 19, 20],
       [22, 23, 24, 25]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;br&gt;
Get the sum of all the values in mat**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.sum(mat)
mat.sum()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Get the standard deviation of the values in mat&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.std(mat)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Get the sum of all the columns in mat&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mat.sum(axis=0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Summary: The NumPy Adventure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the enchanted land of Python, there exists a magical library called NumPy. It's like a spell book that empowers wizards (programmers) to perform incredible feats with arrays and numbers. As we embarked on this NumPy adventure, we uncovered some of its most powerful spells:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Creating Arrays:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.array(): Conjuring arrays from thin air.
np.arange(): Crafting sequences of numbers.
np.zeros() and np.ones(): Summoning arrays filled with mystical zeros and ones.
np.linspace(): Unveiling evenly spaced numbers.
np.identity(): Creating magical identity matrices.
Random Number Magic:

np.random.randint(): Rolling the dice for random integers.
np.random.randn(): Brewing a potion of random standard normal numbers.
np.random.choice(): Drawing numbers from a magical bag.
np.random.shuffle(): Shuffling the deck of numbers.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shape Transformation:

reshape(): Rearranging the layout of magical elements.
Exploring and Analyzing:

np.max() and np.min(): Finding the tallest towers and smallest caves.
np.argmax() and np.argmin(): Locating the peaks and valleys in the mystical landscape.
np.shape(): Revealing the dimensions of the magic grid.
dtype: Uncovering the type of magical essence within.
Fancy Indexing:

Using arrays of indices to precisely pick out or modify elements.
Magical Arithmetic:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;_Conjuring spells like addition, subtraction, multiplication, and division on arrays.&lt;br&gt;
Our journey through the NumPy realm has equipped us with mighty tools and incantations. Armed with this knowledge, you can now weave your own spells and perform powerful transformations on arrays in Python. May your NumPy adventures continue, and may your code be as magical as the wizarding world itself! 🧙✨&lt;br&gt;
_&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sayoonara~~&lt;br&gt;
Good night!&lt;br&gt;
Signing off tata!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7wRA0W9Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9ig00do3ydcz08m90ja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7wRA0W9Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9ig00do3ydcz08m90ja.png" alt="end" width="712" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>numpy</category>
      <category>arrays</category>
      <category>data</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
