Lesson 1: Tools & Your First Python Program

Welcome to this course about Python for PHP developers. There are many Python 101 courses online, but I specifically wanted to create this course for "my audience" of PHP/Laravel professionals.

The idea is that you already know how to code in one language (PHP), and this course will be an overview of what's different in Python compared to PHP:

  • Use-cases
  • Syntax
  • Structure of projects
  • etc.

This course aims to be a reference, preparing you for real practical courses/tutorials on Python and Machine Learning so that you can understand their syntax details.

So, let's start your training!


1. Python Installation

To start writing Python programs, we need to install Python binaries first.

But, compared to PHP, you do not need to set up a web server or MySQL database setup to run your Python programs.

Here are the instructions on how to do that on the leading platforms.

Windows

Download and install the latest stable Python release from the official website - https://www.python.org/downloads/windows/

Python for Windows

MacOS

Download and install the latest stable Python release from the official website - https://www.python.org/downloads/macos/

Python for MacOS

Linux

Most Linux distributions have Python installed by default.

You can check if Python is present on your system by using python --version or python3 --version command:

1$ python --version
2Python 3.11.6
3
4$ python3 --version
5Python 3.11.6

In case you don't have it, you can install Python using these commands:

Fedora / RedHat

1sudo dnf upgrade
2sudo dnf install python

Ubuntu / Debian

1sudo apt update
2sudo apt install python3

At the time of writing this, the latest Python version is 3.12. But even if you have an earlier version, it shouldn't be a problem for this course.


2. PyCharm Installation

For this course, we chose PyCharm Community Edition IDE as our main editor because it provides all essential features out of the box without any additional configuration.

You can download it from the Official JetBrains Website.

PyCharm Website Screenshot

Download an executable for your OS and follow the instructions.

Versions of PyCharm may differ

Windows

  1. Run the pycharm-community-2023.2.5.exe file that starts the Installation Wizard
  2. Follow all steps suggested by the wizard. Please pay special attention to the corresponding installation options

MacOS

  1. Download the pycharm-community-2023.2.5-aarch64.dmg macOS Disk Image file
  2. Mount it as another disk in your system
  3. Copy PyCharm to your Applications folder

Linux

  1. Unpack the pycharm-community-2023.2.5.tar.gz file to an empty directory using the following commands:
1mkdir -p ~/pycharm
2 
3tar -zxf pycharm-community-2023.2.5.tar.gz -C ~/pycharm

Note: A new instance MUST NOT be extracted over an existing one. The target folder must be empty.

  1. Run PyCharm.sh from the bin/ subdirectory
1cd ~/pycharm/pycharm-community-2023.2.5/bin
2 
3./pycharm.sh

Alternative ways to install PyCharm

Fedora using flatpak

Alternatively, you can install PyCharm using flatpak command:

1flatpak install flathub com.jetbrains.PyCharm-Community

Run with this command:

1flatpak run com.jetbrains.PyCharm-Community

3. Run your first program

Open your PyCharm IDE and create a new project:

PyCharm new Project

After pressing the Create button, you will be greeted with the following window:

PyCharm First Window

In file view, you will see this code:

main.py

1# This is a sample Python script.
2 
3# Press Shift+F10 to execute it or replace it with your code.
4# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
5 
6 
7def print_hi(name):
8 # Use a breakpoint in the code line below to debug your script.
9 print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
10 
11 
12# Press the green button in the gutter to run the script.
13if __name__ == '__main__':
14 print_hi('PyCharm')
15 
16# See PyCharm help at https://www.jetbrains.com/help/pycharm/

Press SHIFT+F10 (hotkey may vary on your OS) to run your program.

PyCharm Run

At the bottom of your IDE you should see the output window with the Hi PyCharm! message.


Kwesi Odame Danquah avatar

First step conquered. Great tutorial

😍 3
Geovane Krüger avatar

VScode its not good for use with Python?

dodofix avatar

yes, it is good choice, there are many tutorials ... for example https://code.visualstudio.com/docs/python/python-tutorial

i like vscode due to low memory usage, speed and many plugins ... and you can use one IDE for PHP, Python, web, ...

Povilas avatar

It is a good tool as well, a personal preference. I guess, similar comparison in PHP would be PHPStorm vs VSCode.

Chris McGee avatar

Well, I'm off to a great start; I can't even get past the third step.

Since I'm on Windows and want to use WSL, I have to use PyCharm Professional, so I just bought that. Now, when attempting to create the project using the same settings as shown in this article, there is no .venv directory, let alone a main.py file inside it.

I don't know what I'm doing wrong.

Povilas avatar

To be honest, I haven't used Windows WSL so can't really help, but have you tried looking at the official docs of JetBrains, like this one?

I will also forward to the team, so if teammates have additional advice, I will follow-up.

Chris McGee avatar

I seem to have gotten it to work, so long as I use the "Remote Development" ability. I was hesitant to use that because it didn't work very well in PhpStorm, being slow and sometimes unresponsive. Maybe they've since fixed those issues.

Lackson David avatar

We have started this safari together. Be blessed :D

Muhammad Uzair avatar

Great tutorial, I have started to learn phyton, Let's be master in phyton. I am happy to share that thank you Povilas.

jitin-joseph avatar

Tried it in Windows 11. great start.