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/
MacOS
Download and install the latest stable Python release from the official website - https://www.python.org/downloads/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 --version2Python 3.11.634$ python3 --version5Python 3.11.6
In case you don't have it, you can install Python using these commands:
Fedora / RedHat
1sudo dnf upgrade2sudo dnf install python
Ubuntu / Debian
1sudo apt update2sudo 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.
Download an executable for your OS and follow the instructions.
Versions of PyCharm may differ
Windows
- Run the
pycharm-community-2023.2.5.exe
file that starts the Installation Wizard - Follow all steps suggested by the wizard. Please pay special attention to the corresponding installation options
MacOS
- Download the
pycharm-community-2023.2.5-aarch64.dmg
macOS Disk Image file - Mount it as another disk in your system
- Copy PyCharm to your Applications folder
Linux
- Unpack the
pycharm-community-2023.2.5.tar.gz
file to an empty directory using the following commands:
1mkdir -p ~/pycharm2 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.
- Run
PyCharm.sh
from thebin/
subdirectory
1cd ~/pycharm/pycharm-community-2023.2.5/bin2 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:
After pressing the Create button, you will be greeted with the following 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.
At the bottom of your IDE you should see the output window with the Hi PyCharm!
message.
-
- 1. Tools & Your First Python Program
- 2. Python vs PHP: Main Syntax Differences
- 3. Basic Data Types: string / int / float / bool
- 4. Complex Data Types: list / tuple / set / dictionary
- 5. For/While Loops and Comprehensions
- 6. Defining Your Own Functions
- 7. Importing Libraries
- 8. CSV Files: Reading and Writing
First step conquered. Great tutorial
VScode its not good for use with Python?
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, ...
It is a good tool as well, a personal preference. I guess, similar comparison in PHP would be PHPStorm vs VSCode.
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 amain.py
file inside it.I don't know what I'm doing wrong.
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.
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.
We have started this safari together. Be blessed :D
Great tutorial, I have started to learn phyton, Let's be master in phyton. I am happy to share that thank you Povilas.
Tried it in Windows 11. great start.