Ds Python Unit-I
Ds Python Unit-I
Ds Python Unit-I
Unit-I
Data Analysis
Data analysis is the process of inspecting, collecting , cleaning, transforming,
and modeling data using various statistical and logical methods and techniques to
discover useful information .
Data Analytics
Data analytics is the process of exploring and analyzing large datasets to find
hidden patterns, unseen trends, discover correlations, and derive valuable insights to
make business predictions. It improves the speed and efficiency of your business.
It is described as a traditional
1. It is described as a particularized form of analytics.
form of analytics.
Python Introduction
Python is a very powerful high-level, general-purpose, object-oriented
programming language created by Guido van Rossum in 1989.
Python is very popular for developers and professionals because, it has a rich set of
supporting libraries, and addon modules. It is a cross-platform(platform-independent)
language and runs on all major operating systems including Windows, Linux, and
Macintosh. Python comes with a powerful and easy to-use graphical user interface
(GUI) toolkit that makes the task of developing GUI applications.
The Python programming language was conceived in the late 1980s, and its
implementation was started in December 1989 by Guido van Rossum at CWI(Centrum
Wiskunde & Informatica) in the Netherlands as a successor of ABC programming
language. Python was published in 1991.
Python Versions and Release Dates
Features of Python
2) Expressive Language
Python can perform complex tasks using a few lines of code.
3) Interpreted Language
Python is an interpreted language; it means the Python program is executed
one line at a time. The interpreted language makes debugging easy and
portable.
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux,
UNIX, and Macintosh, etc. So, we can say that Python is a portable language.
6) Object-Oriented Language
Python supports object-oriented language.It supports
object-oriented concepts inheritance, polymorphism, encapsulation, etc.
9) Integrated
Python can be integrated with other languages,
like C, C++, and Java.
Python library
A Python library is a collection of related modules. these libraries can be used
repeatedly in different programs. It makes Python Programming simpler and
convenient for the programmer. As we don't need to write the same code again and
again for different programs.
1. NumPy
NumPy is a popular open-source library for data processing and modeling, that is
widely used in data science, machine learning, and deep learning. NumPy introduces
objects for multidimensional arrays and matrices.
2. Pandas
Pandas is an open-source library for data cleaning and processing. Pandas
specializes in manipulating numerical tables and time series, which are common
data forms in data science.
3. SciPy
Scipy stands for Scientific Python. SciPy is an open-source library for data
processing and modeling for scientific applications. It is useful in solving
many mathematical equations and algorithms.
4. TensorFlow
Tensor Flow is a free and open-source software library for machine learning and
artificial intelligence. Tensor Flow is a library for high-performance numerical
computations. It’s used across various scientific fields.
5.Matplotlib
Matplotlib is an extensive library for creating fixed, interactive, and animated
Python visualizations. Matplotlib is designed to be as functional as MATLAB, with the
additional benefit of being able to use Python. It also free and open source Library.
6.SQLite3
Python programming language provides a library for database operations. Python
SQLite3 module is used to integrate the SQLite database with Python. It is mainly used
for database operations using sql queries. There is no need to install this Library
separately as it comes along with Python after the 2.5x version.
7.Scikit-learn
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 and clustering.
8.Statsmodels
statsmodels is a Python Library that provides a complement for statistical
computations including descriptive statistics ,estimation and inference for statistical
models.
Character set:
Any Programming language is required to represent its characters with the
help of character set. An ASCII character set is used to represent characters up to
3rd generation languages(c/c++). An ASCII character set supports 8 bit characters.
The number of characters that can be represented by this coding system is limited
to 256 only. In order to extend ASCII coding system, an universal coding system
(UNICODE) is introduced. Python adapted this coding system for representing its
characters.
Note: There are 65536(0 to 65535) characters are available
in UNICODE character set.
Keywords
Keywords are the reserved words in Python. There are 35 keywords in
Python 3.10 This number can vary in different versions of Python.
Eg:
>>> 2*10
20
>>> 3+4+9
16
>>> 2**3
8
Python has a rich set of fundamental data types. The operations on an object
depends on its data type.
–2147483648 to 2147483647
Long Integers:
It has unlimited, subject to the memory limitations of the computer.
Strings:
Sequences of Unicode characters. A string can be defined either Single quotes
(or) double quotesor (or) triple quotes. By using triple quotes we can write
multi-line strings.
Boolean:
It can hold only one of two possible values: True or False.
Complex Number:
A complex number has a real and an imaginary component, both represented
by float types in Python. For instance, 2+3j is a complex number, where 3j is the
imaginary component and is
equal to √-9 (√9 × √−1 = 3i or 3j)
Eg1:
>>> complex(2,3)
(2+3j)
>>> type(10.0)
<class 'float'>
>>> type(‘abc’)
<class 'str'>
>>> type(True)
<class 'bool'>
>>> type(2+3j)
<class 'complex'>
Variables
Variables are used for storing data in a program. Therefore, by assigning
different data types to variables(you can store numbers, strings,etc.).In Python,
we don't need to specify the data-type of the variable. When we assign some value
to the variable, it automatically allocates the memory to the variable at run time.
For example
A = 10
value = 10.0
st_value="Hello World!"
You can see in the preceding examples the variable can be a single character or a
word or words connected with underscores.
Multiple Assignment
1) Python allows you to assign a single value to several variables.
For example
a=b=c=1
Here, an integer value 1 is assigned all the three variables.
2)For example –
a,b,c=1,2,'Hello'
Here, two integer values 1 and 2 are assigned to variables a and b respectively,
and a string "Hello" is assigned to the variable c.
a=5
print('The value of a is', a)
Output: The value of a is 5
print(1,2,3,4,5)
Output: 1 2 3 4 5
print(1,2,3,4,5,sep='*')
Output: 1*2*3*4*5
print(1,2,3,4,5,sep='#',end='$')
Output: 1#2#3#4#5$
Comments:
Un executable lines in a program are called as comment lines. Comments lines
are for documentation purposes and these lines are ignored by the interpreter.
’’’
---------------------
--------------------- Multi line comment
----------------------
’’’
Writing Python Programs(Scripts)
We can write Python programs either through IDLE’s built-in editor or through
any editor. We will be using python IDLE’s editor for writing programs. We can
launch IDLE and then select the File > NewFile option to open IDLE’s built-in
editor and write the following small program.
Note: Python script(program) extension is .py
test.py
# This is First program
print ("Hello World!")
print ("Welcome to Python Programming")
P1.py
eno = 100 # An integer assignment
ename = "HHHH" # A string assignment
salary = 20000.00 # A floating point assignment
print("Employ Number :",eno)
print("Employ Name :", ename)
print("Salary :",salary)
Format strings
We can use format strings in print() function like printf() style used in ‘C’
language.
The list of format Strings
eg1:
C Language
int n=10;
printf("value of n = %d",n);
python
n=10
print("value of n = %d"%n)
eg2:
>>> m=10;n=20
prg: Emp.py
eno=100
ename='HHHH'
grade='A'
salary=20000.00
print ("Employ Number :%d"%eno)
print ("Employ Name :%s"%ename)
print ("Grade :%c"%grade)
print ("salary :%.2f"%salary)
Escape Sequences
Python uses some backslash characters in output functions for formatting the
output. These characters are called as Escape sequence characters. In these
characters it consists of two characters ,But it is treated as a single character
Eg:
print('Hello \n Welcome to Python Programming')
print('Name\tEmail Address\tContact Number')
python Input
We want to take the input from the user, We use input() function.
Syntax :
input([prompt])
2) int(x)
This function converts string type to integer.
Program
Conv1.py
3. ord(x) :
converts a character to integer(Unicode value).
4.chr(x) :
Converts an integer(Unicode value) to a character
5. str(x) :
Converts integer (or) float into a string.
6.complex(real,imag) :
Converts real numbers to complex number
Eg(Unicode values):
A to Z 65 to 90
a to z 97 to 122
0 to 9 48 to 57
Esc 27
Backspace 8
Enter 13
SpaceBar 32
Tab 9
etc.
program
Conv2.py
c = ord('A')
print ("After converting character to integer :",c)
c = chr(65)
print ("After converting integer to character :",c)
c = str(123)
print ("After converting integer to string :",c)
c = complex(1,2)
print ("After converting integers to complex number :",c)
Python Indentation
Most of the programming languages like C, C++and Java use curly braces { } to
define a block of code.
Python uses whitespace indentation to define blocks rather than curly
braces . A code block (body of a function, loop etc.) starts with indentation and
ends with un-indentation line.
Python Operators
Operator :
Operator is special symbol in Python and it performs a particular
operation.
Operand :
The value on which the operator acts is called as operand.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication etc.
Ari.py
print('Enter any two Numbers ')
a=int(input())
b=int(input())
print('Addition :',a+b)
print('Subtraction :',a-b)
print('Multiplication :',a*b)
print('Division :',a/b)
print('Modulus :',a%b)
print('Floor Division :',a//b)
print('Exponent :',a**b)
round()
Python provides an inbuilt function round() ,which rounds precision digits of
floating point number.
Syntax:
round(floating number, no of precision digits)
>>>round(10.639 , 2)
10.64
>>> round(10/3,2)
3.33
>>> n=10.3159
>>> n
10.3159
>>> round(n,2)
10.32
2)Relational operators
These are used to test the relation between 2 values or 2 expressions.
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
Logical operators :
These are used to combine the result of 2 or more expressions.
and Logical AND
or Logical OR
not Logical NOT
Bit-wise Operators :
These operators are used for manipulation of data at bit level.
Bit-wise logical operators :
These are used for bit-wise logical decision making.
& Bit-wise logical AND
Bit-wise logical OR
B1 B2 B1 & B2 B1 B2 B1 ^ B2
------------------------------------------------------------------
1 1 1 1 0
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0
Eg : int a=5,b=6;
a=5 - 1 0 1
b=6 - 1 1 0
1 0 1
1 1 0
----------------------
a&b : 1 0 0 =4
----------------------
1 0 1
1 1 0
----------------------
a|b : 1 1 1 =7
----------------------
1 0 1
1 1 0
----------------------
a^b : 0 1 1 =3
----------------------
Bit1.py
print('Enter any two Numbers ')
a=int(input())
b=int(input())
print('a & b :',a&b)
print('a | b :',a|b)
print('a ^ b :',a^b)
Bit2.py
a=4
b=a<<2
c=a>>1
print('a :',a)
print('b :',b)
print('c :',c)
Assignment Operators :
These operators are used to assign values to variables.
Simple assignment :
=
compound assignment
+=
-=
*=
/=
//=
%=
**=
Eg:
>>> n=10
>>> n
10
>>> n+=5
>>> n
15
Special operators
Python language offers two types of special operators. They are
1.identity operators
2.membership operators
Identity operators
‘is’ and ‘is not’ are the identity operators in Python. They are used to check two
values or variables.
eg:
>>> a=10
>>> b=10
>>> c=20
>>> a is b
True
>>> a is not c
True
Membership operators
‘in’ and ‘not in’ are the membership operators in Python.
They are used to test whether a value or variable is found in a sequence(string,
list, tuple,set,Etc.).
>>> st='abcd'
>>> 'a' in st
True
>>> 'k' not in st
True
>>> x='a'
>>> x in st
True
>>> "WEL" in "WELCOME"
True
>>> "WCE" in "WELCOME"
False
Control Flow Statements :
1.Simple if statement
It is a Conditional control statement in Python
The general form of the if statement is:
if condition :
block
2. if-else statement
It is an extension of simple if statement.
if condition :
if block
else :
else block
if a> b :
max=a
else :
max=b
print("Maximum value :",max)
Form : 1
if condition-1 :
if condition-2 :
………….
………..
if condition-n :
Statements
Form : 2
if condition-1 :
if condition-2 :
Statement-1
else :
Statement-2
else :
if condition-3 :
Statement-3
else :
Statement-4
4. if-elif-else statement
This statement is also used for a series of decisions are involved.
Syntax:
if condition-1 :
statements-1
elif condition-2 :
statements-2
………………….
………………….
elif condition-n:
statements-n
else :
else block - statements
In this statement, the conditions are evaluated from top to bottom, if the
condition is true then the statements associated that block is executed and the
control transfers to the next statement. Otherwise when all conditions are false then
the final else block statements will be executed.
While loop
It is a conditional controlled loop statement in python.
Syntax:
while condition :
statements
In this loop first the condition will be evaluated. If it is true, then the
statement block will be executed. After the execution of statements, the condition
will be evaluated once again, if it is true then the statement block will be executed
once again. This process of repeated execution continues until the condition
becomes false.
range(x):
Returns a list whose items are consecutive integers from 0 to x-1 .
range(x, y):
Returns a list whose items are consecutive integers from x to y-1 .
range(x, y, step):
Returns a list of integers from x to y-1 , and the difference between each
successive value is the value defined by step.
#To Display Even and Odd numbers from 1 to given number using for loop
n=int(input("Enter any Number : "))
print("\nEven number from 1 to",n)
for i in range(2,n+1,2):
print(i,end='\t')
Infinite Loop
An infinite loop is a loop that executes its block of statements repeatedly until
the user forces the loop to quit.
break.py
To display Numbers from 1 to given number,if the given number is greater than
20 it displays only 1 to 20 using break.
write a program to display numbers from 1 to 10 except for the values 4 and 7
using continue.
program
k=1
while k <=10 :
if k==4 or k==7:
k=k+1
continue
print (k,end=’\t’)
k=k+1
Output:
1 2 3 5 6 8 9 10
write a program to display numbers from 1 to 10 except for the values 4 and 7
using pass
pass.py
k=1
while k <=10 :
if k==4 or k==7:
pass
else:
print (k,end='\t')
k+=1
Output:
1 2 3 5 6 8 9 10