Python - Variables, Lists, and Dictionaries

In this module we will learn about the basic data types in Python and how we can work with them using variables and a variety of built-in operators. We will also see how we can store entire sets of data within a variable using two important structures: Lists and Dictionaries.

Introduction

If you’ve never written code, or done any computer programming, the whole concept might seem daunting at first. However, while advanced software development is without a doubt incredibly complex, programming in general is based on only a few key concepts. By understanding these concepts from the beginning, you will be less intimidated by all of the particular syntax that you don't yet know. As long as you can express what you want the program to do in general terms, you can always search the internet for examples on using the proper syntax. In fact this is how most people learn to code today. Furthermore, while each programming language has it’s own syntax, they almost all follow the same basic principles, so learning these principles will be useful no matter what language you end up using.

Comments and print()

The first bit of Python syntax we will cover is the all-important 'comment'. You specify a comment by starting a line with '#', which tells Python to ignore everything on that line after the '#' symbol. Try typing the following lines of code into your editor and executing the script:

# this is a comment
print('this is code') # this is also a comment

If you run this code you will see that it prints out 'this is code' because it executes the line print('this is code'). This uses the print() Function which takes in any text (or piece of data) you put inside the parenthesis and prints it to our code editor's console text box. print() is one of many Functions built-in to Python. We will see other useful functions in this and the following module, and will see how we can create our own Functions in a later module.

Meanwhile, it ignores both comments occurring after the '#' symbol. Although every language specifies them differently, comments are an important part of every programming language, as they allow the developer to add extra information and description to their code which is not strictly related to its execution.

Most Python editors also have a shortcut for commenting whole lines of code. Pressing CTRL + / should 'comment out' the current line of code by placing a '#' in front of it. You can also use the shortcut with multiple lines selected to comment out entire sections of code. This is useful during troubleshooting if you want to disable certain parts of your script without deleting the actual code.

Now that we know the basics, let's start to explore the fundamental principles of coding in Python, starting with variables.