Python Tutorial 1: Types & Operations

This first Python tutorial will cover the basic types and operations that form the building blocks of the Python syntax. These types in Python can be defined as:

  • Strings
  • Numbers
  • Lists
  • Dictionaries
  • Tuples
  • Files



  • Strings assume a positional ordering among items
  • You can verify the length of a string with the built in function len()
  • You can fetch components of a string with X[position]

You can also use slicing to extract a piece of a string. Slicing extracts a section of a string instead of an individual character.

This general form X[I:J] means “give me everything in X from offset I up to but NOT including offset J.”

In a slice, the left bound defaults to zero, and the right bound defaults to the length of the string.

Strings support repetition and concatenation

Notice that the plus sign (+) and asterisk (*) do different things for different objects, for strings they represent concatenation and repetition, for numbers they represent addition and multiplication. This is a general property of Python called Polymorphism which will be discussed later on.

Also notice that in these previous operations we did not change the original string; that’s because strings are immutable– They cannot be changed after they are created. Every string operation is defined to produce a new string as its result.

To embed a quote in a string, you can “escape” the quote with a backslash.

An escape sequence always begins with a backslash followed by a character that represents what you would like to do. For example, “\n” is a new line “\t” is a horizontal tab and “\’” is an apostrophe.

To convert a string to a number and a number to a string you must use conversion tools, specifically, int() and str().

If you try and add a string and an integer together you get the error above.

If you convert s to an integer then the plus sign performs addition giving the sum 43. Conversely, if you convert i to a string then the plus sign concatenates the string to ‘421’.



Python supports the standard object types for numbers: integers (whole numbers), floating-point numbers (include decimal point), as well as several more obscure types (long numbers, fixed precision…)

Python supports the standard mathematical operations: +, – , *, /, and ** for exponent.

The “L” at the end of the last number represents Python converting up to a long integer format to try and provide extra precision.



In Python, Lists are “positionally ordered collections of arbitrarily typed objects, and they have no fixed size.”  Unlike strings, Lists are mutable; they can be modified in-place by assignment to offsets. Lists are characterized by brackets with each list item separated by a comma.

We can perform a lot of the same actions on lists as strings. i.e. index, slice, concatenate…

If we want to add another list item to a list, we have to use append() to automatically add a list item to the end of the current list.

Other list operations include sort() (sorts list alphabetically), reverse() (reverses the order of the list), and pop() (removes a list item at a certain index)

Lists also support nesting which allows Lists to be used to represent multi-dimensional matrices.



Up to this point the object types we have learned about have been sequences because they index from left to right. When you want to look at a specific instance in a sequence you call it by its position in the index. Dictionaries are the only core Python object that are not sequences; Dictionaries are known as mappings because they map a key to a value in a Dictionary.

Notice that a Dictionary is syntactically notated with curly braces and key-value pairs with a colon in between.

With Dictionaries it is good practice to perform an audit check on the existence of a key before performing an action on that key. This will prevent an ugly system error and instead, provide a Boolean true or false. This can be done with the built-in function, has_key().

Alternatively, using the  get()  function allows you to set a default answer  if the key does not exist.

Dictionaries also use the same pop() function as lists to delete a key-value pair, the only difference is that you provide a key to delete instead of an index location.



Tuples (pronounces “tuhple” or “toople”) are exactly like a list except they are immutable, they cannot be changed. They are created syntactically with parenthesis instead of brackets and are only useful if you need to pass information that must remain constant, for data integrity.



File objects are Python’s main interface to external files on your computer. There is no specific literal syntax to call a File object, instead you use the built in function open(). When calling this function you pass 2 things, the filename and the action to perform on that file.



  1. Define a string ‘S’ of four characters where S = ‘spam’. Write an assignment that changes the string to ‘slam’, using only slicing and concatenation.
  2. Make a dictionary with key-value pairs for your first and last name, your favorite color, a hobby you enjoy, and your favorite food. Then write your dictionary to a new text document (.txt) called “myInfo” in the following format:

My Info
First Name: Blah
Last Name: Maxwell
Favorite Color: Green
Hobby: Eating snow
Favorite food: Anchovies


Next Up

Once you have a firm understanding of Python Types & Operations, Proceed to Part 2 of this Python tutorial: Statements, Syntax & Functions.

Tim Schnell

Writer of software/english, tech nerd, and genetically predisposed to fight with words instead of actions.