我爱编程

Data Processing Using Python | 用

2017-07-13  本文已影响0人  Percy大大

Welcome | 欢迎学习用Python玩转数据

图形处理,WEB编程,多媒体应用,PYMO引擎

获取股票数据,描述和处理数学问题,解决专业问题(基因序列),搜集互联网信息(文本信息)

1.1 Walk into Python | 走进Python

Python 的应用

1.1.1 Introduction to Python | Python 简介

1.1.2 The First Python Program | 第一个Python程序

经典的Hello World

myString = 'Hello, world'
print myString

Python的运行方式

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

执行环境:Python(x, y)

Python输出:print语句

>>> myString = 'Hello, World!'
>>> print myString
Hello, World!

Python输入:raw_input()语句

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

Python 风格

>>> # comment No.1
>>> print 'hello world'    # comment No.2
hello world!
>>> # long sentence
>>> if (signal == 'red') and \
    (car == 'moving'):
        car = 'stop'
    elif (signal == 'green') and \
    (car == 'stop'):
        car = 'moving'

same as:

>>> long sentence
>>> if (signal == 'red') and (car == 'moving'):
        car = 'stop'
    elif (signal == 'green') and (car == 'stop'):
        car = 'moving'
>>> # triple quotes
>>> print '''hi everybody, 
welcome to python's MOOC course. 
Here we can learn something about 
python. Good lucky!'''
>>> x = 'Today'; y = 'is'; z = 'Thursday'; print (x, y, z)
('Today', 'is', 'Thursday')
>>> x = 'Today'
>>> y = 'is'
>>> z = 'Thursday'
>>> print (x, y, z)
('Today', 'is', 'Thursday')
>>> # Indentation
>>> if (signal == 'red') and (car == 'moving'):
        car = 'stop'
        signal = 'yellow'
    elif (signal == 'green') and (car == 'stop'):
        car = 'moving'
        signal = 'yellow'

1.1.3 Basics of Python Syntax | Python 语法基础

变量

>>> # variable
>>> p = 3.14159
>>> myString = 'is a mathematic circular constant'
>>> print p, myString
3.14159 is a mathematic circular constant

标识符

>>> # Identifier
>>> PI = 3.14159
>>> pi = 'one word'
>>> print PI
3.14159
>>> print pi
one word

关键字

and as assert break class continue def del
elif else except exec finally for from global
if import in is lambda not or pass
print raise return try while with yield

表达式

>>> # expression
>>> PI = 3.14159
>>> r = 2
>>> c_circ = 2 * PI * r    # 2*PI*r是表达式,运算结果赋值给变量c_circ
>>> print "The circle's circum: %f" % (c_circ)

赋值

>>> # Identifier
>>> PI = 3.14159
>>> pi = 'one word'
>>> print PI
3.14159
>>> print pi
one word
>>> # Identifier
>>> PI = 3.14159
>>> pi = PI
>>> print PI
3.14159
>>> print pi
3.14159

赋值 增量赋值

>>> # assignment
>>> m = 18
>>> m %= 5
>>> m
3
>>> m **= 2
>>> m
9
>>> # assignment
>>> PI = 3.14159
>>> pi = PI = PI * 2
>>> pi
6.28318

赋值 多元赋值

>>> # assignment
>>> x = 1
>>> y = 2
>>> x, y
(1, 2)
>>> x, y = y, x
>>> x, y
(2, 1)
>>> # assignment
>>> PI, r = 3.14159, 3
>>> PI
3.14159
>>> r
3
>>> (PI, r) = (3.14159, 3)    # same as no round brackets

语句

>>> # sentence
>>> PI = 3.14159
>>> r = 2
>>> c_circ = 2 * PI * r
>>> print "The circle's circum: %f" %(c_circ)

语句和表达式

1.1.4 Data Types of Python | Python 数据类型

(长)整型,浮点型,复数型,布尔型,字符串,列表,元组,字典

(长)整型

>>> # integer
>>> type(3L)
<type 'long'>
>>> type(3)
<type 'int'>

布尔型

>>> # boolean
>>> x = True
>>> int(x)
1
>>> y = False
>>> int(y)
0

浮点型

>>> # float
>>> 3.22
3.22
>>> 9.8e3
9800.0
>>> -4.78e-2
-0.0478
>>> type(-4.78e-2)
<type 'float'>

复数型

>>> # complex
>>> 2.4 + 5.6j
(2.4 + 5.6j)
>>> type(2.4 + 5.6j)
<type 'complex'>
>>> 3j
3j
>>> type(3j)
<type 'complex'>
>>> 5 + 0j
(5 + 0j)
>>> type(5 + 0j)
<type 'complex'>
>>> # complex
>>> x = 2.4 + 5.6j
>>> x.imag
5.6
>>> x.real
2.4
>>>x.conjugate()
(2.4-5.6j)

序列类型

字符串的表示

>>> myString = 'Hello World!'
>>> print myString
Hello World!
>>> myString = "Hello World!"
>>> print myString
Hello World!
>>> myString = '''Hello World!'''
>>> print myString
Hello World!

映射类型 字典

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

1.1.5 Basic Operations of Python | Python 基本运算

算术运算

>>> # arithmetic
>>> pi = 3.14159
>>> r = 3
>>> circum = 2 * pi * r
>>> x = 1
>>> y = 2
>>> z = 3
>>> result1 = x + 3/y - z % 2
>>> result2 = (x + y ** z * 4) // 5
>>> print circum, result1, result2
18.84954 1 6

比较运算

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

逻辑运算

>>> # logical
>>> x, y = 3.1415926536, -1024
>>> 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

字符运算符

>>> # u
>>> print u'Hello\nWorld'
hello
World
>>> # r
>>> f = open('c:\python\test.py', 'w')
Traceback(most recent call last):
  File "<pyshell#12>", line 1, in <module>
    f = open('c:\python\test.py', 'w')
IOError: [Errno 22] invalid mode ('w') or
filename: 'c:\\python\test.py'
>>> f = open(r'c:\python\test.py', 'w')
>>> f = open('c:\\python\\test.py', 'w')

综合运算

>>> # mix
>>> 3 < 2 and 2 < 1 or 5 > 4
True
>>> x + 3/y - z % 2 > 2
False
>>> 3 - 2 << 1
2
>>> 3 - 2 << 1 < 3
True

1.1.6 Functions, Modules and Packages of Python | Python 的函数,模块和包

函数

abs() bool() oct()
coerce() int() hex()
divmod() long() ord()
pow() float() chr()
round() complex()
dir() raw_input()
help() open()
len() range()

模块

>>> # round-off floor
>>> floor(5.4)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    floor(5.4)
NameError: name 'floor' is not defined
>>> # round-off floor
>>> import math
>>> math.floor(-35.4)
-36.0
>>> math.floor(-35.5)
-36.0
>>> math.floor(-35.8)
-36.0
>>> # module
>>> import math
>>> math.pi
3.141592653589793
>>> import ModuleName
>>> import ModuleName1, ModuleName2, ...
>>> from Module1 import ModuleElement

包(package)

>>> import AAA.CCC.c1
>>> AAA.CCC.c1.func1(123)
>>> from AAA.CCC.c1 import func1
>>> func1(123)
AAA/
  __init__.py
  bbb.py
  CCC/
    __init__.py
    c1.py
    c2.py
  DDD/
    __init__.py
    d1.py
  EEE/
    ...

库(library)

上一篇 下一篇

猜你喜欢

热点阅读