Tag Archives: Python 101

OpenPyXL – Working with Microsoft Excel Using Python

The business world uses Microsoft Office. Their spreadsheet software solution, Microsoft Excel, is especially popular. Excel is used to store tabular data, create reports, graph trends, and much more. Before diving into working with Excel with Python, let’s clarify some special terminology:

  • Spreadsheet or Workbook – The file itself (.xls or .xlsx).
  • Worksheet or Sheet – A single sheet of content within a Workbook. Spreadsheets can contain multiple Worksheets.
  • Column – A vertical line of data that is labeled with letters, starting with “A”.
  • Row – A horizontal line of data labeled with numbers, starting with 1.
  • Cell – A combination of Column and Row, like “A1”.

In this article, you will be using Python to work with Excel Spreadsheets. You will learn about the following:

  • Python Excel Packages
  • Getting Sheets from a Workbook
  • Reading Cell Data
  • Iterating Over Rows and Columns
  • Writing Excel Spreadsheets
  • Adding and Removing Sheets
  • Adding and Deleting Rows and Columns

Excel is used by most companies and universities. It can be used in many different ways and enhanced using Visual Basic for Applications (VBA). However, VBA is kind of clunky — which is why it’s good to learn how to use Excel with Python.

Let’s find out how to work with Microsoft Excel spreadsheets using the Python programming language now! Continue reading OpenPyXL – Working with Microsoft Excel Using Python

Python 101: An Intro to Working with JSON

JavaScript Object Notation, more commonly known as JSON, is a lightweight data interchange format inspired by JavaScript object literal syntax. JSON is easy for humans to read and write. It is also easy for computers to parse and generate. JSON is used for storing and exchanging data in much the same way that XML is used.

Python has a built-in library called json that you can use for creating, editing and parsing JSON. You can read all about this library here:

It would probably be helpful to know what JSON looks like. Here is an example of JSON from https://json.org:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

From Python’s point of view, this JSON is a nested Python dictionary. You will find that JSON is always translated into some kind of native Python data type. In this article, you will learn about the following:

  • Encoding a JSON String
  • Decoding a JSON String
  • Saving JSON to Disk
  • Loading JSON from Disk
  • Validating JSON with json.tool

Continue reading Python 101: An Intro to Working with JSON

Python 101 – Learning About Loops (Video)

In this tutorial, you will learn how to use for and while loops in Python.

Specifically, you’ll learn how to:

  • Create a for loop
  • Loop over a string
  • Loop over a dictionary
  • Extract multiple values from a tuple
  • Use enumerate with loops
  • Create a while loop
  • Breakout of a loop
  • Use continue
  • Use else with loops
  • Nest loops

Would you like to learn more about Python?

Python 101 – 2nd Edition

Purchase now on Leanpub

Python 101 – Debugging Your Code with pdb

Mistakes in your code are known as “bugs”. You will make mistakes. You will make many mistakes, and that’s totally fine. Most of the time, they will be simple mistakes such as typos. But since computers are very literal, even typos prevent your code from working as intended. So they need to be fixed. The process of fixing your mistakes in programming is known as debugging.

The Python programming language comes with its own built-in debugger called pdb. You can use pdb on the command line or import it as a module. The name, pdb, is short for “Python debugger”.

Here is a link to the full documentation for pdb:

In this article, you will familiarize yourself with the basics of using pdb. Specifically, you will learn the following:

  • Starting pdb in the REPL
  • Starting pdb on the Command Line
  • Stepping Through Code
  • Adding Breakpoints in pdb
  • Creating a Breakpoint with set_trace()
  • Using the built-in breakpoint() Function
  • Getting Help

While pdb is handy, most Python editors have debuggers with more features. You will find the debugger in PyCharm or WingIDE to have many more features, such as auto-complete, syntax highlighting, and a graphical call stack.

A call stack is what your debugger will use to keep track of function and method calls. When possible, you should use the debugger that is included with your Python IDE as it tends to be a little easier to understand.

However, there are times where you may not have your Python IDE, for example when you are debugging remotely on a server. It is those times when you will find pdb to be especially helpful.

Let’s get started! Continue reading Python 101 – Debugging Your Code with pdb

Python 101 – Launching Subprocesses with Python

There are times when you are writing an application and you need to run another application. For example, you may need to open Microsoft Notepad on Windows for some reason. Or if you are on Linux, you might want to run grep. Python has support for launching external applications via the subprocess module.

The subprocess module has been a part of Python since Python 2.4. Before that you needed to use the os module. You will find that the subprocess module is quite capable and straightforward to use.

In this article you will learn how to use:

  • The subprocess.run() Function
  • The subprocess.Popen() Class
  • The subprocess.Popen.communicate() Function
  • Reading and Writing with stdin and stdout

Let’s get started! Continue reading Python 101 – Launching Subprocesses with Python

Python 101 – Working with Files

Application developers are always working with files. You create them whenever you write a new script or application. You write reports in Microsoft Word, you save emails or download books or music. Files are everywhere. Your web browser downloads lots of little files to make your browsing experience faster.

When you write programs, you have to interact with pre-existing files or write out files yourself. Python provides a nice, built-in function called open() that can help you with these tasks.

In this chapter you will learn how to:

  • Open files
  • Read files
  • Write files
  • Append to files

Let’s get started! Continue reading Python 101 – Working with Files