Unit-1 Introduction
Introduction:
Python is a high-level, interpreted programming language known for its simplicity and readability. It was
created by Guido van Rossum and first released in 1991. Python supports multiple programming paradigms,
including procedural, object-oriented, and functional programming. Its design philosophy emphasizes code
readability and the use of significant whitespace.
Python is one of the top programming languages in the world, widely used in fields such as AI, machine
learning, data science, and web development. The simple and English-like syntax of Python makes it a go-to
language for beginners who want to get into coding quickly. Because Python is used in multiple fields, there
is a high demand for Python developers, with competitive base salaries.
Python is a high-level, general-purpose, and very popular programming language. Python programming
language (latest Python 3) is being used in web development and Machine Learning applications, along with
all cutting-edge technology in Software Industry. Python language is being used by almost all tech-giant
companies like – Google, Amazon, Facebook, Instagram, Dropbox, Uber… etc.
What can Python do?
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used for rapid prototyping, or for production-ready software development.
• Python can be used to handle big data and perform complex mathematics.
• Python can be used on a server to create web applications.
Why Python?
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
• Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
• Python has a simple syntax similar to the English language.
• Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This
means that prototyping can be very quick.
• Python can be treated in a procedural way, an object-oriented way or a functional way.
History of Python:
• Late 1980s: Python's development began as a hobby project by Guido van Rossum at the Centrum
Wiskunde & Informatica (CWI) in the Netherlands. Van Rossum was inspired by the ABC
programming language, which was designed for teaching and prototyping but lacked extensibility.
• December 1989: Van Rossum began working on Python as a Christmas project. He aimed to create
a language that was simple, easy to read, and powerful enough to handle various tasks. He wanted to
address some of the shortcomings of the ABC language, particularly its lack of access to the
operating system and its inability to extend the language with custom tools.
• February 1991: The first version of Python, Python 0.9.0, was released. It included features such as
classes, exception handling, functions, and the core data types like strings, lists, and dictionaries.
Python also borrowed some concepts from other programming languages like C, Modula-3, and
Unix shell scripting.
• 1991-1994: Python continued to develop, with additional features and bug fixes being added. Python
1.0 was officially released in January 1994. This version included significant features like lambda,
map, filter, and reduce, which are essential for functional programming.
• Mid-1990s: During this period, Python began to gain popularity among developers, particularly in
academic and scientific communities. Python's simplicity and readability made it an attractive choice
for researchers and educators.
• Python 2.0 (October 2000): Python 2.0 marked a major milestone in the language's development. It
introduced new features such as list comprehensions, garbage collection, and the full Unicode
support. Python 2.x series became widely used and remained the standard for many years.
• Python 3.0 (December 2008): Python 3.0 was a significant release that aimed to rectify some of the
design flaws and inconsistencies in Python 2.x. However, it was not backward-compatible with
Python 2.x, meaning that code written in Python 2.x would not run on Python 3.0 without
modification.
Key features of Python 3.0 included:
o A clear distinction between text (Unicode) and binary data.
o A new syntax for print functions (print() instead of print statement).
o Changes in integer division (/ always returns a float).
o Improved handling of exceptions.
o Better support for asynchronous programming.
Python Today:
• Widespread Adoption: Python is now one of the most popular programming languages in the
world, used in a wide range of applications, including web development, data science, artificial
intelligence, machine learning, scientific computing, and automation.
• Active Community: Python has a large and active community that contributes to its development,
maintains a vast ecosystem of libraries and frameworks, and supports newcomers through
documentation, tutorials, and forums.
• Ongoing Development: Python continues to evolve, with regular updates and new features being
added. Python 3.x is now the standard, with the latest versions introducing enhancements in
performance, syntax, and functionality.
Features of Python:
• Simple and Easy to Learn: Python's syntax is clear and intuitive, making it an excellent language
for beginners.
• Interpreted Language: Python code is executed line by line, which makes debugging easier.
• Cross-platform: Python runs on various platforms such as Windows, macOS, and Linux.
• Open Source: Python is freely available to use and distribute.
• Extensive Standard Library: Python comes with a vast standard library that includes modules and
packages for various tasks like file handling, regular expressions, and web development.
• Object-Oriented: Python supports object-oriented programming with classes and objects.
• Dynamic Typing: Python automatically infers the type of a variable at runtime.
• Support for Multiple Paradigms: Python supports procedural, object-oriented, and functional
programming.
• Vast Community Support: Python has a large, active community that contributes to its continuous
development and provides extensive documentation.
• GUI Programming Support
• Large Standard Library
• Frontend and backend development
Working of Python Programming:
A Python program starts as a text file written in a code editor, saved with a .py extension. The Python
interpreter reads this source code, analyzes it, and executes it line by line. During this process, it performs
lexical analysis, parsing, and execution. Optionally, the source code can be compiled into bytecode for faster
execution. The Python interpreter acts as a virtual machine, executing the code by converting it into
machine-specific binary code. The program may utilize library modules for various tasks. When run, it can
interact with the system, process data, and produce output. Any errors or exceptions are handled during
execution.
Python is a high-level programming language known for its simplicity and readability. To understand how a
Python program works, let's break it down into several key steps with example Hello World:
Code Editor:
You start by writing your Python code in a code editor. This is where you create your program and save it
with a .py extension. A code editor provides features for writing and editing code, such as syntax
highlighting and code completion.
Open a text editor, and write the following code:
Source File:
Your Python code is saved in a source file with a .py extension, e.g., [Link]. This file contains the text of
your program written in Python's syntax.
Compiler (Optional):
Python is typically an interpreted language, meaning it doesn't require explicit compilation before running.
However, in some cases, your Python code may be compiled into bytecode. This compiled bytecode is saved
in .pyc files. The compilation is not something you usually do manually; it's managed by the Python
interpreter.
Bytecode:
Bytecode is an intermediate representation of your Python code. It's a low-level, platform-independent
format that the Python interpreter can execute. Bytecode files (.pyc) are generated to improve execution
speed. If the .pyc file exists and is newer than the source code, Python uses it for faster execution. In the
case of "Hello, World!" where the code is simple, Python often skips the bytecode step.
Python Interpreter (Virtual Machine):
The Python interpreter is responsible for executing your Python [Link] execute the Python program in
your terminal or command prompt.
python [Link]
The Python interpreter reads [Link] and performs several steps during the execution:
Lexical Analysis: The interpreter reads the source code, breaks it down into tokens, and identifies
keywords, identifiers, operators, and literals.
Parsing: It constructs a parse tree to understand the code's structure and logic.
Execution: It executes the program by following the instructions defined in the parse tree. This includes
performing calculations, I/O operations, and function calls.
Library Modules:
Python provides a rich standard library, which includes a wide range of modules and functions for various
tasks. You can import these library modules into your program to extend its functionality. For example, the
math module provides mathematical functions, and the os module allows you to interact with the operating
system. While the "Hello, World!"example doesn't use any additional modules, you can import and use them
in more complex programs to extend functionality.
Binary Code:
The Python interpreter converts the bytecode into machine code that is specific to your computer's
architecture. This is done dynamically during the execution of your Python program.
Running Program:
As the Python interpreter executes the bytecode, your Python program runs, and it can interact with the
system, read and write files, and produce output. If there are errors or exceptions, Python will handle them
according to your code or raise an error if not [Link] this example, the program will run and display the
output:
Hello, World!
Installing python and environment set up
Installing Python:
• Download the latest version of Python from the official website ([Link]).
• Follow the installation instructions based on your operating system (Windows, macOS, or Linux).
• Ensure to check the option to add Python to the system PATH during installation.
Environment Setup:
• Interactive Shell and Console: Python provides an interactive shell, where you can execute Python
commands one at a time and see the output immediately. This is useful for testing small snippets of
code.
• IDLE and IDEs: Python comes with IDLE, an Integrated Development Environment, which
provides a simple editor for writing Python scripts. For more advanced development, IDEs like
PyCharm, Visual Studio Code, or Jupyter Notebook are recommended.
• Installing Third-Party Libraries: Python uses the package manager pip to install third-party
libraries. Libraries can be installed by running pip install <library-name> in the terminal or command
prompt.
• Virtual Environment: Virtual environments allow you to create isolated Python environments for
different projects, ensuring that dependencies are managed separately. You can create a virtual
environment using python -m venv <env-name> and activate it with source <env-name>/bin/activate
on macOS/Linux or <env-name>\Scripts\activate on Windows.
Python Installation on Windows
The installation requires downloading the official Python .exe installer and running it on your system. The
sections below will explain several options and details during the installation process.
Step 1: Select Python Version
Deciding on a version depends on what you want to do in Python. The two major versions are Python 2 and
Python 3. Choosing one over the other might be better depending on your project details. If there are no
constraints, choose whichever one you prefer.
We recommend Python 3, as Python 2 reached its end of life in 2020. Download Python 2 only if you work
with legacy scripts and older projects. Also, choose a stable release over the newest since the newest release
may have bugs and issues.
Step 2: Download Python Executable Installer
Start by downloading the Python executable installer for Windows:
1. Open a web browser and navigate to the Downloads for Windows section of the official Python website.
2. Locate the desired Python version.
3. Click the link to download the file. Choose either the Windows 32-bit or 64-bit installer.
The download is approximately 25MB.
Step 3: Run Executable Installer
The steps below guide you through the installation process:
1. Run the downloaded Python Installer.
2. The installation window shows two checkboxes:
• Admin privileges. The parameter controls whether to install Python for the current or all system
users. This option allows you to change the installation folder for Python.
• Add Python to PATH. The second option places the executable in the PATH variable after
installation. You can also add Python to the PATH environment variable manually later.
• For the most straightforward installation, we recommend ticking both checkboxes.
3. Select the Install Now option for the recommended installation (in that case, skip the next two steps).
• To adjust the default installation options, choose Customize installation instead and proceed to the
following step.
• The default installation installs Python
to C:\Users\[user]\AppData\Local\Programs\Python\Python[version] for the current user. It
includes IDLE (the default Python editor), the PIP package manager, and additional documentation.
The installer also creates necessary shortcuts and file associations.
• Customizing the installation allows changing these installation options and parameters.
4. Choose the optional installation features. Python works without these features, but adding them improves
the program's usability.
Click Next to proceed to the Advanced Options screen.
5. The second part of customizing the installation includes advanced options.
Choose whether to install Python for all users. The option changes the install location to C:\Program
Files\Python[version]. If selecting the location manually, a common choice is C:\Python[version] because it
avoids spaces in the path, and all users can access it. Due to administrative rights, both paths may cause
issues during package installation.
Other advanced options include creating shortcuts, file associations, and adding Python to PATH.
After picking the appropriate options, click Install to start the installation.
6. Select whether to disable the path length limit. Choosing this option will allow Python to bypass the 260-
character MAX_PATH limit.
The option will not affect any other system settings, and disabling it resolves potential name-length issues.
We recommend selecting the option and closing the setup.
Step 4: Add Python to Path (Optional)
If the Python installer does not include the Add Python to PATH checkbox or you have not selected that
option, continue in this step. Otherwise, skip to the next step.
Adding the Python path to the PATH variable alleviates the need to use the full path to access the Python
program in the command line. It instructs Windows to review all the folders added to the PATH
environment variable and to look for the [Link] program in those folders.
To add Python to PATH, do the following:
1. In the Start menu, search for Environment Variables and press Enter.
2. Click Environment Variables to open the overview screen.
3. Double-click Path on the list to edit it.
Alternatively, select the variable and click the Edit button.
4. Double-click the first empty field and paste the Python installation folder path.
Alternatively, click the New button instead and paste the path.
5. Click OK to save the changes. If the command prompt is open, restart it for the following step.
Step 5: Verify Python Was Installed on Windows
The first way to verify that Python was installed successfully is through the command line. Open the
command prompt and run the following command:
python --version
The output shows the installed Python version.
The second way is to use the GUI to verify the Python installation. Follow the steps below to run the Python
interpreter or IDLE:
1. Navigate to the directory where Python was installed on the system.
2. Double-click [Link] (the Python interpreter) or IDLE.
3. The interpreter opens the command prompt and shows the following window:
Running IDLE opens Python's built-in IDE:
In both cases, the installed Python version shows on the screen, and the editor is ready for use.
Step 6: Verify PIP Was Installed
To verify whether PIP was installed, enter the following command in the command prompt:
pip --version
If it was installed successfully, you should see the PIP version number, the executable path, and the Python
version:
PIP has not been installed yet if you get the following output:
'pip' is not recognized as an internal or external command,
Operable program or batch file.
If an older version of Python is installed or the PIP installation option is disabled during installation, PIP
will not be available. To install PIP, see our article How to Install PIP on Windows.
Step 7: Install virtualenv (Optional)
Python software packages install system-wide by default. Consequently, whenever a single project-specific
package is changed, it changes for all your Python projects.
The virtualenv package enables making isolated local virtual environments for Python projects. Virtual
environments help avoid package conflicts and enable choosing specific package versions per project.
To install virtualenv, run the following command in the command prompt:
pip install virtualenv