In this Python for Beginners tutorial we will learn how to get started with Python.
I will assume that you have installed python properly.
Let’s learn the first basic things that you should know when learning Python.
Check if Python is Installed
To check if Python is installed, open the Terminal (or Command-Line).
Type:
$ python3 --version

If not, check out the tutorial on how to install Python.
Check Where Python is Installed
If you want to know where Python has been installed, you use the which python command.
$ which python3
The which
command will show you the location where the python3 executable file has been installed.
Run Python in the Terminal
You can run Python in the Terminal with the Python Interpreter.
Open the Interactive Mode
To open the Python interpreter interactive mode, in the Terminal, type:
$ python3
This will open the interactive mode where you can start to run Python code.

Run Python Code
In the Python interpreter, you can start to write Python code.
For example, you can use the print() built-in Python function.
$ print('hello world')

Close the Python Interpreter
To close the Python interpreter you can:
- Press
CTRL + D
(on MacOS) - Write
exit()
and pressEnter
(on Windows)
Using an Integrated Development Environment
Instead of writing Python code in the Terminal, you may want to use an IDE such as Visual Studio Code or Google Colab.
IDEs allow to speed-up development tasks.
My two favourite IDEs are VSCode and Google Colab.
While VSCode makes it easier to work locally and do version control (git), Google Colab comes with Python pre-installed and offers some free GPUs that can be useful in machine learning projects.
Check out:
Basic Python Operations
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.
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.

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.
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
multiline
string
'''
Output:

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:
print('yes!')
IndentationError
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
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.
function_name(arguments)
Arguments are optional.
Create a function using the def
keyword.
def my_func():
print('hello')
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.

SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. In a quest to programmatic SEO for large organizations through the use of Python, R and machine learning.