Walk Into Python: Data Processing Using Python
Walk Into Python: 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
Nanjing University
4
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
Nanjing University
6
Features of Python
Memory management
High-level, object-oriented
Nanjing University
Development of Python 7
Nanjing University
Development of Python 8
TIOBE index
Nanjing University
Application of Python(1) 9
Web
development
Nanjing University
10
Application of Python(2)
Artificial
intelligence
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
Nanjing University
14
Classical 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
Nanjing University
17
Classical Hello World
S ource File
Nanjing University
18
Python Integrated Development Environment (IDE)
Python IDE
• In Mac OS & Linux
– $ python
– $ python3
• Other IDE
– PyCharm
Nanjing University
19
Installation of the package(plug-in)
Installation of plug-ins
Nanjing University
20
Python development platform
Nanjing University
21
Python output: print function
– print(strings)
S ource
Nanjing University
22
Python input: the input() function
S ource
Nanjing University
Python style (1) 23
S ource
Nanjing University
Python style (2) 24
long sentence
\
S ource S ource
Nanjing University
Python style (2) 25
Long sentence
• There are two situations in which \
the line can be continued without
the continuation markers: Source
Nanjing University
Python style (3) 26
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
03
Nanjing University
28
Nanjing University
Variables 29
Name of
Variables
S ource
Nanjing University
Identifiers 30
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
Nanjing University
Expressions 32
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
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
S ource
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
Nanjing University
37
Assignment -Chained assignment
S ource S ource
Nanjing University
38
Assignment- multiple assignments
S ource S ource
Nanjing University
Statement 39
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
Nanjing University
41
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
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
Nanjing University
Floating point type 46
Nanjing University
Complex number 47
Nanjing University
Complex number 48
– 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
Nanjing University
Mapping type-dictionary 51
>>> # dictionary
>>> d ={'sine':'sin','cosine':'cos','PI':3.14159}
>>> d['sine']
'sin'
Nanjing University
52
Nanjing University
Arithmetic operations 53
Nanjing University
Comparison operations 54
>>> # 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
Nanjing University
Character operator 56
>>> # 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
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
Nanjing University
Functions(1) 59
Nanjing University
Functions(2) 60
• Built-in functions
– str() and type() are applicable to all standard types
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
Nanjing University
Module(1) 63
S ource
S ource
Nanjing University
Module(2) 64
Nanjing University
Module(3) 65
>>>import ModuleName
>>>import ModuleName1, ModuleName2, …
>>>from Module1 import ModuleElement
Nanjing University
package 66
Nanjing University
library 67
• A library is a collection
of modules with
related functions decimal
• One feature of Python array
Nanjing University
Summary 68
Nanjing University