Tag Archives: Python 101

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

Python 101 – Exception Handling

Creating software is hard work. To make your software better, your application needs to keep working even when the unexpected happens. For example, let’s say your application needs to pull information down from the Internet. What happens if the person using your application loses their Internet connectivity?

Another common issue is what to do if the user enters invalid input. Or tries to open a file that your application doesn’t support.

All of these cases can be handled using Python’s built-in exception handling capabilities, which are commonly referred to as the try and except statements.

In this article you will learn about:

  • Common exceptions
  • Handling exceptions
  • Raising exceptions
  • Examining exception objects
  • Using the finally statement
  • Using the else statement

Let’s get starting by learning about some of the most common exceptions. Continue reading Python 101 – Exception Handling

Python 101 – Learning About Loops

There are many times when you are writing code that you will need to find a way to iterate over something. Perhaps you’ll need to iterate over the letters in a string or the objects in a list. The process of iterating over something is done via a loop.

A loop is a programming construct that allows you to iterate over chunks. Those chunks could be the letters in the string or the lines of a file.

In Python, there are two types of loop constructs:

  • The for loop
  • The while loop

Besides iterating over sequences, you can use a loop to do the same thing multiple times. One example is a web server that is basically an infinite loop. A server waits, listening for a client to send it a message. When it receives the message, the loop will call a function in response.

Another example is the game loop. When you beat a game or lose a game, the game doesn’t usually exit. Instead, it will ask you if you want to play again. This is also done by wrapping the entire program in a loop.

In this chapter you will learn how to:

  • Create a for loop
  • Loop over a string
  • Loop over a dictionary
  • Extract multiple values from a tuple
  • Using enumerate with loops
  • Creating a while loop
  • Breaking out of a loop
  • Using continue
  • Loops and the else statement
  • Nesting loops

Let’s get started by looking at the for loop! Continue reading Python 101 – Learning About Loops

Type Checking in Python

Type checking or hinting is a newer feature of Python that was added in Python 3.5. Type hinting is also known as type annotation. Type hinting is adding special syntax to functions and variable declarations that tell the developer what type the argument or variable is.

Python does not enforce the type hints. You can still change types at will in Python because of this. However some integrated development environments, such as PyCharm, support type hinting and will highlight typing errors. You can also use a tool called Mypy to check your typing for you. You will learn more about that tool later on in this article.

You will be learning about the following:

  • Pros and Cons of Type Hinting
  • Built-in Type Hinting / Variable Annotation
  • Collection Type Hinting
  • Hinting Values That Could be None
  • Type Hinting Functions
  • What To Do When Things Get Complicated
  • Classes
  • Decorators
  • Aliasing
  • Other Type Hints
  • Type Comments
  • Static Type Checking

Let’s get started! Continue reading Type Checking in Python

Python 101 – Working with Strings

You will be using strings very often when you program. A string is a series of letters surrounded by single, double or triple quotes. Python 3 defines string as a “Text Sequence Type”. You can cast other types to a string using the built-in str() function.

In this article you will learn how to:

  • Create strings
  • String methods
  • String formatting
  • String concatenation
  • String slicing

Let’s get started by learning the different ways to create strings!

Continue reading Python 101 – Working with Strings

Python 101 – Learning About Dictionaries

Dictionaries are another fundamental data type in Python. A dictionary is a key, value pair. Some programming languages refer to them as hash tables. They are described as a mapping object that maps hashable values to arbitrary objects.

A dictionary’s keys must be immutable, that is, unable to change. Starting in Python 3.7, dictionaries are ordered. What that means is that when you add a new key, value pair to a dictionary, it remembers what order they were added. Prior to Python 3.7, this was not the case and you could not rely on insertion order.

You will learn how to do the following in this chapter:

  • Create dictionaries
  • Access dictionaries
  • Dictionary methods
  • Modifying dictionaries
  • Deleting from your dictionary

Let’s start off by learning about creating dictionaries! Continue reading Python 101 – Learning About Dictionaries