Skip to content

Chapter 2 Getting Started

CHAPTER 2

Getting Started

Keynote

1.1 Python Specification

  • Python is a specification for a language that can be implemented in different ways. There are many implementations of this specification written in different languages.

  • Different popular Python implementations are:

  • CPython - is the reference implementation, written in C.
  • PyPy - Written in a subset of Python language called RPython.
  • Jython - Written in Java.
  • IronPython - Written in C#.

  • All the implementations are compilers as well as interpreters. The compiler converts the Python program into intermediate bytecode. This bytecode is then interpreted by the interpreter.


1.2 Python Installation under Windows

  • Python has evolved over the years. At the time of writing of this edition the latest version for Windows and Linux environments was Python 3.14.1.

  • Python is not shipped as part of Windows OS. So we need to install it separately. For this we need to download the Python installer from www.python.org/downloads/.

  • While downloading ensure that you choose the appropriate installer from the following, based on whether you wish to install it on a 32-bit machine or a 64-bit machine:

  • 64-bit machine: Download Windows x86-64 executable installer
  • 32-bit machine: Download Windows x86 executable installer

  • Once you have chosen and downloaded an installer, execute it by double-clicking on the downloaded file. A dialog shown in Figure 2.1 will appear on the screen. Fig 2.1 Python 3.14.1 Installer Screen

  • In this dialog check the check box 'Add Python 3.14 to PATH' or 'Add python.exe to PATH' to ensure that the interpreter will be placed in your execution path.

  • Click on 'Install Now' and the installation will happen in a few minutes. Python files will get installed in the directory: C:\Users\Joseph\AppData\Local\Programs\Python\Python314

  • In this path β€˜Joseph’ represents the user name and β€˜Python314’ represents the version number of Python installation that you have downloaded and installed.

  • If you forget to check the check box, you can add the path mentioned above to PATH variable through Control Panel > System > Environment Variables > Edit. The PATH variable already contains many semicolon separated values. Append the above path to existing values.

  • Once the installation is complete, you can verify it by opening a command prompt and typing python --version.

  • If the installation was successful, you will see the version of Python installed on your system.

  • If you are using Windows 10, you can also verify the installation by opening Settings > Apps > Apps & features > Python.

  • If you are using Windows 11, you can also verify the installation by opening Settings > Apps > Apps & features > Python.


1.3 Python Installation under Linux

  • Most Linux distributions (like Ubuntu) already contain Python in them. However, the installed Python version may not be the latest one. You can check the version as shown below:

    Checking Python version
    python3 --version
    
  • If you find that the version is not the latest one, then you can install the latest specific version (e.g., Python 3.13) using the command:

    Installing Python 3.13
    sudo apt update
    sudo apt install python3.13
    

1.4 Python Resources


1.5 Third-party Packages

  • Pythonistas in Python community create packages (libraries) and makes it available for use for other programmers. They use PyPIβ€”Python Package Index (www.pypi.org) to distribute their packages.

  • PyPI maintains the list of such third-party Python packages available.

  • There are third-party packages available for literally doing everything under the sun. Some packages that are popularly used for creating Data Science applications include:

  • NumPy: Advanced mathematical operations library with support for large multi-dimensional arrays and matrices.
  • SciPy: Scientific computing library for optimization, integration, interpolation, signal processing, image processing, etc.
  • Pandas: Library for manipulating numerical tables and time series.
  • MatPlotLib: 2D and 3D Data visualization library.
  • OpenCV: Open source Computer vision library.

1.6 Python Packages and Development Tools

  • You too can register at PyPI and upload your packages there. You should follow the guidelines given at www.pypi.org to create the package, build it and upload it to the Python Package Index.

  • pip is a commonly used tool for installing packages from PyPI. This tool gets installed when you install Python.

1.6.1 Data Science and AI Tools

  • Many tools have come into existence to help Python programmers build and document their Data Science and Artificial Intelligence applications. These include:
  • Jupyter Notebook - It is a very flexible browser-based tool that lets us to interactively work with Python (and many other languages). It lets us put our Python code, output of the code and any kind of visualization or plot etc. in the same document called Notebook. It is a great tool doing modular program development.
  • Google Colab - This tool provides a free Jupyter notebook environment to execute code on Google's cloud servers. As a result, you can leverage the power of Google's hardware.
  • Spyder - This tool provides a Scientific Python Development Environment with sophisticated testing and debugging features.

  • Both Jupyter and Spyder are part of a very popular software distribution called Anaconda. So once you download and install Anaconda, you get Jupyter and Spyder ready-made.

1.6.2 IDLE (Integrated Development and Learning Environment)

  • Once Python is installed, program development can be done using the built-in Python Integrated Development and Learning Environment (IDLE).

  • IDLE is a good development tool. It offers handy features like syntax highlighting, context-sensitive help and debugging.

  • Syntax highlighting feature display keywords, functions, methods and strings in different colors making it easy to identify them.
  • Context-sensitive help can be obtained by pressing Ctrl Space wherever you need help as you type the program. This is immensely useful since it is almost impossible to remember names of all functions and methods and their parameters.
  • Debugger lets you locate any logical errors that you may have committed in your program by allowing you trace the flow of execution of the program. This tracing can be done a step at a time by setting up break points and by single stepping through the program. As you do so IDLE lets you watch the values of different variables as they change during execution.

1.6.3 Python Modes

  • Python can be used in two modes:
  • Interactive mode - used for exploring Python syntax, seek help and debug short programs.
  • Script mode - used for writing full-fledged Python programs.

  • Both modes are supported by IDLE.

1.6.3.1 To use IDLE in Interactive mode:
  • Locate it in Windows by typing IDLE in Windows search bar and hit enter, or double click the IDLE icon.
  • It will open the Python shell window showing >>> Python shell prompt.
  • Execute the following Python code at this prompt.

    Interactive mode
    >>> print('Keep calm and bubble on')
    
  • It will display the message 'Keep calm and bubble on' followed by the >>> prompt.

1.6.3.2 To use IDLE in Script mode:
  • Launch IDLE. In the IDLE shell window from the menu select File | New File. A new window will open. Type the following script in it:

    Script mode
    print('Those who can\'t laugh at themselves…')
    print('leave the job to others.')
    
  • Using File > Save and save the script under the name 'Test.py'.

  • Execute the script from the Run menu or using F5. The two messages will get printed.
1.6.3.3 Using Other IDEs
  • Instead of IDLE if you decide to use NetBeans or Visual Studio Code for program development then follow the steps given below:
  • Create a new Python project β€˜Test’.
  • Type the script in Test.py.
  • Execute the script using F6 in NetBeans or Ctrl F5 in Visual Studio Code.
  • On execution it will print the two lines and then you are ready to create another project and another script in it.

1.7 Checking Python Version

  • Python has evolved over the years. You can determine the version installed on your machine through a simple Python script:

    Checking Python version
    import sys
    print(sys.version)
    

Exercises

[A] Answer the following quesitons:

  • a. What do the prompt C:>,$ and >>> signify?
  • b. In which two modes can IDLE be used?
  • c. What is the purpose of the two programming modes offered by IDLE?
  • d. How can third party libraries be used in a Python Program?

[B] Match the following pairs:

Section A Section B
a. pip 1. Advanced mathematical operations
b. Jupyter 2. Scientific Computing
c. Sypder 3. Manipulation numerical tables
d. PyPI 4. Visualization
e. Numpy 5. Computer Vision
f. Scipy 6. Package installation tool
g. Pandas 7. Build and document applications
h. MatPlotlib 8. Scientific Library
i. OpenCV 9. Python Package index

[C] State whether the following statements are True or False:

  • a. Python is a specification that can be implemented through languages like Python,C#,Java,etc.
  • b. CPython is implementation of Python specification, written in C.
  • c. Python program is first compiled intor byte code, which is then interpreted.
  • d. Most Linux distributions already contain Python.
  • e. Windows system doesn't contain Python and it needs to be separately installed.
  • f. PYthon prgorams can be built using IDLE, Netbeans, PyCharm and Visual Studio Code.
  • g. Thrid-party Python packages are distributed using PyPI.

Comments