A few weeks ago, we told you 10 reasons why you should learn to program in Python. But if you are still not convinced, in this article, we will talk about how to start programming with Python and the best way to do it.
Before starting:
1. Programming in Python is fun.
Python code is easier to write and understand than other comparable programming languages. This is due to its easy syntax. Even if you've never programmed before, you'll be able to understand what the code does just by reading it normally.
2. The syntax is not too strict
In Python, it is not necessary to specify the data type of a variable. In addition, the semicolon is not necessary after each statement (something that more than one will appreciate 😉)
Python forces you to follow best practices (like proper indentation). This may seem like torture at first, but it can make programming much easier for beginners.
3. Expressiveness of language
Python allows you to write programs with extensive functionality in just a few lines of code. It's amazing what you can do with Python once you master the fundamentals.
4. A great community and support
Python is supported by a large community. There are several active online forums that can help you if you get stuck.
Here are some examples:
Once we have everything clearer, the next question is:
How to install the program?
At this point I must refer you to my previous article, where we talked about operating systems to start working with Python.
Once you have chosen yours, we will explain the installation process for each of them.
We want to show you how to install and use Python on your operating system in the easiest way possible. This way, you can quickly get started in programming.
Python Installation Guide on MacOSX
- Open the page python download in your browser and select Download Python 3.10.7 (you may see a different version, always select the latest one).
- When the download is complete, you must open the .dmg file and follow the installation instructions.
- A good editor can be a great help for Python development. We recommend Sublime Text (download Sublime Text 3) either Visual Studio Code. Both editors can be used for free. To simplify, we are going to select the Sublime Text editor in this guide.
- The installation works like any other program.
- Open Sublime Text and select File > New File (shortcut: Cmd+N). Then save (Cmd+S or File > Save) the file with the .py extension (for example, Helloworld.py).
- Write your code and save it. You can use this code to test:
print(“Hello, world!”)
For this simple program, type “Hello World” into the command line. Select Tools > Build (Cmd + B). You’ll see “Hello World” in the Sublime Text console. Congratulations, you’ve just run your first Python program!
Installation instructions for Windows
- Open the page python download in your browser and select Download Python 3.10.7 (you may see a different version, always select the latest one).
- When the download is complete, double-click the file to open it.
- Along with Python, a small IDE called IDLE will be installed.
- Open IDLE and write the following code.
- print(“Hello, world!”)
- To create a new file in IDLE, select File > New Window (shortcut: Ctrl+N).
- Write your Python code (you can use the code below for now) and save it (shortcut: Ctrl+S) with the file extension .py (e.g. hello.py)
print(“Hello, world!”)
- Choose Run > Run Module (Shortcut: F5) to see the output of your program. Congratulations, you just ran your first Python program.
At this point, it's time to move on to the next step.
Your first Python program
The classic way to start programming is always the “Hello World” example. A “Hello World” program simply produces the text “Hello World,” as we saw in the previous section.
Therefore, let's choose another example:
- # Sum of two numbers
- number1 = 2
- number2 = 4
- sum = number1+number2
- print(sum)
How does this Python program work?
Line 1: # Sum of two numbers
All lines starting with # in Python programming are comments.
Comments are used in programming to explain the purpose of the code. This helps other programmers understand the intent of the code. Comments are completely ignored by compilers and interpreters.
Line 2: number1 = 2
Here, number1 is a variable. You can store a value in a variable. Therefore, the number 2 is stored in this variable.
Line 3: number2 = 4
Likewise, the number 4 is stored in the variable number2.
Line 4: sum = number1+ number2
The variables number1 and number2 are added together using the "+" operator. The result of the addition is stored in another variable, sum.
Line 5: print(sum)
The print() function outputs to the screen. In our case, the number 6 is displayed on your monitor.
It is important to remember something:
To represent a statement in Python, a new line is used. The use of semicolons (cf. C/C++, PHP) is optional and it is recommended to omit this one. Braces are used to represent a block.
Yes, I know… it's something very simple. How do I keep moving forward?