=============================================================================
用import 导入模块 要安装 “模块。函数” 的格式来使用函数
例如:
>>>import math
>>>math.floor(32.9)
32
在使用“from 模块 import 函数” 这种形式的import命令后 就可以直接使用函数
>>>from math import floor
>>>floor(32.9)
32
=============================================================================
1. 长字符串 需要跨越多行 可以用 ''' 三个引号代替普通引号
例如:
print ''' iqoashdas
jasldasd
kasdasdpo
zcasd
hoellasd
lkasdsad'''
iqoashdas
jasldasd
kasdasdpo
zcasd
hoellasd
lkasdsad
2. 普通字符也可以跨行,如果一行之中最后一个字符时反斜线,那么换行符本身就“转义”了
例如:
>>>print "hello.\
world"
hello.world
3.原始字符串
例如:
>>>print "hello \nworld"
hello
world
>>>path = 'C:\nshowhere'
>>>path
C:
showhere
暂时解决办法 使用反斜线对其本身进行转义
>>>path = 'C:\\nshowhere'
>>>path
C:\nshowhere
但是对于长字符串或者比较长的路径就需要很多反斜线,比较麻烦,这个时候可以使用原始字符串
>>>print r'C:\nshowhere'
C:\nshowhere
>>>print r'Let\'s go!'
Let\'s go!
原始字符串以r开头,可以在字符串中放入任何字符
注意:不能在原始字符串结尾输入反斜线,如果最后一个字符是反斜线,python就不清楚是否应该结束字符串
例如:
>>>print r'this is a pig\'
SyntaxError: EOL while scanning string literal
如果希望最后一个字符以\结尾,可以用下面的办法
>>>print r'C:\nasdasd asd' '\\'
C:\nasdasd asd\
3. unicode 字符串
>>>u'hello world'
u'hello world'
-----------------------------------------------------------------
分片 步长
>>> numbers=[1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:1]
[1]
>>> numbers[7:10]
[8, 9, 10]
>>> numbers[-3:-1]
[8, 9]
>>> numbers[-4:-1]
[7, 8, 9]
>>> numbers[-3:0]
[]
>>> numbers[-3:]
[8, 9, 10]
>>> numbers[:3]
[1, 2, 3]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
步长
>>> numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> numbers[3:6:3]
[4]
>>> numbers[::4] -----每四个元素中第一个提取出来
[1, 5, 9]
----步长不能为0(那样不会执行的) 可以为负数 即:从右往左提取元素
>>> numbers[0:10:-1]
[]
>>> numbers[10:0:-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
>>> numbers[::-2]
[10, 8, 6, 4, 2]
>>> numbers[5::-2]
[6, 4, 2]
>>> numbers[:5:-2]
[10, 8]
===========================================================
乘法
>>> 'qiaoc' * 5
'qiaocqiaocqiaocqiaocqiaoc'
>>> [33]*8
[33, 33, 33, 33, 33, 33, 33, 33]
None。空列表和初始化
>>> sequence = [None]*10
>>> sequence
[None, None, None, None, None, None, None, None, None, None]
序列相加
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'hello ' + 'world'
'hello world'
>>> [1,2,3] + 'hello'
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
[1,2,3] + 'hello'
TypeError: can only concatenate list (not "str") to list
注意:2种相同类型的序列才能进行连接操作
============================================================
成员资格
>>> permisionns='rw'
>>> 'r' in permisionns
True
>>> 'x' in permisionns
False
>>> users = ['qiao','hehe','haha']
>>> raw_input('Enter your user name:') in users
Enter your user name:qiao
True
>>> subject = '$$$ get rich now! $$$'
>>> '$$$' in subject -------------------检查某个字符是否存在于一个字符串中
True
============================================================
数组长度,最小值和最大值
>>> number=[100,245,22]
>>> len(number)
3
>>> max(number)
245
>>> min(number)
22
>>> max(2,3)
3
>>> min(9.3,2.5)
2.5
>>> min(9,3,2,5)
2
注意:
>>> len(2,3,4)
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
len(2,3,4)
TypeError: len() takes exactly one argument (3 given)
======================================================
list函数
>>> list("hello")
['h', 'e', 'l', 'l', 'o']
注意:list函数适用于所有类型的序列,而不只是字符串
======================================================
基本列表操作
1.改变列表:
>>> x=[1,1,1]
>>> x[1]=2
>>> x
[1, 2, 1]
2.删除列表:
>>> x
[1, 2, 1]
>>> del x[2]
>>> x
[1, 2]
3.分片赋值
>>> name=list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name[2:]=list('ar')
>>> name
['P', 'e', 'a', 'r']
>>> name[1:]=list('python')
>>> name
['P', 'p', 'y', 't', 'h', 'o', 'n']
分片赋值可以插入新的元素,也可以删除元素
>>> numbers=[1,5]
>>> numbers[1:1]=[2,3,4]
>>> numbers
[1, 2, 3, 4, 5]
>>> numbers[1:4]=[]
>>> numbers
[1, 5]
==================================================================