Introduction to Python
Dr. Faisal Anwer
Department of Computer Science
Aligarh Muslim University, Aligarh-202002
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap of Previous Lecture
• Introduction to Data Science
• AI, Machine Learning and Data Science
Introduction
➢ Python is Developed by Guido van Rossum.
➢ The Python language is built around 19 guidelines from the
Zen of Python.
➢ The first release appeared in 1991 as Python 0.9.0.
Introduction: Features
Python is an interactive, general-purpose, object-oriented, and high-
level programming language.
Open-Source
Simplicity & Readability
Portable
Huge community of developers/users
Python works on different platforms
Python is widely used in various types of applications
Introduction: Features
Vast Support of Libraries and frameworks
• Data Science: NumPy, Pandas, SciPy, PyTorch, etc.
• Machine Learning: Tensorflow, Scikit-Learn, PyBrain,
PyML, etc.
• Artificial Intelligence: Keras, OpenCV, NLTK, etc.
• Web Development: Django, Flask, Pyramid, etc.
• Network Programming: Asyncio, Pulsar, Pyzmq, etc.
INTRODUCTION: OFFICIAL HOME
✓ The official home of the Python Programming Language
is www.python.org
✓ The latest release can be checked
(https://www.python.org/downloads/)
✓ Python extension is .py
IDEs & Editors
✓Integrated Development Environment (IDE)
✓Text/Code Editor
✓How to select IDE/Editor?
➢ Beginner — IDLE (or Online Python Editors) is perfect.
➢ Intermediate/ Advanced —Google Colab, Jupyter
Notebook, PyCharm, Sublime, etc.
✓What’s your purpose?
➢Data Science —Jupyter Notebook, Colab, PyCharm
Professional
➢Web development — PyCharm Professional, VS Code
IDEs & Editors
Integrated Development and Learning
Environment (IDLE)
✓IDLE is a cross-platform
IDEs & Editors:
open-source IDE
that comes by default
IDLEwith Python.
✓Suitable for simple projects
Installation Steps for IDLE
✓ Go to https://www.python.org/downloads/
✓ Download the latest release of Python as
per your OS ( Windows/Linux/Mac)
✓ Run the downloaded (.exe) file
IDEs & Editors: Jupyter Notebook
✓A web-based interactive development environment
✓Easy to use, open-source software that allows you to create
and share live code, visualizations, and others.
First Method:
Installing Jupyter using Anaconda
✓Download latest version of Anaconda
(https://www.anaconda.com/products/individual)
✓Install Anaconda
(https://docs.anaconda.com/anaconda/install/windows/)
IDEs & Editors: Jupyter Notebook
Second Method:
Installing Jupyter with pip (Python’s package manager)
✓Rum command prompt (cmd)
✓Upgrade pip (pip3 install --upgrade pip)
✓pip3 install jupyter
Start with Jupyter Notebook
✓Rum command prompt (cmd)
✓Write jupyter notebook
IDEs & Editors: Colab
✓A free Jupyter notebook environment that runs entirely in the
cloud.
✓Requires no installation/setup
✓Allows you to work in a team
✓Supports many popular machine learning libraries
IDEs & Editors: Getting Started
✓ Run IDLE or Jupyter Notebook or Colab
✓ print(‘Hello, World’)
✓ a=50
✓ b=6
✓ c=a%b
Running code in the interactive shell
• Python is an interpreted language.
• Python expressions and statements can be run in an
interactive programming environment called the shell.
At the prompt (>>>) type:
print (“Python: Hello World”)
– Press [Enter]
13
Running code in the interactive shell
Calculator: Interactive Mode
• Start the Python interpreter and use it as a calculator.
• Use an interactive shell to perform the following
operations
➢2 + 3,
➢6/2,
➢6/4,
➢6%4,
➢6%2,
➢2*4-3,
➢4-2*6-4
14
Programming in Script Mode
• Open a New File from the File menu of the IDLE shell
window
• Write your program and save your file using the “.py”
extension
• Use Run Module (F5) from the Run menu.
15
Program
1.Write a program in Python programming and save it as
MyfirstProgram.py to perform the following operations
Addition, Subtraction, Multiplication, Division, Modulo
division.
2.Write a program in Python programming and save it as
MysecondProgram.py to display the following messages:
• “Hello World, I am studying Data Science using Python”
• “ Guido Van Rossum invented the Python programming
language.”
16
Program Documentation
• Comment lines provide documentation about your
program
– Anything after the “#” symbol is a comment
– non-executable (Ignored by the python shell)
#My First Python Program
#May February, 2024
17
Basic Concepts
✓ A case-sensitive language
✓ Comment (single line (#) or Multi-lines (triple-quotes))
✓ Indentation is essential (no curly braces)
✓ Multiple statements are allowed with a semicolon
✓ a=5; b=6; c=a+b
✓ Keywords: A set of reserved words that can’t be used as variable
names, function names, or any other identifier:
✓ import keyword as K
✓ len(K.kwlist) --- 36
✓ To print the list, print(keyword.kwlist)
Introduction to Python Advanced Libraries
NumPy
• NumPy stands for Numerical Python.
• It is a library consisting of
multidimensional array objects and a
collection of routines for processing
those arrays.
• Using NumPy, mathematical and
logical operations on arrays can be
performed.
Pandas
• Pandas is a Python library used for
working with data sets.
• It has functions for analyzing,
cleaning, exploring, and
manipulating data.
• Pandas can clean messy data sets,
and make them readable and
relevant.
Seaborn
• Seaborn is a python library for making statistical graphics.
• It is built on top of matplotlib and integrates closely with
pandas data structures.
import pandas as pd
import seaborn as sns
df = pf.read_csv('PIMA_diabetes.csv')
k=df.corr()
sns.heatmap(k, annot=True, cmap="bwr")
Scikit-learn (Sklearn)
• Scikit-learn (Sklearn) is the most useful and robust library
for machine learning in Python.
• It provides a selection of efficient tools for machine
learning and statistical modeling including classification,
regression, clustering and dimensionality reduction via a
consistence interface in Python.
Scikit-learn Features
• Scikit-learn library focuses on modeling the data. Some of the
most popular groups of models provided by Sklearn are as
follows −
• Supervised Learning algorithms
• Unsupervised Learning algorithms
• Clustering − This model is used for grouping unlabeled data.
Example
from sklearn.linear_model import LinearRegression
from sklearn.metrics import
mean_absolute_error,mean_squared_error
lin_reg = LinearRegression()
lin_reg.fit(X_train, Y_train)
Y_pred=lin_reg.predict(X_test)
Summary
• Features of Python
• IDE and Editors
• How to run a python code
• Introduction of Advanced Libraries