Python Tutorial: Part Two

Welcome to part 2 of our tutorial on the Python programming language. While the first part explained white space, comments, functions, and variables, in this blog we’ll review math and logic, data types, and handy resources for further learning.

If you need to review our earlier explanations, click here to return to part one of the Python tutorial. Otherwise, read on for an explanation of these basic Python concepts that you’ll use when working with Python and data analysis.

Math and Logic in Python

Python is frequently used for mathematical purposes. Even if you aren’t strictly using it for math, you’ll encounter math while programming, as it’s an integral part of writing code. Just above, you saw how we used addition to combine two variables and output their combined values to the console. Math doesn’t need to be big and scary – sometimes it’s just as simple as that!

Ready to jump in? Sign up for the Data Analytics or Data Science Porogram today to launch your data career.

Take a look at the mathematical operators as they’re written out in Python:

  • + represents addition
  • - represents subtraction
  • * represents multiplication
  • / represents division
  • ** represents exponentiation, where a number is squared, cubed, and so on
  • % represents modulus, which is used to get the remainder of a division

Additionally, there are several logical operators that are frequently used in Python programming. Check them out:

  • < represents less than
  • <= represents less than or equal to
  • > represents greater than
  • >= represents greater than or equal to
  • == represents exactly equal to
  • != represents not equal to
  • ! represents not
  • | represents or
  • & represents and

Python Data Types

Python has a number of data types that are built-in to its programming, all of which do different things. You’ve already encountered a couple of them earlier: strings, when we assigned a string of text to a variable, and numbers, when we assigned numeric values to a variable.

The data type of a variable is determined by the data that’s stored in it. So, x isn’t always just x; it can variously be a text datatype, a numeric datatype, and so on. It all depends on what data is assigned to it!

There are five major data types in Python programming. There are also a few other types, but for now you don’t need to worry about them. For the purposes of beginner programming, pay attention to the following data types:

  • Strings
  • Numbers
  • Lists
  • Tuples
  • Dictionaries

Let’s look at each of these types in depth!

String Data Type

A string, known in Python as the shorthand str, is a string of alphabetical characters. This can be a name, a word, a phrase, and more. When we declared that x = “I’m ready for the 21DDC!”, x became a string data type. Pay attention to the quotation marks that surround the text we assigned to x. Strings can be declared in Python using either single or double quotation marks, but using them is necessary. Without the quotation marks, the interpreter won’t know that what you’re writing is a string.

Numeric Data Type

Numbers are essential when programming, and Python recognizes a few different types of numbers. Within Python, there are three types of numeric values you’ll encounter: int, float, and complex. What Python knows as int means a simple number, like 6 or 7 or 20024. What it calls a float is a decimal number, like 6.7 or 9.0000001. Finally, what it calls a complex is an algebraic number, like 44 or 6002.

List Data Type

Remember earlier on, when we assigned multiple variables the same value? Well, doing it in reverse will do something else: generate a list. In Python, a list is identified by the square brackets around the values. Within the brackets, the values are separated by commas. For example:

x = [“duck”, 7, “goose”, 100, “pigeon”, 204]

The above code block is a list. See how we used both string data types and numeric data types within the list? That’s because a list can contain items of any data type.

Tuple Data Type

It sounds strange and esoteric, but the central idea of a tuple is very simple. A tuple is nearly identical to a list, with one key difference: whereas lists are mutable, tuples are immutable. This means that the data contained within a list can be changed, but a tuple’s data can never change. Look at the earlier example of the list we saw: using functions, we can, say, remove “goose” from the list. But if that list was a tuple, we wouldn’t be able to do that. Tuples can be distinguished from lists by the bracket type. Tuples use round brackets, rather than square ones. Here’s an example of a tuple:

x = (“kitchen”, 100, “bedroom”, 74).

Dictionary Data Type

The last major data type in Python is the dictionary. Similar to lists, the dictionary is a mutable container that stores data. However, the dictionary contains data that is mapped against a set of keys. Dictionaries are distinguished by the curly brackets that enclose its data. The list we earlier used is random, and doesn’t mean anything. However, if we turned it into a dictionary, it would look like this:

x = {“duck” : 7 , “goose” : 100 , “pigeon” : 204}.

Within a dictionary, colons separate a key from its value. So now the string “duck” corresponds with 7, “goose” with 100, and so on. Each key, on the left side of a colon, corresponds with a value, on the right side of a colon.

Python Resources for Further Learning

Learning a programming language is a journey, and to embark on it properly you should cultivate your curiousity and excitement. To learn more about Python, check out the following resources: