Tag Archives: Python 101

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

Python 101 – Learning About Tuples

Tuples are another sequence type in Python. Tuples consist of a number of values that are separated by commas. A tuple is immutable whereas a list is not. Immutable means that the tuple has a fixed value and cannot change. You cannot add, delete or modify items in a tuple. Immutable objects are useful when you need a constant hash value. The most popular example is the key to a Python dictionary.

In this article, you will learn how to:

  • Create tuples
  • Work with tuples
  • Concatenate tuples
  • Special case tuples

Let’s find out how to create tuples! Continue reading Python 101 – Learning About Tuples

What’s New in Python 101 2nd Edition

The original Python 101 was the first book I had ever written. In deciding to write a 2nd edition, I needed to decide what I should keep and what I should remove from the book. What I ended up doing was rewriting the book from the ground up.

Python 101 2nd Ed Kickstarter

In the original book, Python 101 was based on Python 3.5 and had the following sections:

  • Part I – Learning the basics
  • Part II – Learning from the library
  • Part III – Intermediate Odds and Ends
  • Part IV – Tips, tricks and tutorials
  • Part V – Packaging and Distribution

For the 2nd edition, I am dropping part II as most of that information can be covered in later chapters. Now that Python 3.8 is out, I am able to cover such things as

  • f-strings
  • assignment expressions
  • dictionaries being ordered by default
  • type hinting
  • source control
  • review questions for most chapters
  • and more!

And mention other neat things, like sub-interpreters, the futures module, and audit hooks, although those won’t be covered in detail. I am also planning on making the tutorials section into a more proof-of-concept section where you will have little useful scripts that you can take and use. That was already there to some degree, but I think these scripts will be even better than the original ones.

The distribution section will also get a makeover to cover the latest conventions for distributing your code to the Python Packaging Index. The original book’s coverage is out of date.

Also while the first book mentioned lots of different options for creating executables, the new version of the book will focus on only one or two, but it will show how to create a binary for Windows and Mac. I am investigating how to support Linux, but that may or may not be included.

I will be making quite a few chapters available in the free sample available on Leanpub so people can check it out before purchasing.

I hope you’ll check out the book. Feel free to ask questions in the comments or send me an email.

Python 101 2nd Edition will be released in September 2020

Python 101: Learning About Lists

Lists are a fundamental data type in the Python programming language. A list is a mutable sequence that is typically a collection of homogeneous items. Mutable means that you can change a list after its creation. You will frequently see lists that contain other lists. These are known as nested lists. You will also see lists that contain all manner of other data types, such as dictionaries, tuples or objects.

Let’s find out how you can create a list!

Creating Lists

There are several ways to create a list. You may construct a list in any of the following ways:

  • Using a pair of square brackets with nothing inside creates an empty list: []
  • Using square brackets with comma-separated items: [1, 2, 3]
  • Using a list comprehension: [x for x in iterable]
  • Using the list() function: list(iterable)

An iterable is a sequence, a container that supports iteration or an iterator object. Lists themselves are sequences as are strings.

Let’s look at a few examples of creating a list so you can see it in action: Continue reading Python 101: Learning About Lists

Python 101: Working with Dates and Time

Python gives the developer several tools for working with dates and time. In this article, we will be looking at the datetime and time modules. We will study how they work and some common uses for them. Let’s start with the datetime module!

The datetime Module

We will be learning about the following classes from the datetime module:

  • datetime.date
  • datetime.timedelta
  • datetime.datetime

These will cover the majority of instances where you’ll need to use date and datetime object in Python. There is also a tzinfo class for working with time zones that we won’t be covering. Feel free to take a look at the Python documentation for more information on that class. Continue reading Python 101: Working with Dates and Time