Python

Learn Python for Beginners – Part 1: Variables, Strings, Lists, Dictionaries, and Sets


In this tutorial you’ll learn basics of the Python programming language. The first part covers variables, strings, lists, dictionaries, and sets.

The tutorial uses Jupyter Notebook as the Python development environment. If you have not installed Jupyter Notebook yet take a look at Getting Started With Jupyter Notebook For Python.

Let’s start with a first statement printing out the text Hello World:

print('Hello World')

As a result you’ll get the text Hello World printed out:

>> Hello World

Comments

If you’d like to include comments in your Python code you can use the #-sign. You can choose to add the the #-sign at the beginning of the line or to include the comment at the end of the line:

# One line comment starts with hash
print('Hello World') # comment at the end of the line

Variables

Using variables in Python is easy. You do not need to declare variables. Instead you can choose a name and assign the value directly as you can see in the following:
var1 = 2
Now we can print our the value of var1 by using the following line of code:
print(var1)
 As a result you’ll get the value:
>> 2
In the following piece of code we’re defining a second variable with name var2 and a third variable with name var3. var3 gets assigned the sum of var1 and var2. Finally a print statement is used to print out the sum:
var2 = 3
var3 = var1 + var2
print('The sum of var1 and var2 is {}'.format(var3))
 As a result the following text should be outputted:
>> The sum of var1 and var2 is 5

Strings

In Python you can define strings by using single quotes or double quotes:
# Single Quote String
'This is a string'
# Double Quote String
"This is a string"
Of course, it’s possible to assign a string to a variable, e.g. string1:
string1 = 'This is a string'
Printing out the value of string1
print(string1)
will then print out the string:
>> This is a string
In Python it’s possible to access single characters of the string by using brackets and passing in the index. As indexing always starts at 0 the following line of code:
string1[1]
will return the second character of the string:
>> 'h'
By adding a colon you’ll get back a substring starting from index 1 until the end of the string:
string1[1:]
Result:
>> 'his is a string'
Adding another number of the colon is returning a substring from index 5 to index 7 (not including):
string1[5:7]
Result:
>> 'is'

Lists

Another important feature of the Python programming language are lists. You can define a list by using brackets and including the elements of the list separated by a comma:
list1 = [1,2,3]
Printing our the value of list1now:
print(list1)
 leads to the following result:
>> [1, 2, 3]
A list can also include strings:
list2 = ['a', 'b', 'c']
Printing our list2:
print(list2)
should present the following result:
>> ['a', 'b', 'c']
If you want to append a list you can use the append method. The method is expecting to get the new element as a parameter. The following call of the append method is adding the element ‘d’ to the list which is stored in list2:
list2.append('d')
Printing our the list again:
print(list2)
 Shows that the new item has been added and that the list is now comprises four items in total:
>> ['a', 'b', 'c', 'd']
Selecting a list element by using the index selector can be done in the following way:
list2[0]
As indexing starts at zero this is returning the first element of the list:
>> 'a'
A re-assignment is also possible by using the index selector:
list2[0] = 'abc'
The print out of the list content
print(list2)
now shows that the value from the first list item has changed from ‘a’ to ‘abc’:
>> ['abc', 'b', 'c', 'd']
It’s also possible to define nested lists. A nested list is a list which is included in another list. One way to create a nested list is to to execute the following line of code:
# Nested Lists
list2[0] = ['a', 'b', 'c']
This command is assigning a list to the first element of list2. To check the result let’s print out list2 again:
print(list2)
The result now shows the nested list in the following way:
>> [['a', 'b', 'c'], 'b', 'c', 'd']

Dictionaries

The next important data structure which is supported by Python is a dictionary. In comparison to a list each element within a dictionary is made up of two parts: key and value. To define a list we’re using the following syntax:
d1 = {'key1': 'value1', 'key2': 'value2'}
This will create the following dictionary with two key-value pairs:
>> {'key1': 'value1', 'key2': 'value2'}
If we now want to access a specific value from the dictionary we can use the corresponding key for selection. The name of the key is passed into the brackets syntax in the following way:
d1['key2']
The result is:
>> 'value2'
The value of a key within a dictionary can be of any type, e.g. we can use a list as a value. The following example shows how to define a dictionary with one key. The value of that key is a list with three numeric elements:
# Lists in Dictionaries
d2 = {'list1':[1,2,3]}
Printing out d2 shows the following result:
>> {'list1': [1, 2, 3]}
Accessing the list1 key directly with the following code:
d2['list1']
will return the list:
>> [1, 2, 3]
We can also retrieve the list from the dictionary and assign this list at the same time to a new variable which is called list1:
list1 = d2['list1']
As seen before we can now use the index selector to access items from the list.
list1[2]
Result:
>> 3
Doing the same in just one step is also possible:
d2['list1'][2]
As seen with lists before, it’s also possible to include a dictionary within a dictionary. In the following example a dictionary is key1 is defined and the value of this key is set to another dictionary:
# Nesting Dictionaries
d3 = {'key1': {'key2': 'value2'}}
Accessing the inside dictionary is straight forward:
d3['key1']
The output you should see is the inside dictionary:
>> {'key2': 'value2'}
Of course you can access a specific element within the inside dictionary. Here you need to use brackets two times. First to access the value of key1 (which is the inside dictionary) and then access the value of key2 from the inside dictionary:
d3['key1']['key2']
The command returns the following value:
>> 'value2'

Sets

Last but not least, let’s take a look at sets, another data structure which is part of Python. Sets are defined by using curly braces:

{1,2,3}

Sets can only contain distinct values. If you try to define a set which includes duplicates, like the following set:

{1,2,2,3,3,3}
You’ll get the following result:
{1, 2, 3}
The result set contains only unique values, all duplicates are removed automatically.
Off course, sets can be stored in variables:
s1 = {1,2}
Adding another element to the set s1 can be done by calling the add method and passing in the new element as a parameter:
s1.add(3)
Now the set looks like the following:
>> {1, 2, 3}

COURSE: Complete Python Masterclass

Check out the great Complete Python Masterclass Online Course by Tim Buchalka and Jean-Paul Roberts with thousands of students already enrolled:

Complete Python Masterclass

  • You’re taught step by step how to program in Python
  • Understand data structures and how to access the web with Python
  • The skills to get a job with Python under your belt as taught by the best
  • With each step, the why you’re doing it is explained

Go To Course


Using and writing about best practices and latest technologies in web design & development is my passion.