DEV Community

Cover image for Python : Crash Course
Eric The Coder
Eric The Coder

Posted on

Python : Crash Course

What will you learn in this course?

This course will teach you basic concepts in Python. All with easy-to-understand terms and examples. Before learning to run, you must know how to walk. Knowing the basics is essential to master a language like Python.

The crash course is free and will be posted here on dev.to. I will publish a new article every two days or so. To not miss anything, you can follow me on twitter: Follow @EricTheCoder_

Why a crash course?

This type of course allows you to learn a maximum of concepts in a minimum of time.
An accelerated course allows the student to maintain a high level of interest and motivation.
The time saved allows the concepts learned to be put into practice more quickly.
At the end of the accelerated course, the student knows enough about the subject to learn on their own the concepts that have not been covered.

Installing Python

To start coding with Python you need to make sure you have an up-to-date version of Python (version 3.10 or +)

From the terminal, type this command to find out the version of Python installed on your machine.

$ python --version

or

$ python3 --version
Enter fullscreen mode Exit fullscreen mode

If you don't have Python or a version earlier than 3.10, you can get the latest version of Python from the official site: https://www.python.org/downloads/

If you want to start coding immediately and install Python later you can use a web-based (free) version of Python.

Here is the link:

https://replit.com/languages/python3

Code Editor

To follow this tutorial you will need to use a code editor. There are several choices, if you don't have a favorite editor yet, I recommend Microsoft's vscode.

Here is the link:

https://code.visualstudio.com/download

In this tutorial we will not teach how vscode works. If you want to learn the basics of vscode, there are several free tools available.

One option I recommend is to configure vscode to allow running from the terminal. Here is a link on the instructions for doing this:

Max OSX

https://code.visualstudio.com/docs/setup/mac

Windows

https://code.visualstudio.com/docs/setup/windows

Run python code

Once the installation of Python is complete on your machine, it is now time to run your first Python code.

In the next sections we will learn together the basics of Python languages. I will therefore invite you to create a folder and a file in order to place the code which will be given as an example.

Create a folder named “demo_python”

~$ mkdir demo_python
~$ cd demo_python
Enter fullscreen mode Exit fullscreen mode

Inside the folder create a file named “main.py”

~$ code main.py
Enter fullscreen mode Exit fullscreen mode

Your first line of code

Add this code in the “main.py” file

print("Hello World")
Enter fullscreen mode Exit fullscreen mode

Here the “print()” instruction is used to display text in the terminal

Once the file is saved, from the terminal you can run it using Python.

~$ python3 main.py

or

~$ python main.py
Enter fullscreen mode Exit fullscreen mode

Python will execute all the lines of code present in the file and display the result in the terminal.

Hello World
Enter fullscreen mode Exit fullscreen mode

Run Python Code Quickly

If you want to run Python code quickly and without creating a file, you can use the shell. The shell is an application that allows you to enter a line of Python code, execute it and display the result immediately.

To run the shell just run Python without adding any argument

~$ python3

or 

~$ python
Enter fullscreen mode Exit fullscreen mode

The command will launch the shell from the terminal

Python 3.9.9 (main, Nov 21 2021, 03:16:13) 
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> _
Enter fullscreen mode Exit fullscreen mode

Once the application is launched, it is possible to enter a command line after the following characters >>>

Example:

>>> print("Hello World")
Enter fullscreen mode Exit fullscreen mode

Once the command has been entered, press the “return” key and the code will be executed

>>> print("Hello World")
Hello World
>>> _
Enter fullscreen mode Exit fullscreen mode

You will have understood that the shell is more useful for testing a Python command than for writing a real application.

If you are developing a real python application the approach to use is to put the code in files with the extension “.py”

Your first steps with Python

For the next examples, I invite you to use the “main.py” file that we created in the previous example.

The variables

Variables allow you to store information that can be used later by your application.

To define a variable, all you have to do is give it a name and a value.

For example, suppose you want to store the username, you can use a variable and give it the name: name and set its contents equal to “Mike Taylor”

name = "Mike Taylor"
print(name)
Enter fullscreen mode Exit fullscreen mode

Here the variable is called “name” and contains the text “Mike Taylor”. The quotes tell Python that its content is text.

Types of variables

A variable can contain several types of data. In the last example it is text but we could have assigned an integer to it.

num = 25
Enter fullscreen mode Exit fullscreen mode

Note that to be recognized as a number, you must not use quotes ““

With Python, it is not necessary to specify the type of the variable. Python will detect the type automatically depending on the content you assign to it.

Depending on the type detected by Python, it will be possible to perform operations on this variable.

Here is an example :

value = "100"

value2 = 100
Enter fullscreen mode Exit fullscreen mode

Here “value” is a text type variable (string) which contains the text “100”. This variable is not a number that can be used in a mathematical operation.

“value2” is an integer type variable that has the number 100 as its content. This variable is a number and therefore can be used for mathematical operations.

result = value + value
# "100100"

result2 = value2 + value2
# 200
Enter fullscreen mode Exit fullscreen mode

The addition of these two variables does not give the same result. In Python Adding a text variable (string) will pair the two texts together. While the addition of integer variables will mathematically add the two values.

Here are different types of variables that can be defined with Python. We will come back to each of them in detail later.

name = 'Mike' # string (texte)
age = 42 # integer (nombre entier)
price = 199.99 # float (nombre décimal)
is_active = True # boolean (vrai ou faux)

colors = ['red', 'green', 'blue'] # liste
states = ('inactive', 'active', 'archive') # tuple
products = { 'name': 'iPad Pro', 'price': 199.99 } # dictionnaire
Enter fullscreen mode Exit fullscreen mode

In order to simplify the tutorial, from this point, the types of variables will be called by their English name. For example I will not use the term “text” or “integer” but rather “string” and “integer”.

Reporting convention

By convention, Python variables are defined in lowercase letters and may contain the _ character as a separator.

first_name = "Mike"
Enter fullscreen mode Exit fullscreen mode

Comments

It is possible to insert a comment in your code. To do this, you must use the “#” character. Anything after this character will not be executed by Python

# Ceci est un commentaire
name = "Mike Taylor"

age = 27  # Ceci est un commentaire
Enter fullscreen mode Exit fullscreen mode

It is also possible to create multi-line comments with the use of three quotes “”” comments “””

def say_hello():
    """ Cette fonction permet d'afficher un
    messsage de bienvenu sur plusieurs lignes """ 

   print("Hello World")
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's all for today, I'll post a new article every other day or so. To make sure you don't miss anything, you can follow me on twitter: Follow @ EricTheCoder_

Discussion (0)