Easy Introduction to Python (with Examples)

In this tutorial, we will have a first easy introduction to Python and the basic concepts that you will need to know when getting started.

Importing Modules

One of the powerful things Python, or whatever programming languages, can do is to be
able to leverage the code that others have written, into your own code.

In Python, the way to import this code is to use the import keyword along with the name of the library to be imported.

Subscribe to my Newsletter

import library_name

In this case, I will import this.

import this

The this module is simply the Zen of Python, a code that does nothing apart printing rules that Python programmers should have in mind when writing code.

Installing Libraries

To install a library in Python, use the pip install command. Depending on the version of pip that is installed, you may be required to use pip or pip3.

$ pip install advertools
$ pip3 install advertools

Writing Comments in Python

Comments in Python begin with the hashtag character (#). This way, Python does not execute anything that follows a hashtag on a line. 

# this is a single line comment
print('hello') # This is a comment

Hashtags allow you to annotate your code so that humans can understand, but that your computer will skip.

Creating Strings in Python

Strings are different than comments as strings are a data type that is returned to the console when called, whereas a comment is skipped.

Single line Strings

"this is a single line string"

Mutliline strings

This is a 


Here multiline comment are showing the newline symbol (\n) showing that if you print the string, it will add new lines to replace the escape character.

Python Indentation

One thing that is very important with Python is the indentation.

Without the proper indentation in your code, the code will break.

You need to add indentation whenever you create a new block of code (using the if, the def or the for keywords for example).

Example Indentation

if 2 > 1:


Here is an example if you don’t indentation in your code.

Spaces vs Tabs

While Python.org documentation prefers spaces over tabs because tab is actually a character (\t) which equals 4 spaces.

However, nobody actually types 4 spaces, so considering the human laziness factor, I always recommend to use tabs instead of individual spaces as it makes it easier to be consistent and prevents things like IndentationError.

Python Variables

In Python, variables are containers that store data values.

Assign a value to a variable

You can assign a variable using the equal (=) symbol where the left is the name of the variable and the right of the sign is the value assigned to the variable.

x = 10

Assign Different Data Types

You can assign different data types to a variable:

x = 10 # int 
y = 'string' # str
z = [1,2,3] # list

Python Casing

Python has some standard that you can follow to make your code easy to understand.

  • Snake case (snake_case) is the preferred format for variables, functions and modules.
  • Pascal case (PascalCase) is the preferred format for classes
  • Capitalized snake case (CAP_SN_CASE) is the preferred format for constants.

If.. Elif.. Else Blocks

If you want to make conditional statements in Python, use the if, elif, else keywords.

If statements are used to tell the computer to do something if a condition is met (if), something else if another condition is met (elif) and something else (else) if not.

if condition:
    # do something
elif condition2:
    # do something else
    # Do something else

For Loops

A for loop is used to iterate over a sequence.

The sequence should be either a list, a string, a dictionary, a tuple, or a set.

It is implemented using this structure, starting with the for keyword.

for element in sequence:
    # do something

For example, we can loop through a list of elements and print it.

Python Functions

In Python, a function is a portion of reusable code.

A function is called using its name with parentheses and arguments inside.


Arguments are optional.

Create a function using the def keyword.

def my_func():

To call the function, use its name.

Multiline Statements

If your code takes to much space on a single line, you can break it out using the backslash (\) symbol.

Instead of:

Use multiline statements.

Dealing with Errors

Errors are part of the daily life of a Python programmer.

For example, if you try to divide by 0.

10 / 0

Python with throw a ZeroDivisionError.

It is important that you learn how to read Python errors in order to debug your code.

Whenever you face an error in Python, Python will provide a traceback to your error.

You should read the traceback from bottom to top.

The name of the error is first printed (ZeroDivisionError). Sometimes, this is enough for you to understand what is going on.

If not, go to the next line. The first line of the traceback block with be the path of the file that had an error. Below, you will know the line location (shown with the arrow —–>) within the file that had the error.

Going up this way, you can investigate any Python exception.

After Learning the Python Basics

Now that we have learned the basics of Python, let’s dive a bit deeper into each of the concepts that we have just seen.

Enjoyed This Post?