Sequences - Python Bootcamp: The Crash Course for Understanding the Basics of Python Computer Language (2016)

Python Bootcamp: The Crash Course for Understanding the Basics of Python Computer Language (2016)

Chapter 4. Sequences

Sequences, one of the basic structures in programming, allow you to save values easily and efficiently. Python supports three types of sequences, namely: lists, tuples, and strings. Let’s discuss each sequence in detail:

Lists

As their name suggests, lists are collections of values that follow a certain arrangement. You can use square brackets to create a list. For instance, you can use the statement below to initialize an empty list:

You should use commas to separate values. Here’s a sample list:

You can place different kinds of values inside the same list. For instance, the list above holds numbers and letters.

Similar to characters within a string, you can access listed items using indices that start at zero. Accessing a listed item is easy. You just have to specify the name of the list where that item belongs. Then, indicate the number of the item inside the list. Enclose the number using square brackets. Here’s an example:

Python also allows you to enter negative integers. These numbers are counted backwards, starting from the last item in the list.

You may use len() to determine the quantity of items inside a list. Check the image below:

Lists are similar to typical variables in one aspect: they allow you to change the items inside them. Analyze the following example:

ou can also slice strings:

Python offers different methods of adding items to any list. However, the easiest method is this:

To remove items, you can apply the“del” statement onto the list. Here’s an example:

Lists automatically“fix” themselves after each item deletion. That means you won’t see any gap in the numbering of items.

Tuples

Tuples and lists are similar except for one thing: tuples cannot be edited. After creating a tuple, you won’t be able to change it in any way. You can’t expand, edit, or delete the elements within a tuple. If you’ll ignore this immutability, you can say that lists and tuples are identical.

You should use commas when declaring tuples:

Sometimes, you have to differentiate tuples using parentheses. This process is similar to performing several assignments using the same line. Here’s a simple example:

Strings

You’ve already learned about strings. However, it is important to discuss it again as a Python sequence. For other programming languages, you can access the characters elements inside strings using square brackets (known as the subscript operator). This method is also effective in Python:

Python assigns numbers to indices using this formula: 0– n1 (n represents the number of characters in the string). Check the screenshot below:

Indices work with the characters that come right after them. For negative indices, you should count backwards:

Unlike other programming languages, Python allows you to place up to 2 numbers inside square brackets. You can do this using a colon (i.e.“:”). For sequences that concentrate on numeric indices, the combination of brackets and colons returns the portion between the indices. This technique is called“slicing.” If you’ll slice a string, you will get "substrings.” Analyze the screenshot below:

The statements given above show an important rule:

“If you’ll omit a number, Python assumes the missing number as the start or end of that particular sequence (depending on the position of the missing number).”

Dictionaries

Dictionaries are similar to lists. Unlike tuples, dictionaries allow users to modify their content. That means you may add, edit, and delete the elements of any dictionary. The main difference between lists and dictionaries is this: dictionaries don’t bind their elements to any number.

A dictionary’s element has two aspects: (1) the key and (2) the value. If you’ll call the key of a dictionary, you’ll get the values related to that particular key. Computer programmers consider lists as special dictionaries, where numbers represent the key of each element.

How to Use a Dictionary

You should use curly braces when declaring a dictionary. Also, you should use the following format when declaring elements for a dictionary: (1) enter the key of the element, (2) add a colon, and (3) assign the value. Here’s an example:

Additionally, adding elements to dictionaries is simple and easy. It’s like adding an ordinary variable: