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

Walk Into Python: Data Processing Using Python

This document introduces Python as an interpretative, object-oriented, high-level programming language and discusses its history, features, and applications. It provides examples of Hello World programs in Python and discusses how to set up a Python development environment. The document also covers basic Python concepts like variables, data types, input and output functions, comments, and installing additional packages.

Uploaded by

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

Walk Into Python: Data Processing Using Python

This document introduces Python as an interpretative, object-oriented, high-level programming language and discusses its history, features, and applications. It provides examples of Hello World programs in Python and discusses how to set up a Python development environment. The document also covers basic Python concepts like variables, data types, input and output functions, comments, and installing additional packages.

Uploaded by

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

Data Processing Using Python

Walk into Python


ZHANG Li/Dazhuang
Department of Computer Science and Technology
Department of University Basic Computer Teaching
2

Data Processing Using


Python

AN INTRODUCTION TO PYTHON

Nanjing University
What is Python 3

simple

Clear-cut
Ease to use, similar to script languages
and interpretive programming languages
elegant
It has all the powerful functions of a traditional
compiler language

Python is an interpretative, object-oriented, high-level programming language


with dynamic semantics.

Nanjing University
4

• The first Python compiler/interpreter was


born in 1991

• The name of Python comes from Guido's


beloved TV show Monty Python's Flying
Circus

• Python is between C and Shell,


comprehensive, easy to learn, and extensible

Nanjing University
History of Python 5

• • Glue Language
胶水语言(Glue Language)
It is easy to connect to and integrate with other well-known program languages (like C/C + +) 脚本语

言(Script Language)
• Script Language
Advanced script language, which is more powerful than general script languages that can handle only simple tasks 面

向对象语言(Object-Oriented Language)
• Object-Oriented Language

Full support to inheritance, overload, derivation, and multiple inheritance

Nanjing University
6
Features of Python

Portable, upgradable, and extendable

Robust, interpretative and buildable

Easy to learn, read and maintain

Memory management

High-level, object-oriented

Rapid Prototyping Tools

Nanjing University
Development of Python 7

Popularity of programming language(PyPL, Sept 2019 )

Nanjing University
Development of Python 8

TIOBE index

Nanjing University
Application of Python(1) 9

Python defines a WSGI standard application


interface to coordinate communication
between HTTP servers and Python-based Web
applications.

Web
development

Python provides various libraries with extremely


Big Data
convenient and powerful data processing and
statistical functions.

Nanjing University
10
Application of Python(2)

Based on the rich Python third-party library, it can


easily and efficiently implement all stages of AI
tasks.

Artificial
intelligence

It can be used in 3d scene production in computer Multi-


games. media

Nanjing University
11
Application Examples of Python

Nanjing University
12
Python mottos

禅。
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated. >>> import this
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

by Tim Peters

Nanjing University
13

Data processing Using


Python
THE FIRST PYTHON
PROGRAM

Nanjing University
14
Classical Hello World

myString = 'Hello, World!'

print(myString)

Nanjing University
15
How Python works (1)
Shell way

• Shell is an interactive
interpreter

• When a line of
command is input, the
interpreter will interpret
and run it to get the
corresponding result.

Nanjing University
How Python works (2) 16

File way

• Create a file with extension name py in the IDE


environment of Python.

• Run in the Shell using Python interpreter to get the


result.

Nanjing University
17
Classical Hello World

S ource File

>>> myString = 'Hello, World!'


>>> print(myString) # Filename: helloworld.py
Hello, World! myString = 'Hello, World!'
>>> myString print(myString)
'Hello, World!'

Nanjing University
18
Python Integrated Development Environment (IDE)

Python IDE
• In Mac OS & Linux
– $ python
– $ python3

• Python built-in IDE


– IDLE

• Other IDE
– PyCharm

Nanjing University
19
Installation of the package(plug-in)

Installation of plug-ins

• Install the Python packages


using pip, you can search in
the website https://pypi.org.
For example installing the
package of atx,

> pip install atx

Note: Run in the OS Shell please, not in the


Python Shell!

Nanjing University
20
Python development platform

• Anaconda integrated development


platform
– Download the installation package
(https://www.continuum.io/downloads)

– Install and use

① Choose the checkbox of“Add Anaconda to


my PATH environment variable”

② Spyder is recommended after installing the


Anaconda

Nanjing University
21
Python output: print function

• Python uses the print function


to output information
– print(variables)

– print(strings)
S ource

>>> myString = 'Hello, World!'


>>> print(myString)
Hello, World!

Nanjing University
22
Python input: the input() function

• The type returned by input() is a string type.

S ource

>>> price = input('input the stock price of Apple: ')


input the stock price of Apple: 109
>>> price
'109'
>>> type(price)
<class 'str'>
>>> price = int(input('input the stock price of Apple: '))
>>> price = eval(input('input the stock price of Apple: '))

Nanjing University
Python style (1) 23

Single line comment


#

S ource

>>> # comment No.1


>>> print('Hello, World!') # comment No.2
Hello, World!

Nanjing University
Python style (2) 24

long sentence
\
S ource S ource

>>> # long sentence


>>> # long sentence
>>> if signal == 'red' and\
>>> if signal == 'red' and car == 'moving':
car == 'moving':
car = 'stop'
car = 'stop'
elif signal == 'green' and car == 'stop':
elif signal == 'green' and\
car = 'moving'
car == 'stop':
car = 'moving'

Nanjing University
Python style (2) 25

Long sentence
• There are two situations in which \
the line can be continued without
the continuation markers: Source

– Multiple lines can be written in >>> # triple quotes


parentheses, brackets, and curly >>> print('''hi everybody,
braces
welcome to python’s MOOC course.
Here we can learn something about
– Strings included in triple quotes can
python. Good lucky!''')
also be written across lines.

Nanjing University
Python style (3) 26

Multiple statements in one line


;
S ource

>>> x = 'Today' ; y = 'is' ; z = 'Thursday' ; print(x, y, z)


Today is Thursday

S
ource

>>> x = 'Today'
>>> y = 'is'
>>> z = 'Thursday'
>>> print(x, y, z)
Today is Thursday
Nanjing University
Python style (4) 27

Indentation
02
S ource

Same indentation level >>> # Indentation


01 means the same level of
statement block in
>>> if signal == 'red' and car == 'moving':
Python. car = 'stop'
Increasing indentation
represents the beginning signal = 'yellow'
of a statement block.
elif signal == 'green' and car == 'stop':
Reducing indentation car = 'moving'
represents the end of a
statement block. signal = 'yellow'

03

Nanjing University
28

Processing Data Using


Python
PYTHON GRAMMAR
FOUNDATION

Nanjing University
Variables 29

Name of
Variables

S ource

Object >>> # variable


reference >>> p = 3.14159
>>> myString = 'is a mathematic circular constant'
>>> print(p, myString)
3.14159 is a mathematic circular constant

Nanjing University
Identifiers 30

• Identifiers are valid symbols in Python


language that could be used as names S ource

of variables or other objects


– The first character is a letter or an
>>> # Identifier
underline ( _ )
>>> PI = 3.14159
>>> pi = 'one word'
– The rest can be letters, underlines, and
>>> print(PI)
numbers
3.14159
– Case sensitive (PI and pi are different
>>> print(pi)
identifiers)
one word

Nanjing University
Keyword 31

• Keywords are key components of Python language and cannot be used as identifier for
other objects
– Key word in a language is basically a fixed set of characters

– Often appear with different color or fonts in IDE


>>> import keyword
>>> print(keyword.kwlist)

False None True and as assert break class continue


def del elif else except finally for from global
if import in is lambda nonlocal not or pass
raise return try while with yield

Nanjing University
Expressions 32

• Expressions are combinations of various types of data and operators.

Arithmetic
operators
Bit-wise
Power ** operators
plus or minus Comparison
sign + - operators
multiply or Logical
Not ~
divide */
and & less than <
operators
Exact division
or |
more than
// >
XOR ^ less than or
Reminder % equal <=
Left shift <<
Plus & minus
Right shift >>
more than or not
operator +- equal >=
equal == and
not equal !=
or

Nanjing University
Expressions 33

• Operators have precedence order.

• The expression must have a result.

S ource

• 2*PI*r is an expression.
>>> # expression • The result is assigned to
>>> PI = 3.14159 variable c_circ
>>> r = 2
>>> c_circ = 2 * PI * r
>>> print("The circle's circum is", c_circ)

Nanjing University
Assignment Statement 34

• When variable is first assigned with a value, it gets both the type and the value.
– Python is a dynamic, strongly-typed language

– No explicit declaration. The type depends on the “value”.

– Assignment is implemented in a "reference" way. =

S ource

>>> # Identifier add


PI >>> PI = 3.14159
3.14159 assignment
>>> pi = 'one word'
>>> print(PI) expression
pi 3.14159 like“x :== 3”
'one word' >>> print(pi) in Python 3.8
one word

Nanjing University
Assignment 35

S ource

>>> # Identifier
>>> PI = 3.14159
PI >>> pi = PI
>>> print(PI)
3.14159 3.14159
pi >>> print(pi)
3.14159
>>> p = 3
>>> q = 3
>>> p is q
True

Nanjing University
36
Assignment -Augmented assignment

Augmented
assignment
operator += -= *= /= %= **= <<= >>= &= ^= |=

S ource

• m %=5 equals to m = m % 5 >>> # Augmented assignment


>>> m = 18
• m **=2 equals to m = m ** 2
>>> m %= 5
>>> m
3
>>> m **= 2
>>> m
9

Nanjing University
37
Assignment -Chained assignment

S ource S ource

>>> # Chained assignment >>> # Chained assignment


>>> PI = pi = 3.14159
>>> PI = 3.14159
>>> PI
>>> pi = PI = PI * 2
3.14159
>>> pi
>>> pi
6.28318
3.14159

Nanjing University
38
Assignment- multiple assignments

• The forms of tuples appear in both sides of the equal sign.

S ource S ource

>>> # assignment >>> # assignment


>>> x = 1 >>> temp = 3.14159, 3
>>> y = 2 >>> PI, r = temp
>>> x, y >>> PI
(1, 2) 3.14159 Tuple packing
>>> x, y = y, x >>> r Sequence unpacking
>>> x, y 3
(2, 1) >>> (PI, r) = (3.14159, 3) # same as no round brackets

Nanjing University
Statement 39

• A line of logical codes that completely performs a task


– The assignment statement performs the assignment operation.
– The print () function calls statements and completes the output task.

S ource

>>> # statement
>>> PI = 3.14159
>>> r = 2
>>> c_circ = 2 * PI * r
>>> print("The circle's circum is", c_circ)

Nanjing University
40
Statements and expressions

Complete a task A specific component of the


task
e.g. printing a document
e.g. the specific content of the
document

Nanjing University
41

Processing Data Using


Python

DATA TYPE IN
PYTHON

Nanjing University
Data type 42

• There must be clear data types for program to assign accurate storage sizes to constants
and variables so as to perform precise or efficient operations.

1 0 0 1 0 0 1 1
1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1

0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0

1 0 0 1 1 0 1 1

Nanjing University
Python Standard data type 43

Nanjing University
Integer 44

• The integer and long integer type are not


S ource

strictly distinguished
>>> # integer
• In Python 2 , integer value affixed with L is >>> type(3)
<class 'int'>
interpreted as long integer

Nanjing University
Boolean type 45

• Subtype of integer

• Only two values: True and False S ource

• In essence, they are stored as integer 1 >>> # boolean


>>> x = True
and 0. >>> int(x)
1
>>> y = False
>>> int(y)
0

Nanjing University
Floating point type 46

• real number in mathematics


S ource
• can be expressed in the way of scientific
>>> # float
notation >>> 3.22
3.22
>>> 9.8e3
9800.0
>>> -4.78e-2
-0.0478
>>> type(-4.78e-2)
<class 'float'>

Nanjing University
Complex number 47

• j= −1, then j is imaginary


S ource

• Real part+ imaginary part= >>> # complex


complex number >>> 2.4+5.6j
(2.4+5.6j)
>>> type(2.4+5.6j)
• The imaginary part must have a j <class 'complex'>
>>> 3j
3j
>>> type(3j)
<class 'complex'>
>>> 5+0j
(5+0j)
>>> type(5+0j)
<class 'complex'>

Nanjing University
Complex number 48

• The complex number can be separated into real


part and imaginary part S ource

– complex.real
>>> # complex
– complex.imag
>>> x = 2.4+5.6j
• Conjugate of complex Numbers >>> x.imag
– complex.conjugate()
5.6
>>> x.real
2.4
>>> x.conjugate()
(2.4-5.6j)

Nanjing University
Sequence types 49

01 Strings
Strings are contents inside single quote,
double quotes, or triple quotes, which are
immutable

02 List
A strong, mutable type, defined
in the square brackets [].

Tuple
Similar to list,immutable
03
type,defined in the
parenthesis ()

Nanjing University
50
Representation of String

• Single quotes

• Double quotes S ource

>>> myString = 'Hello World!'


• Triple quotes
>>> print(myString)
Hello World!
>>> myString = "Hello World!"
>>> print(myString)
Hello World!
>>> myString = '''Hello World!'''
>>> print(myString)
Hello World!

Nanjing University
Mapping type-dictionary 51

• Defined by the curly braces {}

• Similar to the key-value pairs in


hash table
S ource

>>> # dictionary
>>> d ={'sine':'sin','cosine':'cos','PI':3.14159}
>>> d['sine']
'sin'

Nanjing University
52

Data Processing Using


Python
BASIC OPERATIONS IN
PYTHON

Nanjing University
Arithmetic operations 53

• The precedence of S ource


# % can be used for
arithmetic operators >>> # arithmetic negative numbers
– Power * *, positive and >>> pi = 3.14159 >>> -7 % 2
>>> r = 3 1
negative sign + -, >>> circum = 2 * pi * r # equal to -7-(-7//2)*2
multiply & divide by * >>> x = 1 >>> -7 // 2
>>> y = 2 -4
/, exact division / /,
>>> z = 3
remainder %, add and >>> result1 = x + 3/y -z % 2
subtract + - >>> result2 = (x + y**z*4)//5
>>> print(circum, result1, result2)
18.84954 1.5 6

Nanjing University
Comparison operations 54

• Numerical comparison: by value

• String comparison: the value of ASCII code


S ource

>>> # compare
>>> 2 == 2
S ource True
>>> 2.46 <= 8.33
>>> # compare
True
>>> 3 < 4 < 7 # same as 3 < 4 and 4 < 7
>>> 'abc' == 'xyz'
True
False
>>> 4 > 3 == 3 # same as 4 > 3 and 3 == 3
>>> 'abc' > 'xyz'
True
False
>>> 4 < 3 < 5 != 2 < 7
>>> 'abc' < 'xyz'
False
True

Nanjing University
Logical operations 55

• Logical operator S ource

precedence: >>> # logical


>>> x, y = 3.1415926536, -1024
– Not, and, or >>> x < 5.0
True
>>> not x < 5.0
False
>>> x < 5.0 or y > 2.718281828
True
>>> x < 5.0 and y > 2.718281828
False
>>> not x is y
True
>>> 3 < 4 < 7 # same as " 3 < 4 and 4 < 7"
True

Nanjing University
Character operator 56

• Raw string operator (r/ R) :


– For places where you don't want the escape
character to work S ource

>>> # r
• All strings are Unicode strings:
>>> f = open('c:\python\test.py','w')
– In Python 2.x, need to be converted to a Traceback (most recent call last):
Unicode string
File "<pyshell#12>", line 1, in <module>
f = open('c:\python\test.py','w')
S ource IOError: [Errno 22] invalid mode ('w') or
filename: 'c:\\python\test.py'
>>> # u in Python 2.x
>>> f = open(r'c:\python\test.py','w')
>>> print u'Hello\nWorld'
>>> f = open('c:\\python\\test.py','w')
hello
>>> f = open('c:/python/test.py','w')
World

Nanjing University
Mixed operation 57

Operator •Arithmetic > Bitwise > Comparison (~ is special ) > Logical


precedence •Arithmetic operators: ** > + - (plus and minus sign) > * / // % > + -
•Logical operators: not > and > or

Arithmetic operator S ource

1 >>> # mix
Bitwise operator
>>> 3 < 2 and 2 < 1 or 5 > 4
2 Comparison operator
True
>>> x + 3/y -z % 2 > 2
3 False
Logical operator
>>> 3-2 << 1
4 2
>>> 3-2 << 1 < 3
True
Nanjing University
58

Data Processing Using


python
FUNCTIONS, MODULES AND
PACKAGES OF PYTHON

Nanjing University
Functions(1) 59

• A function can be regarded as a mathematic function

• A piece of code that completes a specific task


– Absolute function abs(x)

– Type function type(x)

– Round-off function round(x)

Nanjing University
Functions(2) 60

• Built-in functions
– str() and type() are applicable to all standard types

Numerical built-in functions Useful functions

abs() bool() oct() dir() input()


round() int() hex() help() open()
divmod() ord() pow() len() range()
float() chr() complex()

Nanjing University
Built-in functions >>> dir(__builtins__) 61

Built-in Functions
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()

Nanjing University
Functions(3) 62

S ource S ource

>>> # round-off int >>> # ord


>>> int(35.4) >>> ord('3')
35 51
>>> int(35.5) >>> ord('a')
35 97
>>> int(35.8) >>> ord('\n')
35 10
>>> type(int(35.8)) >>> type(ord('A'))
<class 'int'> <class 'int'>

Nanjing University
Module(1) 63

• How to use non-built-in functions?

S ource
S ource

>>> # round-off floor


>>> # round-off floor
>>> import math
>>> floor(5.4)
>>> math.floor(-35.4)
Traceback (most recent call last):
-36
File "<pyshell#0>", line 1, in <module>
>>> math.floor(-35.5)
floor(5.4) -36
NameError: name 'floor' is not defined >>> math.floor(-35.8)
-36

Nanjing University
Module(2) 64

• A complete Python file is a module


– File: physical organization math.py

– Module: logical organization math


S ource
• Python usually uses "import module" to apply functions,
>>> # module
types in a given module to other code blocks. >>> import math
>>> math.pi
– The value of math.pi can be used directly without defining
3.141592653589793
by yourself.

Nanjing University
Module(3) 65

• Import multiple modules

• To import a specified module element to current module is to


import the specified name to the current scope

>>>import ModuleName
>>>import ModuleName1, ModuleName2, …
>>>from Module1 import ModuleElement

Nanjing University
package 66

• A hierarchical file directory structure AAA/


• Defines the execution environment __init__.py
bbb.py
for Python application consisting of
CCC/
modules and sub-packages
__init__.py
>>> import AAA.CCC.c1 c1.py
>>> AAA.CCC.c1.func1(123) c2.py
DDD/
__init__.py
>>> from AAA.CCC.c1 import func1 d1.py
>>> func1(123) EEE/

Nanjing University
library 67

• A library is a collection
of modules with
related functions decimal
• One feature of Python array

is that it has a powerful


random
standard library, as well
as third-party libraries
and custom modules
Numeric-related standard libraries

Nanjing University
Summary 68

Nanjing University

You might also like