0% found this document useful (0 votes)
1K views

1 1pythonworld PDF

Python is well-suited for data science tasks due to its simple syntax, rich library ecosystem, and readability. The document outlines the key reasons to learn Python for data science, including that it is a free, flexible language that allows for data manipulation, analysis, and visualization using powerful libraries for machine learning and scientific computing. It then provides an overview of the core Python concepts and libraries to learn for data science, such as NumPy for efficient data handling, Pandas for data analysis, Matplotlib for visualization, and Scikit-learn for machine learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

1 1pythonworld PDF

Python is well-suited for data science tasks due to its simple syntax, rich library ecosystem, and readability. The document outlines the key reasons to learn Python for data science, including that it is a free, flexible language that allows for data manipulation, analysis, and visualization using powerful libraries for machine learning and scientific computing. It then provides an overview of the core Python concepts and libraries to learn for data science, such as NumPy for efficient data handling, Pandas for data analysis, Matplotlib for visualization, and Scikit-learn for machine learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Why Learn Python For Data Science?

• Python is a free, flexible and powerful open


source language
• Python cuts development time in half with its
simple and easy to read syntax
• With Python, you can perform data
manipulation, analysis, and visualization
• Python provides powerful libraries for
Machine learning applications and other
scientific computations
What you learn for data science?
• Python is a general-purpose programming language that is
becoming more and more popular for doing data science.
• Companies worldwide are using Python to harvest insights from
their data and get a competitive edge. Learn
• Python Basics
- Learn how to use Python both interactively and through a script
• Functions and Packages
- learn about using functions, methods and packages. This will
help you to reduce the amount of code you need to solve
challenging problems!
• Python data structures-Lists,tuple,set,dictionary
- Learn to store, access and manipulate data
• NumPy
- Python package to efficiently do data science.
Learn to work with the NumPy array, a faster and
more powerful alternative to the list
• Python Pandas
- Learn Pandas for Data Analysis
-manipulate data in DataFrame and Series
• Matplotlib
- a python package used for 2D graphics and data
visualization
• Scikit learn – Machine Learning using Python
Python covers
Python Job Profiles

• Software Engineer
• Python Developer
• Research Analyst
• Data Analyst
• Data Scientist
• Software Developer
Python Salary
payscale.com
TIOBE Index for Aug 2018 – Python
Career
Python in 4 th place for programming community index
Basic Python Interview Questions

LIST TUPLES

Lists are mutable i.e they can be Tuples are immutable (tuples are
edited. lists which can’t be edited).

Lists are slower than tuples. Tuples are faster than list.

Syntax: tup_1 = (10, ‘Chelsea’ ,


Syntax: list_1 = [10, ‘Chelsea’, 20]
20)
How can you generate random
numbers in Python?
• import random
• random.random
• randrange(a, b): it chooses an integer and define the range in-
between [a, b). It returns the elements by selecting it randomly
from the range that is specified. It doesn’t build a range object.
• uniform(a, b): it chooses a floating point number that is defined in
the range of [a,b).Iyt returns the floating point number
• normalvariate(mean, sdev): it is used for the normal distribution
where the mu is a mean and the sdev is a sigma that is used for
standard deviation.
• The Random class that is used and instantiated creates an
independent multiple random number generators.
variables
Assigning values to a variable:

• S = 10
• print(S)
Data strucures in Python:
Data Types
Numeric:
A = 10
# Convert it into float type
B = float(A)
print(B)

A = 10.76
# Convert it into float type
B = int(A)
print(B)
List:
Subjects = ['Physics', 'Chemistry', 'Maths', 2]
print(Subjects)

Tuples:

Chelsea = ('Hazard', 'Lampard', 'Terry')

Strings:
S = "Welcome To Saveetah"
D = ' Saveetah ‘

Set: every element has to be unique


Set_1 = {1, 2, 3}
Dictionary: key-value pair
Dict = {'Name' : 'Saurabh', 'Age' : 23}
Dict['Age'] = 32
Dict['Address'] = 'Starc Tower'
Operators in Python:
Arithmetic Operators:
a = 21
b = 10
c=0

c=a +b
print ( c )

c=a -b
print ( c )

c=a *b
print ( c )

c=a /b
print ( c )

c=a %b
print ( c )
a=2
b=3
c = a**b
print ( c )

Output = 31, 11, 210, 2.1, 1, 8


Comparison Operators:
a = 21
b = 10
c=0

if ( a == b ):
print ("a is equal to b")
else:
print ("a is not equal to b")

if ( a != b ):
print ("a is not equal to b")
else:
print ("a is equal to b")

if ( a < b ): print ("a is less than b")


else: print ("a is not less than b")

if ( a > b ):
print ("a is greater than b")
else:
print ("a is not greater than b")
cont.....
a=5
b = 20
if ( a <= b ): print ("a is either less than or equal to b")
else: print ("a is neither less than nor equal to b")

if ( a => b ):
print ("a is either greater than or equal to b")
else:
print ("a is neither greater than nor equal to b")
Output = a is not equal to b a is not equal to b a is not less than b a
is greater than b a is either less than or equal to b b is either
greater than or equal to b
Assignment Operators:
a = 21
b = 10
c=0

c=a +b
print ( c )

c += a
print ( c )

c *= a
print ( c )

c /= a
print ( c )

c =2
c %= a
print ( c )

c **= a
print ( c )
Output = 31, 52, 1092, 52.0, 2, 2097152, 99864
Bitwise Operators:
a = 58 # 111010
b = 13 # 1101
c=0

c=a &b
print ( c ) # 8 = 1000

c=a |b
print ( c ) # 63 = 111111

c=a ^b
print ( c ) # 55 = 110111

c = a >> 2
print ( c ) # 232 = 11101000

c = a << 2
print ( c ) # 14 = 1110
Output = 8,63,55,232,14
Logical Operators:

x = True
y = False

print('x and y is',x and y)

print('x or y is',x or y)

print('not x is',not x)
Output = x and y is False x or y is True not x is False
Membership Operators:

X = [1, 2, 3, 4]
A=3
print(A in X)
print(A not in X)
Output = True False
Identity Operators:

X1 = 'Welcome To saveetha'

X2 = 1234

Y1 = 'Welcome To saveetha'

Y2 = 1234

print(X1 is Y1)

print(X1 is not Y1)

print(X1 is not Y2)

print(X1 is X2)
Output = True False True False
Conditional Statements:
Example
X = 10
Y = 12

if X < Y: print('X is less than Y')


elif X > Y:
print('X is greater than Y')
else:
print('X and Y are equal')
Output = X is less than Y
Loops:
Loop works
• First the control will check the condition. If it is
true then the control will move inside the loop
and execute the statements inside the loop.
• Now, the control will again check the condition, if
it is still true then again it will execute the
statements inside the loop.
• This process will keep on repeating until the
condition becomes false. Once the condition
becomes false the control will move out of loop.
Loops in Python:

There are two types of loops:


• Infinite: When condition will never become
false
• Finite: At one point, the condition will
become false and the control will move out of
the loop
• While
• For
• Nested
while
count = 0
while (count < 10):
print ( count )
count = count + 1

print ("Good bye!")


Output = 0 1 2 3 4 5 6 7 8 9 Good bye!
for
fruits = ['Banana', 'Apple', 'Grapes']

for index in range(len(fruits)):


print (fruits[index])
Output = Banana Apple Grapes
Nested for loop
for i in range(10):
print (str(i) * i)

for j in range(0, i):


count = count +1
Output = 1 22 333 4444 55555 666666 7777777
88888888 999999999
Functions:
ex
def add (a, b):
return a + b
c = add(10,20)
print(c)
Output = 30
Read/write/append file
f= open(“saveetha.txt") # equivalent to 'r' or 'rt'
f = open ("saveetha.txt",'w') # write in text mode
f = open ("img1.bmp",'rb' ) # read and write in binary mode

f = open (“saveetha.txt")
f.close()
Python world
CSE2602/CSE2603
Python programming for Data Analytics

3 Credits
Book2(Unit1-4): Fabio Nelli “Python Data Analytics Data Analysis and Science
Using Pandas, matplotlib, and the Python Programming Language” Apress
Media LLC, 233 Spring Street, New York, 2015.

Book3(Unit5):Wes McKinney “Python for Data Analysis” Published by O’Reilly


Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
October 2012: First Edition
Unit I
Introduction to the Python’s World - Python—The Programming
Language – Python – The Interpreter - Python 2 and Python 3 - Installing
Python- Python Distributions - Using Python -Writing Python Code –
Ipython - The IDEs for Python – SciPy.
Programming Language(30 Mins)

• Features of Python
• Shell mode
• Interactive mode
• Compare C, Java and Python
• Code
• Advantages of python
Features
• created by Guido Von Rossum in 1991
• starting with the previous language called ABC.
series of features:
• interpreted
• portable
• object-oriented
• interactive
• interfaced
• open-source
• easy to understand and use
features
• Python is a programming language interpreted, that is pseudo-
compiled.
• code of a program, needs an interpreter.
• The interpreter is a program that has the task of interpreting the
source code and run it. Therefore unlike language such as C, C ++,
and Java, there is no compile time.
• Python is a highly portable programming language. The decision
to use an interpreter as an interface for reading and running the
code has a key advantage: portability.
• In fact, you can install on any existing platform (Linux, Windows,
Mac) an interpreter specifically adapted to it while the Python
code to be interpreted will remain unchanged.
• Python also, for this aspect, was chosen as the programming
language for many small-form devices, such as the Raspberry Pi
and other microcontrollers.
oops
• Python is an object-oriented programming
language. In fact, it allows you to specify
classes of objects and implement their
inheritance.
• But unlike C ++ and Java there are no
constructors or destructors.
Interactive
• launch the program, or you can enter a command
line at once and execute it, immediately getting
the results of the command, and depending on
them you can decide what will be the next line of
command to be run.
• This highly interactive mode to execute code
makes Python a computing environment
perfectly similar to Matlab.
• This is a feature of Python that brought the
success of this programming language in the
scientific community.
Interfaced
• Python is a programming language that can
be interfaced.
• This programming language has among its
features the characteristic to be interfaced
with code written in other programming
languages such as C / C ++ and Fortran.
Open source and simple
• Python is an open-source programming language.
CPython, which is the reference implementation of
the Python language is completely free and open-
source.
• Additionally every module or library in the network is
open-source and their code is available online.
• Finally, Python is a simple language to use and learn.
This aspect is perhaps the most important of all
because it is the most direct aspect which a
developers facing difficulties in existing programming
languages due to their complexity.
Modes of python execution
• Shell mode-Interactive
Each time you run the python command the
Python interpreter starts, characterized by a
>>> prompt.
• The Python interpreter is simply a program
that reads and interprets the commands
passed to the prompt.
• Script mode
Entire code in a file can be executed at a time
comparison
➢ Java pgm to print helloworld ➢ C pgm to print helloworld
Public class hello{ #include<stdio.h>
Public static void main(String[] args) Void main()
{ {
System.out.println(“hello world”); printf(“hello world”);
}} }

➢ Python pgm to print helloworld

>>>Print “hello world”


Advantages and applications
1. Desktop Applications
2. Web Applications→Django
3. Database Applications
4. Networking Applications
5. Game Development
6. Data Analysis
7. Machine Learning
8. Artificial Intelligence
9. IoT Applications
10. But not for Mobile Applications ,not recommended
as of now
Tokenization, Byte code and PVM
• Each time you press the Enter key, the
interpreter begins to scan the code written
(either a row or a full file of code) token by token
(tokenization). These tokens are fragments of
text which the interpreter will arrange in a tree
structure.
• The tree obtained is the logical structure of the
program which is then converted to bytecode (.
pyc or .pyo).
• The process chain ends with the bytecode which
will be executed by a Python virtual machine
(PVM)
Steps in interpretter
Ex: Tokenize given text and words
text = ‘Welcome to sse'

# Splits at space
print(text.split())

word = ‘apple,orange,mango'

# Splits at ','
print(word.split(', '))

word = ‘name:age:dob'

# Splitting at ':'
print(word.split(':'))

word = 'CatBatSatFatOr'

# Splitting at 3
print([word[i:i+3] for i in range(0, len(word), 3)])
output

[‘welcome', ‘to', ‘sse']


[‘apple', ‘orange', ‘mango']
[‘name', ‘age', ‘dob']
['Cat', 'Bat', 'Sat', 'Fat', 'Or']
Types of interpretter
• Cython

• Jython

• PyPy
Cython
• The project Cython is based on creating a compiler
that translates Python code into C code equivalent.
• This code is then executed within a Cython
environment at runtime.
• This type of compilation system has made possible
the introduction of C semantics within the Python
code to make it even more efficient.
• This system has led to the merging of two worlds of
programming language with the birth of Cython that
can be considered a new programming language.
• URL is http://docs.cython.org
Jython
• In parallel to Cython, there is a version totally
built and compiled in Java, named Jython.
• It was created by Jim Hugunin in 1997
• Jython is a version of implementation of the
Python programming language in Java;
• it is further characterized by using Java
classes instead of Python modules to
implement extensions and packages of
Python.
• (http://www.jython.org).
PyPy
• The PyPy interpreter is a JIT (just-in-time)
compiler, which converts the Python code
directly in machine code at runtime.
• This choice was made to speed up the
execution of Python.
• However, this choice has led to the use of a
small subset of Python commands, defined as
RPython.
• http://pypy.org.
Summary
• Open source
• Simple
• Easy to learn
python2 vs python 3
why version 2.x is still being released if it is distributed
around a much more enhanced version such as 3.x?
• Guido Van Rossum decided to bring significant
changes to the Python language, but he soon found
that these changes would make new Python
incompatible with a lot of existing code. Thus he
decided to start with a new version of Python called
Python 3.0.
• To overcome the problem of compatibility and create
huge amounts of code unusable spread to the
network, it was decided to maintain a compatible
version, 2.7 to be precise.
Compare python 2 & 3
Example 1: Array of bytes from a
string-Bytearray()
• The bytearray() method returns a bytearray
object which is an array of the given bytes.
string = "Python is interesting.“
# string with encoding 'utf-8‘
arr = bytearray(string, 'utf-8')
print(arr)

When you run the program, the output will be:


bytearray(b'Python is interesting.')
Example 2: Array of bytes of given
integer size
size = 5
arr = bytearray(size)
print(arr)

When you run the program, the output will be:


bytearray(b'\x00\x00\x00\x00\x00')
Example 3: Array of bytes from an
iterable list
rList = [1, 2, 3, 4, 5]
arr = bytearray(rList)
print(arr)

Output:
bytearray(b'\x01\x02\x03\x04\x05')
Bytes()
• The bytes() method returns a immutable bytes object initialized
with the given size and data.
• The syntax of bytes() method is:
bytes([source[, encoding[, errors]]])
The bytes() method returns a bytes object which is an immmutable
(cannot be modified) sequence of integers in the range 0 <=x <
256.
bytes() Parameters
The bytes() takes three optional parameters:
• source (Optional) - source to initialize the array of bytes. And it
can be string,integer,iterable objects like list,tuple etc
• encoding (Optional) - if source is a string, the encoding of the
string.
• errors (Optional) - if source is a string, the action to take when
the encoding conversion fails (Read more: String encoding)
Example 1: Convert string to bytes
string = "Python is interesting.“
# string with encoding 'utf-8‘
arr = bytes(string, 'utf-8')
print(arr)
Output:
b'Python is interesting.'
Ex: unordered types
Python cannot order str values and int values
together.
Ex:
x=0
time=input(“enter year”)
while (x < time):
statements
TypeError: unorderable types: int() < str()
To fix this, simply use int( input(“enter year”)) to
convert your string to an integer
Important differences
Python 2.x Vs Python 3.x

• Division operator
• Print function
• Unicode
• Xrange
• Error handling
Important differences
Python 2.x Vs Python 3.x
• Division operator
In python 3.x
Ex:print 7 / 5 output is 1.4
print -7 / 5 output is -1.4
But in python 2.x ,the same code will output as 1 and -2
5/2=2 in python2 where as 5/2=2.5 in python 3
• Print function
The print function in Python 2.x is replaced by print() function in Python 3.x.i.ePython
3.x need an extra pair of parenthesis.
• Unicode
In Python 2, implicit str type is ASCII(8 bit). But in Python 3.x implicit str type is Unicode.
(16 bit)
• Xrange
Python 3.x’s range function is xrange from Python 2.x.Python 3 gives name error for
xrange.
• Error handling
In python 3.x, ‘as’ keyword is required in except Nameerror as err
Instead python 2 uses except Nameerror, err
Installing Python
On Debian-Ubuntu Linux systems
$ apt-get install python
(or)
$ sudo apt-get update
$ sudo apt-get install python3.7

On Red Hat, Fedora Linux systems working with rpm packages


yum install python

To see the version of python


$ python –version
Python 1.1
$ python2 –version
Python2.2
$python3 –version
Python 3.6.5
If your operating system is Windows or Mac OS X
you can go on the official Python site (http://www.python.org) and download the version you prefer.
The packages in this case are installed automatically.
Installation steps-windows
Step 1: Download the Python 3 Installer
• Open a browser window and navigate to the Download page for
Windows at python.org.
• Select Python Releases for Windows, click on the link for
the Latest Python 3 Release - Python 3.x.x.
• Scroll to the bottom and select either Windows x86-64 executable
installer for 64-bit or Windows x86 executable installer for 32-bit.
Step 2: Run the Installer
Then just click Install Now. You have a working Python 3
installation on your system.
Online Python Interpreters

• If you want to try without installing Python on


your machine, there are several web sites
available where you can interact with a Python
interpreter online:
• Python.org Online Console: www.python.org/
shell
• Python Fiddle: pythonfiddle.com
• Repl.it: repl.it
• Trinket: trinket.io
• Python Anywhere: www.pythonanywhere.com
Python 2 &3 sample code

Python2
Print “hello world”

Python3
Print(“hello world”)
Python Interpreter Interactively

• To start talking to Python is in an interactive Read-


Eval-Print Loop (REPL) environment.
• That simply means starting up the interpreter and
typing commands to it directly.
The interpreter:
• Reads the command you enter
• Evaluates and executes the command
• Prints the output (if any) to the console
• Loops back and repeats the process
• The session continues in this manner until you
instruct the interpreter to terminate.
Shell mode
Starting the Interpreter
• Start menu labeled Python 3.x, and under it a menu item
labeled Python 3.x (32-bit),
• Opens a window with >>> propmt
Executing Python Code
• Ensure that the >>> prompt is displayed, and the cursor is
positioned after it.
• Type the command print("Hello, World!") exactly as shown.
• Press the Enter key.
Exiting the Interpreter
Type exit() and press Enter or
type Ctrl+Z and press Enter
Script mode running
Running a Python Script from the Command
Line
• C:\Users\>python hello.py
Hello, World!
• $ python hello.py in linux
Hello, World!
Starting IDLE

Windows
• Start menu and select All Programs-> IDLE
(Python 3.x 32-bit)
• Click on the icon to start IDLE.

Starting IDLE in Linux


• install IDLE.
• Then, type idle3 in a terminal window and
press Enter to run it.
Python2 pgm
#pgm using function for random number generation from 1 to 99

import random

#input number
n=int(raw_input("enter a number"))
if n>99:
print n,"is anumber too high"
elif n<=1:
print n," is a number too low"
else:
print n,"is a correct number"
print "the random number generated for you is ",random.randrange(1,n)
Python3 pgm
# Program to generate a random number between 1 and 99
# import the random module
import random
n=int(input("enter a number"))
if n>99:
print (n,"is anumber too high“)
elif n<=1:
print (n," is a number too low“)
else:
print (n,"is a correct number“)

print("the random number generated for you is ", random.


randint(1,99))
Greeting pgm
• #greet in language of choice using function
• def greets(language):
• if language=='english':
• print 'good morning'
• elif language=='tamil':
• print 'vanakkam'
• elif language=="hindi":
• print "namasthe"
• else:
• print 'hello'

• for i in range(3):
• language=raw_input("enter ur language")
• greets(language)
Python3 pgm?
Harshad number?
#program to find harshad number
#harshad number is a number that is divisible by the sum of its digits

def ishashad(a):
n=a
d1=n%10

d2=n/10
div=d1+d2
if a%div==0:
print "number is harshad "
else:
print "number is not harshad"

print "welcome to bsa python course"


a=input("Enter the number")
ishashad(a)

Output?
a=0
while a>100:
print a
a+=1
print(a)
ans
• 0
Output?
What is output?
def f():
try:
print "a"
return
except:
print "b"
else:
print "c"
finally:
print "d"

f()
ans
a
d
3. What is output?

try:
print "a"
raise Exception("doom")
except:
print "b"
else:
print "c"
finally:
print "d"
ans
a
b
d
Escape sequences

• >>> print("a\tb")
a b
• >>> print("a\nb")
a
b
• >>> print("a\\tb")
Print multiple lines with tripple quote
• >>> print("""This is a string
that spans across
several lines""")
This is a string
that spans across
several lines
Type() function
• >>> type(22)
<class ‘int'>
• >>> type(4.2)
<class ‘float'>
Other hints
• specify the string literal hi‘john in Python
‘hi\’john
OR
“””hi’john”””

You might also like