0% found this document useful (0 votes)
4 views

Python Programming

The document outlines a comprehensive Python programming course, covering topics such as installation, data structures, object-oriented programming, and libraries like Numpy, Pandas, and Matplotlib. It includes practical examples, assignments, and explanations of key concepts like variables, data types, functions, and control flow statements. The course is structured over several weeks, with each week focusing on specific topics and skills essential for Python programming.

Uploaded by

binir61150
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Programming

The document outlines a comprehensive Python programming course, covering topics such as installation, data structures, object-oriented programming, and libraries like Numpy, Pandas, and Matplotlib. It includes practical examples, assignments, and explanations of key concepts like variables, data types, functions, and control flow statements. The course is structured over several weeks, with each week focusing on specific topics and skills essential for Python programming.

Uploaded by

binir61150
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 151

Content

Topic Week

Installing Python & Python IDEs 1

Python Data Structures 1

Flow Control Statements 2

Object Oriented Programming 2

Inheritance 3

Numpy – For Numerical Computing 3

Pandas – For Data Manipulation 4

Matplotlib – For Data Visualization 4


How To Install Python ?

• https://www.python.org/downloads/

Click On This Link & Download Latest Version of Python


How To Install Python IDEs ?

• What is IDEs ?
Integrated Development Environment

• Pycharm : Used to provide tools for python programming

https://www.jetbrains.com/pycharm/

• Anaconda : Provide some extra libraries like Numpy, Matplotlib

https://www.anaconda.com/
My First Python Program
How do I Store Data ?

Data/Values can be stored in temporary storage spaces called variables.

“MANISH”

“NITIN”

“RAHUL”
Variable in python

Students

“NITIN”

0x1098ab
Data types in python

• Every variable is associated with data type.

“NITIN” 5+8j
7,25,49 9.3, 14.657 True , False
“MANISH” 9+25j

INTEGER FLOAT BOOLEAN STRING COMPLEX


Operator in python

+,-,/,* < , > , == , != &,|

Arithmetic Operator Relational Operator Logical Operator


Logical Operator Truth Table

Truth Table For AND Truth Table For OR


A B A AND B A B A OR B
TRUE TRUE TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE FALSE TRUE
FALSE TRUE FALSE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE FALSE FALSE
Comment in python
Python Tokens

Token : Smallest Meaningful Component in a program

• Keywords
• Identifiers
• Literals
• Operators
Python Keywords

These are special reserved words.

Logical Keywords Control Flow Key Words Definition Key Words Module Keywords

TRUE if def import


FALSE else global from
not elif nonlocal as
and for class
or while lambda
is break with
None continue assert
in pass del
try
except
finally
raise
return
yield
Python Identifier

• These are names used for Variables, Functions or Objects.

RULES:
1.No. special character expect _ (Underscore)
2.Identifier are case Sensitive
3.First letter cannot be digit
Python Literals

• In Python, literals refer to the data that is specified in a variable


or constant. So, literal basically corresponds to a short and easy
way to specify a value.
• Literals in almost all cases correspond to data types that are
immutable

STRING LITERAL

NUMERIC LITERAL
TYPES OF LITERALS
BOOLEAN LITERAL

LIST LITERAL
Python String

• String are sequence of characters enclosed within single quotes


(‘’) , double quotes(“”) or triple Quotes (‘’’ ‘’’)

• ‘Hello World’
• “Hello Python”
• ‘”Say Hello To Programming’”
Extracting individual character
String Function in Python

Finding Length of Converting string to Converting string to


string lower case upper case
String Function in Python

Replacing a substring Number of occurrences of


substring
String Function in Python

Finding the index of substring How to split a string


Data Structure in Python

• Tuple
• List
• Dictionary
• Set
Tuple in python

• Tuple is an ordered collection of element enclosed within()

Tuple are immutable


Extracting Individual Element from an Tuple
Modifying a Tuple

You cannot modify a tuple because it is immutable.


Tuple Basic Operation

Finding a length of tuple Concatenating of Tuple

Finding Minimum Finding Maximum


No. from a Tuple No. from a Tuple
Tuple Basic Operation

Repeating & Concatenating


Repeating Element of Tuple Element of Tuple
List in Python

• List is an ordered collection of element enclosed within []

• List are mutable


Extracting Individual Element from a list
Modifying a list

Changing the Popping the last Appending a new


element at 0TH index element element
Modifying a list

Reversing elements Inserting element Sorting a list


of a list at a specified index
List Basic Operation

Concatenating Lists Repeating Element


Dictionary in python

• Dictionary is an unordered collection of key-value enclosed within{ }

Tuple are mutable


Extracting keys & Values

Extracting Keys Extracting Values


Modifying a Dictionary

Adding a new Changing an


element existing element
Dictionary Function

Update one dictionary’s Popping an element


element with another
Set in python

• Set is an unordered & unindexed collection of element enclosed within{ }

• Duplicate are not allowed in Set


Set Operations

Update one dictionary’s Updating Multiple Removing an


element with another Element Element
Set Functions

Union of two sets Intersection of two sets


If Statement

If else
Marks>33%
If… else Pseudo Code

If(condition){
Statements to be executed…
}

else{
Statements to be executed…
}
If… elif…else
If… elif…else Example
Looping Statement

Looping statement are used to


repeat a task multiple times
While Loop

Syntax:

While Condition:
Execute statements
While loop Example
While loop Example
For Loop

For Loop is used to iterate over a sequence

Syntax:

For val in sequence:


Body of for
While loop Example
Assignment

Q1. Print First 10 natural numbers using while loop ?


Q2. Accept number from user and calculate the sum of all number between 1 and
given number ?
Q3. Print multiplication table of given number ?
Q4. Given a list iterate it and display numbers which are divisible by 5 and if you
find number greater than 150 stop the loop iteration ?
Q5. Given a number count the total number of digits in a number ?
Q6. Reverse the following list using for loop ?
Q7. Display -10 to -1 using for loop ?
Function

Function is a block of code which performs a specific task


Function
Lambda Function
Assignment

Q1. Create a function that can accept two arguments name and age and print its
value ?
Q2. Write a function func1() such that it can accept a variable length of
argument and print all arguments value ?
Q3. Create a function show Employee() in such a way that it should accept
employee name, and it’s salary and display both, and if the salary is missing in
function call it should show it as 9000 ?
Q4. Create an inner function to calculate the addition in the following way ?
Q5. Write a recursive function to calculate the sum of numbers from 0 to 10 ?
Q6. Generate a Python list of all the even numbers between 4 to 30 ?
Python Object Oriented Programming
Class

Class is a
template/blue print
for real world
entities(Object) Class is a user
defined Data
Type

Properties Behavior
Object

Object
are
specific
instances
of a class
Creating a first class

Creating the ‘Python’ Class

Instantiating the ‘p1’ Object

Invoking method through


object
Adding parameter to the class

Instantiating the ‘p2’ Object

Invoking method through


object

Adding Parameter to ‘Python’


Class
Creating a class with constructor

Init method acts as the constructor


Instantiating Object

Instantiating the ‘e1’ object

Invoking the
‘employee_details’
method
Inheritance in python

With inheritance one class can derive the


properties of another class

Man inheriting features from


his parents
Inheritance Example

Creating the base class

Instantiating the object for base class


Inheritance Example

Creating the child class

Instantiating the object for child class

Invoking the child class method


Over-riding init method

Over- riding init method

Invoking show_details() method from


parent class

Invoking show_car_details()
method from child class
Types of Inheritance

Single Inheritance

Multiple Inheritance

Multi-level Inheritance

Hybrid Inheritance
Multiple Inheritance

In multiple inheritance, the child inherits


from more than 1 parent class

Parent 1 Parent 2

Child
Multiple Inheritance in python

Parent-1 Class

Child Class

Parent-2 Class
Multiple Inheritance in python

Instantiating object of child class


Invoking Method
Multi-level Inheritance

In multi-level inheritance, we have parent,


child & Grand child relationship
Parent

Child

Grand Child
Multi-level Inheritance in python

Parent Class

Grand Child Class

Child Class
Multi-level Inheritance in python

Invoking the Grand child class


method
Hybrid Class
Delete an Object Pass, To avoid Error
Python Iterators

• An iterator is an object that contains a countable number of


values.

• An iterator is an object that can traverse through all the values.

• An iterator is an object which implements the iterator protocol, which


consist of the methods __iter__() and __next__().

• Lists, tuples, dictionaries, and sets are all iterable objects.


Python Iterators
Python Iterators

Strings are also


iterable objects,
containing a
sequence of
characters
Creating a iter()
Stop The Iteration

The example above would


continue forever if you
had enough next()
statements, or if it was
used in a for loop.

To prevent the iteration to


go on forever, we can use
the StopIteration
statement.
Local Scope

A variable
created inside a
function is
available inside
that function

The local variable


can be accessed
from a function
within the
function
Global Scope

A variable created
outside of a
function is global
and can be used by
anyone
Naming Variables

If you operate with the same


variable name inside and
outside of a function,
Python will treat them as
two separate variables, one
available in the global scope
(outside the function) and
one available in the local
scope (inside the function)
Global Keyword

If you need to create a


global variable, but
are stuck in the local
scope, you can use the
global keyword .The
global keyword makes
the variable global.
Python Modules

A module is a file containing python definitions and statements, all the files
in module have .py suffix

A file containing sets of function we want import or include in our python


file.
Variables in Module

The module can contain functions, as already described, but also


variables of all types (arrays, dictionaries, objects etc)
Re-naming a Module
Built-in Modules

Import and use the


platform module dir() Function Python Dates
Python Math

The min() and The abs() function The pow(x, y)


max() functions returns the function returns
can be used to absolute (positive) the value of x to
find the lowest value of the
or highest value the power of y
specified number
in an iterable (xy)
The Math Module

The math.ceil() method rounds a number upwards to its


nearest integer, and the math.floor() method rounds a
number downwards to its nearest integer, and returns
the result

When you have imported the math module, you can start
using methods and constants of the module.

The math.sqrt() method for example, returns the square


root of a number
Libraries in Python

Python library is a collection of functions and


methods that allow you to perform many actions
without writing your code

For Numerical For Data For Data


Computing Manipulation Visualization
Python Numpy

Numpy stands for Numerical python and is the core


library for numeric and scientific computing

It consist of Multidimensional
array objects and a collection of
routines for processing those
array
Creating Numpy array

Single Dimensional array Multi- Dimensional array


Initializing Numpy array

Initializing Numpy array


with zeros
Initializing Numpy array

Initializing Numpy array


with same number
Initializing Numpy array

Initializing Numpy array within a range


Initializing Numpy array

Initializing Numpy array with random


numbers
Numpy-Shape

Checking the shape of Numpy array


Joining Numpy Arrays

Vstack() hstack() Column_stack()


Numpy intersection & Difference
Numpy array Mathematics

Addition of Numpy Arrays


Numpy array Mathematics

Basic Addition Basic Multiplication

Basic Subtraction Basic Division


Numpy Math Functions

Mean Standerd Deviation

Median
Numpy Save & Load

Saving Numpy Array

Loading Numpy Array


Python Pandas

Pandas stands for Panel Data and is the core library


for data manipulation and data analysis

It consists of single and multidimensional


data structures for data manipulation
Pandas Series Object(One Dimensional)

Series Object is one dimensional labeled array


Changing Index
Series Object from Directory

You can also create a series object from a dictionary!!


Changing Index Position

You can change index position


Changing Index Position

Extracting a single element Extracting a single element

Extracting a single element


Basic Math Operation on Series

Addition a scalar value to series Elements


Pandas Data frame(2- Dimensional)

Dataframe is a 2-dimensional labelled data-structure


*A dataframe comprises of rows and columns.
How to Create Data frame
Create DataFrame with dictionary array
Create DataFrame with dictionary array
Sort by value
Slicing DataFrame
Query DataFrame by Tag
Copy a DataFrame
How to Null a DataFrame
How to modify an element in DataFrame
How to perform operation on index
DataFrame using string
How to convert DataFrame string in lower
case
How to convert DataFrame string in upper
case
Operation for DataFrame missing values
Visualization in pandas
Python Matplotlib

Matplotlib is a python library used for data


visualization

You can create bar plot ,scatter plots,


histograms and a lot more with matplotlib
Line plot
Line plot
Adding title & Labels
Line plot

Changing Line Aesthetics


Line plot

Adding two lines on same plot


Line plot

Adding Sub Plot


Bar plot
Bar plot
Bar plot
Horizontal Bar Plot
Scatter plot

Creating a basic scatter plot


Scatter plot

Changing Marks Aesthetics


Scatter plot

Adding two markers


in the same plot
Scatter plot
Adding Sub-plot
Histogram
Histogram

Changing Aesthetics
Assignment -01

Q.1 Write a Python program to sum all the items in a list ?

Q.2 Write a Python program to get the largest number from a list.

Q.3 Write a Python program to create a tuple with different data types.

Q.4 Write a Python program to get the 4th element and 4th element from last of a tuple.

Q.5 Write a Python program to sort a list of tuples using Lambda.

Q.6 Write a Python program to find those numbers which are divisible by 7 and multiple of 5,
between 1500 and 2700 (both included).
Assignment -02

Q.1 Write a Python program to add member(s) in a set ?

Q.2 Write a Python program to sort a list of tuples using Lambda ?

Q.3 Write a Python function to find the Max of three numbers ?

Q.4 Write a Python program to find the median of three values ?

Q.5 Write a Python program to get the class name of an instance in Python ?

Q 6 Write a Python program to insert items into a list in sorted order ?


Expected Output:
Original List:
[25, 45, 36, 47, 69, 48, 68, 78, 14, 36]
Sorted List:
[14, 25, 36, 36, 45, 47, 48, 68, 69, 78]

You might also like