Python Data Types Part Two

In this tutorial you will learn:

  1. Strings
  2. List
  3. Tuple
  4. Set
  5. Dictionary

1 Strings:

We have already been using this type in prevoius tutorials, this data type is actualy a sequence of characters enclosed within single (“) or double quotes (”“).
Strings (called “str” in python) are an immutable sequence which means we can’t change any of the charachters in the sequence.
Lets make this more clear to you with an example:

0name = "Itachi"
1
2#OUTPUT: I t a c h i
3print(name[0], name[1], name[2], name[3], name[4], name[5])
4
5#OUTPUT: TypeError 'str' object doesn't support items assignment 
6name[0] = 'K'

Like we said before strings are just a sequence of charachters each charachter has its own index number, the index numbers start with zero, this mean that the first charater in our sequence is at the index 0 and the second character is at the index 1 and so on.
To access any of these charachters we use the indexing method by typing the variable name that contain the string value followed by an opend square bracket ‘[’ and a closed square bracket ‘]’ and in between these brackets we put the index number of the charachter that we want to access it (variable_name[index_number]).
In the last statment we have tried to assign the value ‘K’ to the first index which is 0, but when we run the code we get a TypeError telling us that ‘str’ doesn’t support items assignment, because the strings are immutable.
Now lets see another example:

0#OUTPUT: SyntaxError
1var = 'I can't stop coding'
2
3#Solutions
4#var = "I can't stop coding"
5#var = 'I can\'t stop coding'
When you run this code you will get a SyntaxError caused by the single quote in (can’t).
When you strat a string with a single quote the python interpreter will search for the second single quote which define the end of the string, and in our case the interpreter has found the single quote before the end of our string which caused the syntax error.
To avoid this error we can use two methods the first is to use double quotes when we want to use single quote in our charachters sequence or we use the single quote when we want to use double quotes in our charachters sequence.
The second method is to use the backslash symbol (\) to escape any single or double quotes used in our charachters sequence.

 0'''
 1If you start your string with single quote you escape only the single quotes
 2that are presented in your string.
 3'''
 4var = 'text that contain both "double quotes" and \'single quotes\''
 5
 6'''
 7If you start your string with double quotes you escape only the double quotes
 8that are presented in your string.
 9'''
10var1 = "text that contain both \"double quotes\" and 'single quotes'"

You can use the str() built-in function to convert a number to str.

0var = 45
1
2#OUTPUT: <class 'int'>
3print(type(var))
4
5var = str(var)
6
7#OUTPUT: <class 'str'>
8print(type(var))

You will learn more about strings in the next tutorial.

2 List:

List is a mutable sequence of items, which means that we can change the items in the list.
List items can be all of the same type or different types.
List items are enclosed within brackets [].

 0var = [1, 2, 3, 5.5, 7.3, 'hi']
 1
 2#OUTPUT: <class 'list'>
 3print(type(var))
 4
 5#OUTPUT: 1 5.5 hi
 6print(var[0], var[3], var[-1])
 7
 8#Replace list items
 9var[0] = 44
10
11#OUTPUT: 44
12print(var[0])
In this example we have created a list that contain some items with different types int, float and str.
To access these items we use the same indexing method that we have used with strings.
The new syntax here is “var[-1]“, the -1 means the last item in the sequence, we can use the negative numbers in the indexing method to access the items from the right side of the sequence: (-2 means the second item from the right and -3 is the third item from the right and so on …).

3 Tuple:

Tuple is same as list, both are an ordered sequence of items but the difference between tuple and list is that tuple is immutable which means that we can’t modify its content, and the items are enclosed within parentheses ‘()’.

0var = (1, 2, 3, 5.5, 7.3, 'hi')
1
2#OUTPUT: <class 'tuple'>
3print(type(var))
4
5#OUTPUT: 1 5.5 hi
6print(var[0], var[3], var[-1])
As you see we access the tuple’s items using the same method that we have used in both list and str.
Note: To create a tuple that contain only one item you need to add comma after the item: (1,), if you don’t do that the interpreter will consider it as an integer assginment not a tuple.

4 Set:

Set is an unordered collection of unique items enclosed within curly braces ‘{}’, this mean that we can’t use the indexing method to access the items and all the items are uniques, that mean no repeated items.

 0var = {1, 2, 3, 4, 4, 2, 5, 1, 1, 'hi', 'hi'}
 1
 2#OUTPUT: <class 'set'>
 3print(type(var))
 4
 5#OUTPUT: {1, 2, 3, 4, 5, 'hi'}
 6print(var)
 7
 8#OUTPUT: TypeError
 9print(var[0])
When you run this code, the second print function “print(var)” will print only unique items all the repeated items are removed.
When we try to access the first item using the indexing method we get an error, because set doesn’t support the indexing method.
Note: When you print your set, the result won’t be always ordered like list and tuple.
You will learn more about sets in an upcoming tutorial.

5 Dictionary:

Dictionary is an unordered collection of Key-Value pairs enclosed within curly braces ‘{}’ and each Key-Value is seperated by colon ‘:‘.
The key can be any immutable type like tuple, string and numbers.
The value can be any type.

0var = {'key':'value', 'banana':35}
1
2#OUTPUT: <class 'dict'>
3print(type(var))
4
5#OUTPUT: 35
6print(var['banana'])
As you see in the first statement we have created our dictionary then we have printed the type of our variable, and finally we have printed the value of the key ‘banana’, this is how we access the values in our dictionary.

In an upcoming tutorials you will learn more about these types (string, list, set, dict).

Adnan

Read more posts by this author.