Unit – 1
Introduction to Python
1.1 Introduction to Python
1.1 INTRODUCTION TO PYTHON
● Python is a general-purpose, interpreted, object-oriented programming language.
● Python was created by Guido van Rossum in the Netherlands in 1990 and was named
afterthe popular British comedy troupe Monty Python’s Flying Circus.
● Van Rossum developed Python as a hobby, and Python has become a popular
programming language widely used in industry and academia due to its simple, concise,
and intuitive syntax and extensive library .
● Python is now used in the Google search engine, in mission-critical projects at NASA,
and in transaction processing at the New York Stock Exchange.
2
Features of python
Python is a versatile and powerful programming language known for its simplicity and ease of use. It is widely used in
various domains, including web development, data analysis, artificial intelligence, automation, and more.
Here are some key features of Python:
Simple and Readable Syntax: Python's syntax is designed to be clear and readable, making it easy for beginners to
learn and understand.
Interpreted Language: Python is an interpreted language, which means you can run code directly without the need for
explicit compilation.
Dynamic Typing: Python is dynamically typed, allowing you to assign variables without declaring their data types
explicitly.
High-level Language: Python abstracts complex low-level operations, making it easier to focus on problem-solving
rather than managing memory and other system-level details.
Large Standard Library: Python comes with a vast standard library that provides modules for various tasks, such as
string manipulation, file I/O, networking, and more.
Extensible: You can easily extend Python by creating custom modules and integrating them into your projects.
3
Features of python
Object-Oriented Programming (OOP): Python supports object-oriented programming, allowing you to create and use classes and
objects for better code organization and reusability.
Cross-platform: Python code can run on various operating systems, such as Windows, macOS, Linux, etc., without modification.
Exception Handling: Python has robust support for handling exceptions, which helps in writing more robust and error-resistant code.
Dynamically Allocated Memory: Python uses automatic memory management with garbage collection, making memory allocation
and deallocation more convenient for developers.
Libraries and Frameworks: Python has a rich ecosystem of libraries and frameworks that facilitate various tasks, such as Django and
Flask for web development, NumPy and Pandas for data analysis, TensorFlow and PyTorch for machine learning, and many more.
Indentation-based Code Blocks: Python uses indentation to define code blocks, promoting clean and consistent code formatting.
Integration Capabilities: Python can easily integrate with other languages like C, C++, and Java, enabling developers to leverage
existing codebases.
Community and Support: Python has a large and active community, providing extensive documentation, tutorials, and support for
developers.
4
Python with Interpriter
Python is an interpreted programming language, meaning that it executes code directly without the need for explicit compilation.
When you run a Python script, the interpreter reads the code line by line and executes it on the fly. Here's how the Python
Interpreter works:
Source Code: You write your Python code in a plain text file with a .py extension. This file contains the source code you want to
execute.
The Interpreter:
The Python interpreter is a program that reads your Python source code and converts it into intermediate code known as bytecode.
Bytecode: Bytecode is a low-level representation of your Python code, which is platform-independent. It allows the Python
interpreter to execute the code more efficiently.
Execution: The Python interpreter then executes the bytecode line by line, performing the actions and computations specified in
the code.
Output: If your code includes any print statements or functions that generate output, the interpreter will display the results in the
output console.
Since Python is an interpreted language, you can execute Python code directly without the need to compile it beforehand. This
makes Python development and debugging much more straightforward and accessible, especially for beginners.
5
Applications of python
Python's versatility and extensive libraries have led to its use in a wide range of applications across various domains.
Some of the key applications of Python include:
Web Development: Python is widely used for web development, thanks to frameworks like Django and Flask. Django, a
full-stack web framework, enables rapid and secure development of complex web applications. Flask, a micro-
framework, is popular for building small to medium-sized web applications and APIs.
Data Analysis and Visualization: Python has become a preferred choice for data analysis and visualization tasks.
Libraries like NumPy, Pandas, and SciPy provide powerful tools for data manipulation, exploration, and statistical
analysis. Matplotlib and Seaborn are widely used for creating insightful visualizations.
Artificial Intelligence and Machine Learning: Python's simplicity and robust libraries make it a favorite for artificial
intelligence and machine learning projects. Libraries like TensorFlow, PyTorch, and scikit-learn provide powerful tools for
building and training machine learning models.
Scientific Computing: Python is used in scientific computing and numerical simulations due to its ability to handle
complex mathematical computations. Libraries like NumPy, SciPy, and SymPy are popular choices for these tasks.
Scripting and Automation: Python's ease of use and cross-platform compatibility make it an ideal choice for scripting
and automation tasks. It is commonly used for automating repetitive tasks, managing files, and interacting with system
processes.
6
Applications of python
Game Development: Python is used in game development, especially for building prototypes and simple games. The
Pygame library provides a set of tools for creating 2D games.
Internet of Things (IoT): Python's simplicity and support for various hardware platforms make it a popular choice for
IoT projects. Libraries like Adafruit CircuitPython simplify interactions with hardware devices.
Desktop Applications: Python can be used to create desktop applications with graphical user interfaces (GUI) using
libraries like PyQt and Tkinter.
Natural Language Processing (NLP): Python is widely used in NLP applications, such as text processing, sentiment
analysis, language translation, and chatbots, with libraries like NLTK and spaCy.
Big Data and Cloud Computing: Python is used in big data processing and cloud computing environments. Libraries
like PySpark enable data processing in distributed computing frameworks like Apache Spark.
Finance and Quantitative Analysis: Python is used in finance for tasks like algorithmic trading, risk analysis, and
financial modeling due to its powerful libraries and ease of implementation.
Image and Video Processing: Python's libraries like OpenCV are used for tasks like image and video manipulation,
object detection, and computer vision.
7
Installation steps for Python
Visit the Python website: Go to the official Python website at https://www.python.org/.
Choose the Python version: On the Python website, you will find different versions available for download. There
may be a "Downloads" or "Downloads & Releases" section on the homepage. Choose the version that is suitable for
your operating system. As of my knowledge cutoff in June 2023, Python 3.x is the recommended version for most
users.
Select the installer: After selecting the Python version, you will see a list of available installers. Choose the installer
file that corresponds to your operating system. There are typically separate installers for Windows, macOS, and
Linux.
Download the installer: Click on the download link for the installer file, and it will start downloading to your
computer. The download file size may vary depending on the version and your operating system.
Run the installer: Once the download is complete, locate the installer file on your computer and double-click on it to
run the installer.
Start the installation: The installer will launch and guide you through the installation process. Make sure to check
the box that says "Add Python to PATH" during the installation. This will allow you to use Python from the command
line or terminal.
8
Installation steps for Python
Customize the installation (optional): During the installation, you may be prompted to select additional
components or customize the installation. You can choose the default settings unless you have specific
requirements.
Complete the installation: Follow the instructions provided by the installer to complete the installation process.
Once the installation is finished, you should see a message indicating a successful installation.
Verify the installation: To verify that Python is installed correctly, open a command prompt (Windows) or a
terminal (macOS/Linux) and type the following command:
Congratulations! You have successfully installed Python on your computer. Now you can start writing and
running Python code. You can use a text editor, an integrated development environment (IDE), or even an online
code editor to write Python programs.
9
Getting Started with Python
You can start Python in a command window by typing python at the command prompt IDLE (Interactive
Development Environment) is an integrated development environment (IDE) for Python.
You can create, open, save, edit, and run Python programs in IDLE. Both the command-line Python interpreter and
IDLE are available after Python is installed on your machine.
To exit Python, press CTRL+Z and then the Enter key.
10
Installing Jupyter Notebook
Install the classic Jupyter Notebook with:
pip install notebook
To run the notebook:
jupyter notebook
11
Programming style in python
Programming style in Python refers to the conventions and guidelines followed by developers to write clean,
readable, and maintainable code.
Adopting a consistent programming style not only makes your code easier to understand but also helps in
collaborating with other developers and maintaining codebases in the long run.
The official style guide for Python is defined in PEP 8 (Python Enhancement Proposal 8).
12
Programming style in python
Here are some key aspects of programming style in Python:
Indentation and Whitespace: Python uses indentation to define code blocks instead of braces like in many other
programming languages. It is recommended to use four spaces for each level of indentation. Consistent and clear
indentation enhances code readability.
Line Length: PEP 8 suggests limiting lines to a maximum of 79 characters, which helps in easy readability and avoids
horizontal scrolling in text editors.
Naming Conventions: Use descriptive and meaningful names for variables, functions, and classes. Follow the
conventions:
Variables and functions: Use lowercase letters, separated by underscores (snake_case).
Classes: Use CamelCase (each word capitalized without underscores).
Constants: Use uppercase letters, separated by underscores (ALL_CAPS).
PEP : Python Enhancement Proposal. PEP 8 is the Style Guide for Python Code
13
Programming style in python
Whitespace in Expressions and Statements:
Use whitespace to enhance readability:
Put one space around binary operators (e.g., x = a + b, not x=a+b).
Put no space between a function name and the opening parenthesis of its arguments (e.g., print("Hello"), not print
("Hello")).
Put two blank lines between top-level functions and classes.
Imports: Import modules or packages on separate lines. Avoid using wildcard imports (from module import *) to
prevent namespace pollution.
Comments: Use comments to explain the purpose and logic of your code. Follow the recommended style for
comments:
Use complete sentences for docstrings (used for function and class documentation).
Use # for single-line comments and place them above the code they refer to. 14
Programming style in python
Function and Class Definitions: Use two blank lines to separate function and class definitions.
Avoid Global Variables: Minimize the use of global variables; prefer passing data explicitly to functions or using class
attributes.
Exception Handling: Only catch exceptions that you expect and handle. Avoid using a bare except: clause, as it can
catch unexpected exceptions and hide errors.
String Formatting: Prefer using f-strings (formatted strings) for string interpolation. For example: f"The result is
{result}".
15
Hello World..!!
Open Jyputer Notebook
Create python file as shown in figure with extension ipynb.
16
Hello World..!!
Insert new cell and write
print(“Hello World”)
To run : press ctrl + ent
17
Thanks