If you’re new to Python and want to start running scripts, you’ll be glad to know it’s much easier than you might think. In just a few steps, you can install Python, write your first script, and even mess around with more advanced stuff. I’ll walk you through everything, from installing Python to running a script that reads an Excel file and splits names into first and last names.

Step-by-Step Guide to Install Python and Run Your First Script

This step-by-step guide will walk you through the process of installing Python on your computer and running your very first script. Follow these simple instructions to get started with Python and begin your coding adventure.

Step 1: Install Python

For Windows:

  1. Download Python:
  • Head over to the official Python website.
  • Hit the download button for the latest version of Python to get the installer.
  1. Run the Installer:
  • Find the downloaded file and run it.
  • Make sure to check the box that says “Add Python to PATH” before clicking “Install Now”.
  • Follow the on-screen steps to finish the installation.

For macOS:

  1. Download Python:
  • Go to the official Python website.
  • Click the download button for the latest version of Python to get the installer.
  1. Run the Installer:
  • Locate the downloaded file and run it.
  • Follow the on-screen steps to finish the installation.

For Linux:

  1. Use the Package Manager:
  • Open a terminal.
  • Use these commands to install Python (commands might vary based on your Linux version):
    bash sudo apt update sudo apt install python3

Step 2: Verify the Installation

  1. Open Command Prompt (Windows) or Terminal (macOS/Linux).
  2. Check the Python Version:
   python --version

or

   python3 --version

You should see the installed version of Python.

Step 3: Write a Python Script

  1. Open a Text Editor:
  • You can use any text editor (Notepad, Notepad++, VS Code, Sublime Text, etc.).
  1. Write Your Script:
  • For example, create a file called hello.py with the following content:
    python print("Hello, World!")
  1. Save the File:
  • Save the file with a .py extension, like hello.py.

Step 4: Run the Python Script

  1. Open Command Prompt (Windows) or Terminal (macOS/Linux).
  2. Navigate to the Directory Containing Your Script:
   cd path/to/your/script
  1. Run the Script:
   python hello.py

or

   python3 hello.py

You should see the output Hello, World!.

Example: Manipulating an Excel File with Python

Let’s create a Python script to read an Excel file with a single column of names, split the names into first names and last names, and save the modified file.

  1. Install Required Libraries:
  • You’ll need the pandas library to work with Excel files. Install it using pip:
    bash pip install pandas openpyxl
  1. Write the Script:
  • Create a new file called names.py on your Desktop and add the following code:
import pandas as pd

# Load the Excel file
file_path = '/path/to/your/Desktop/names.xlsx'
df = pd.read_excel(file_path)

# Split the 'Name' column into 'First Name' and 'Last Name' columns
df[['First Name', 'Last Name']] = df['Name'].str.split(' ', 1, expand=True)

# Save the modified file
output_path = '/path/to/your/Desktop/names_modified.xlsx'
df.to_excel(output_path, index=False)

print("Excel file has been modified and saved successfully.")
  1. Run the Script:
  • Open Command Prompt or Terminal.
  • Navigate to the Desktop:
    bash cd Desktop
  • Run the script:
    bash python names.py
    or
    bash python3 names.py

This script will read the names.xlsx file from your Desktop, split the single “Name” column into “First Name” and “Last Name” columns, and save the modified file as names_modified.xlsx on your Desktop.

Live Example

Here’s how you can run the script with the provided Excel file:

import pandas as pd

# Load the Excel file
file_path = '/mnt/data/names.xlsx'
df = pd.read_excel(file_path)

# Split the 'Name' column into 'First Name' and 'Last Name' columns
df[['First Name', 'Last Name']] = df['Name'].str.split(' ', 1, expand=True)

# Save the modified file
output_path = '/mnt/data/names_modified.xlsx'
df.to_excel(output_path, index=False)

import ace_tools as tools; tools.display_dataframe_to_user(name="Modified Excel Data", dataframe=df)

This script reads the Excel file, splits the “Name” column into “First Name” and “Last Name” columns, and saves the modified file. You can now check the modified Excel file to see the changes.

Conclusion

Running Python scripts is pretty straightforward and can make your life a lot easier by automating your workflow. With just a few steps, you can install Python, write a script, and do things like messing around with Excel files. Python is super versatile and easy to use, so it’s a great choice whether you’re just starting out or already know a bit about programming. Happy coding!

Categories: Python

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *