Python Basic – How to setup the local environment and run python

Home /

Table of Contents

Local Environment Setup for Python

If you are a Linux or Mac user, you already have Python on your computer. As of the writing of this course (May 2022), Python 2.7.x is the default version of Python built-in with these two major operating systems. However, this course is based on Python 3.9.x. In fact, versions 2 and 3 of Python have a number of intermediate changes in syntax and features. Python’s official website is currently focused on Python 3X, and they have stated unequivocally that Python 3 is the current and future version of Python.

What is the difference between Python 2 and Python 3?

The difference between these two major versions of Python and more details can be read in the official python wiki: Python2 or Python3 – Python Wiki 

Installation of Python

Below we will learn about the steps for installing the latest version of Python 3 on some major operating systems. As mentioned earlier, Linux or Mac has two versions of Python built-in. So to start this Python interpreter directly, you need to open the terminal and type the command

tuhin@linuxmint:~$ python

And pressing Enter will start the Python 2 interpreter. But since we will not work with this version, we will focus on the latest version installation below.

Linux (Debian, Ubuntu, Linuxmint)

The latest version of Ubuntu also has Python 3 installed (eg: Python 3.8.10) but not set by default. To launch this version of the interpreter you may need to type python3 into the terminal and press Enter.

The two pythons have different binary save names and their paths can also be seen. Issuing the command which python and which python3 commands in the terminal will show  /usr/bin/python and  /usr/local/bin/python3 respectively. That means the default Python and Python 3.4 paths are different.

However, if we want to install the latest version, we need to download the Gzipped source tarball of Python 3.10.4 directly from Download Python 3.10.4  this link and extract it. This will create a folder named Python-3.10.4 on the computer.

Now, open the terminal and go to the location of that folder using cd (Change Directory) command. 

E.g.

tuhin@linuxmint:~$ cd ~/Downloads/Python-3.10.4

Then you have to run the following command,

tuhin@linuxmint:~$ ./configure

Now run the following command,

tuhin@linuxmint:~$ make

Then,

tuhin@linuxmint:~$ sudo make install

When all is well, open the terminal and run the command,

tuhin@linuxmint:~$ python3   

The output will be as follows,

tuhin@linuxmint:~$ python3   
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

   

This means that the Python 3.8.10 console or REPL has been launched.

To find out the location of this new python which python3 command you can see which output can show the path  /usr/local/bin/python3

If it is not installed in the above method, then run the following command and follow all the above steps again.

tuhin@linuxmint:~$ sudo apt install build-essential

Mind it Carefully,

Since Linux and Mac have python 2 binary built-in and separately installed Python binary name is usually python3.9 or python3.10; So many people do python by changing the name of the newly installed python binary or by creating a symbolic link so that the python 3 interpreter is started only after executing the python command in the terminal. This should not be done at all. Because Linux or Mac has some tools and programs that depend on the Python of that system. Now when you change the name of Python 3 to just Python, then those system programs may not work properly. Because they know the interpreter of Python 2 as Python.

However, these minor complications can be solved by creating a virtual environment which will be discussed at the end of the course.

Install Python on Windows

Here are the steps to install Python on Windows:

  1. Go to the official Python website at www.python.org/downloads/ and click on the “Download Python” button.
  1. Choose the appropriate version of Python (Python 3 is recommended) and click on the corresponding installer link.
  1. After the installer file downloads, run it to start the Python installation process.
  1. Follow the prompts in the installer to select the installation directory, customize the installation, and install Python.
  1. Once the installation is complete, open the Command Prompt by typing “cmd” in the Windows search bar and pressing Enter.
  1. In the Command Prompt, type “python” and press Enter to start the Python interpreter.

That’s it! You should now have Python installed on your Windows machine and be able to run Python code from the Command Prompt.

Python Interpreter

The Python interpreter is a program that runs Python code. It reads and executes the code line by line, and it is responsible for translating the code written by the programmer into machine-readable instructions that the computer can execute.

There are two types of Python interpreters:

  • The standard interpreter, also known as CPython, is the reference implementation of Python and is written in C. It is the most widely used interpreter and can be downloaded from the official Python website.
  • Alternate interpreters: There are several other interpreters for Python, such as Jython (written in Java), IronPython (written in C#), and PyPy (a faster implementation of Python that uses JIT compilation).

Python code can be run using the interpreter in different ways:

  • Interactive mode: It allows you to type Python code and see the results immediately. It is useful for experimenting with the language and testing small snippets of code.
  • Script mode: In this mode, you write your code in a file with the .py extension and then execute that file using the interpreter.
  • Integrated Development Environment (IDE): An IDE such as PyCharm or IDLE provides a graphical user interface for writing and running Python code.

Python allows a user to perform interactive mode programming that involves running a program by typing directly into the command line. To start interactive programming, call the Python interpreter without passing the script file mentioned below:

tuhin@linuxmint:~$ python3   
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>print("Hello EnableGeek")
  Hello EnableGeek
>>>

Then you will get the output in the next line as below,

>>> print("Hello EnableGeek")
    Hello EnableGeek
>>>

The sign “>>>” means that the Python interpreter is ready to take the Python statement from you, and here you can write.

The Python interpreter takes the source code, which is the code written by the developer and converts it into machine-readable instructions that the computer can execute. It also handles tasks such as memory management and error handling.

Python Syntax and Variables

Mathematical calculations can be easily done on the Python console. So open Python Interpreter again.

The results can be easily found by writing the following mathematical commands on the console. Like addition and subtraction, multiplication and division can be done easily here. Using brackets it is possible to determine which part of the piece will be operated before. Dividing by a single / using the result is float type decimal. And if you divide using double //, the result is a decimal of integer type.

>>> 3+8+10
21
>>> 3*8*10
240
>>> 3*8*(5+5)
240
>>> 3*8/(5+5)
2.4
>>>
>>> x = 50
>>> y = 34
>>> x + y
84
>>>

As in general math, in Python, dividing a number by zero will result in an error,

>>> 240/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>

Float Data Type

Float is used to represent numbers in Python that are not integer types. E.g. 1.0, -5.15 etc. Using a decimal point in any number means that it becomes a float type of data. Or dividing the number of any two integer types in Python gives the result of a float type.

E.g.

>>> 1/3
0.3333333333333333
>>> 3/8
0.375
>>> 10/8
1.25
>>> 3+1.25
4.25
>>> 3*8.90
26.700000000000003
>>> 8/2.50
3.2
>>> a = 10.5
>>> b = 6.3
>>> c = a + b
>>> print(c)
16.8
>>>

Some more numeric operations

Let’s look at the example below:

>>> 8**3
512
>>> 2**3
8
>>> 24/5
4.8
>>> 24//5
4
>>> 38%3
2
>>> 50%13
11
>>>

In addition to, addition, subtraction, and multiplication, Python has support for expansion, which we call the power of one number over another. This operation is done with two ** marks. Only integer division is used to determine the quotient and the modulo operator is used to determine the quotient. Integer division is done using two forward slashes // and the modulo operator is used with the % symbol.

Share The Blog With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article