LEARN PYTHON PROGRAMMING: BASIC PYTHON INTRODUCTION AND GETTING STARTED
Desiliciouskitchen-Want to learn the python programming language, but still not sure where to start?
Calm… Because in this article, we will discuss the initial preparation thoroughly:
- What is Python and why learn Python?
- What are the tools needed to learn python?
- How to write a python program?
Let’s talk about everything…
What is Python?
Python is a high-level programming language developed by Guido van Rossum. Python is widely used to create various programs, such as: CLI programs, GUI (desktop) programs, Mobile Applications, Web, IoT, Games, Hacking Programs, etc. Python is also known as a programming language that is easy to learn, because its syntax structure is neat and easy to understand. (Python is great for beginners who have never coded)
Why learn Python?
Python is indeed very simple compared to other languages. You don’t need this and that to create a Hello World! program. Even the tagline on the website explains that python will make you work faster and more effectively.
So why learn Python?
- Fast and effective;
- Easy to learn;
- Widely used in large companies;
- Just curious.
- …(add your own)
Preparation Tools for Learning Python Programming
What are the tools that must be prepared to learn python programming?
- Python: An interpreter that translates python into machine language, so that programs can be run.
- Text Editor/IDE: Program used to write code.
How to install Python?
For Linux users, Python does not need to be installed. Because most Linux distros already provide it by default. For Windows users Install Python on Windows.
Python Version 2 vs Python Version 3
There are two versions of Python currently available, namely versions 2 and 3.
What’s the difference?
Python version 2 is the version that is widely used today, both in production and development environments. While Python version 3 is a continuation of version 2. Python 3 has more features than Python 2. To open Python 2 we only use the python command, while Python 3 uses the python3 command.
Which one should I choose?
For those who are just learning I recommend using version 2. While for those who are advanced, you can try version 3.
Set up a Text Editor/IDE for Writing Code
The text editor used to write python programs can be anything. Even Notepad can. On Linux, there are many choices of text editors that can be used. In addition to a text editor, we can also use an IDE (Integrated Development Environment). However, we will discuss later. For now, let’s just use a text editor, so we can understand more about programming concepts.
Get to know Python’s Interactive Mode
Interactive mode is a facility/feature provided by Python as a place to write code interactively. This feature is also known as Shell, Console, REPL (Read–Eval–Print Loop), interpreter, etc. How to open interactive mode is to type python command in terminal.
The >>> sign means that python is ready to accept commands.
There is also a … sign which means secondary prompt or sub prompt, usually appears when creating a block of code and writing a single command over several lines.
Let’s try to give the print command, this command works to print text to the screen. Try writing print “Hello World” then press Enter. The commands we write are executed immediately and the results are displayed. This is interactive mode, every code or command typed will be responded directly by python.
We can take advantage of this interactive mode to:
- Test a function;
- Specific module experiments;
- Seek help with certain functions;
The thing we need to try is to seek help on a specific function, as it will be very helpful in learning python.
There are two functions used to seek help:
- dir() function to see what functions are available in a module;
- the help() function to open the documentation for a function.
After that, we can see what functions are available in the module. Then, we can find out how to use these functions with help(). Suppose we want to find out how to use the function pow(), then we have to give the command help(math.pow).
=> To exit documentation press q
After that, we can use it and try its function.
Python Programming Workflow
- Create python scripts with a text editor.
- The python script is translated into binary code by the python (interpreter), so the computer can understand the meaning of the command.
- The computer performs the command.
Understand…? Up to this point, we already know how to program Python. Next, we’ll learn how to create a Python program using the PyCharm IDE.
Python Programming Workflow
PyCharm is the best IDE for python programming. PyCharm is made by JetBrains.
There are two versions of PyCharm:
- Professional Version (30 days Trial) – Has more features for python and web programming.
- Community Version (Free and opensource) – Features standard for python programming
In this guide, we will be using the Community version of PyCharm.
Installing PyCharm on Linux
First, make sure your computer has the JDK (Java Development Kit) or JRE installed. Because PyCharm is made of Java and it needs JRE to run. Please read How to Install JDK and JRE on Linux.
After that… Please download PyCharm on the JetBrains website. Make sure to download the community version.
After that, follow these steps to install it:
- Open File manager as root.
- Find the PyCharm file that was downloaded earlier, then extract it to the /opt directory.
- Reopen File Manager as normal user. Then go to /opt/pycharm-community-2017.1.2/bin and double click on the pycharm.sh file, then select run.
- Wait a moment, it will open a new window. Select Don not import settings, then click OK.
- A welcome window will open, please tick “Enable opening file…” so that the charm command is recognized in the terminal, then click Ok.
Enter the Password when prompted, then click OK. Finished
Creating a Python Project in PyCharm
To create a new project, we can click “Create new Project” in the PyCharm welcome window. Or it can also be done via the File->New Project menu. After that, we will be asked to fill in the name of the project and choose the Python version to use. Just fill the project name with hello-world and use Python version 2 (python2.7). Then we’ll go straight down to the PyCharm workspace. Next, please add the python file by right-clicking on the project directory, then selecting New->Python File. After that, give the file name with program-first and click Ok.
Next, please write the program code. Then open the Run->Run… menu or press Alt+Shift+F10. If the option appears, just choose the first program.
Our first program in PyCharm has been executed successfully. That’s how to make a python program using the PyCharm IDE, easier isn’t it? We do not need to open a terminal to execute the program.