Python3 tutorial day1

An Informal Introduction to Python

https://docs.python.org/3/tutorial/index.html

Numbers

>>> 17/3
5.666666666666667
>>> 5 ** 2
25
>>> 5**2
25
>>> 17//3
5

 

Strings

'\'用来转义

字符串前面加‘r’表示用原始字符串

字符串前面加‘u’表示用unicode字符串

>>> print('C:\some\name')
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name
>>>

用三个引号可跨多行打印

using triple-quotes: """...""" or '''...'''.

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")


Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to

 字符串可以用‘+’拼接 用‘*’重复

>>> 3*'ab' + 'ha'
'abababha'
>>>

 

相邻的多个字符串自动拼接

>>> print('Py' 'thon')
Python

 

字符串可以通过下标索引到

>>> word = 'Python'
>>> word[0]
'P'

Indices may also be negative numbers, to start counting from the right:

 negative indices start from -1.

>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
>>> word[-6]
'P'

In addition to indexing, slicing is also supported.

>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'

Note how the start is always included, and the end always excluded. This makes sure that s[:i] s[i:] is always equal to s:

>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'

Python strings cannot be changed — they are immutable.


内置函数len() 返回字符串的长度、

>>> len(word)
6

str内置方法

capitalize()

count(‘x’) 返回字符串中包含‘x’的个数

endswith()

find()返回位置

format()

>>> "The sum of 1 + 2 is {0} {1}".format(1+2,'ok')
'The sum of 1 + 2 is 3 ok'

index()

isalnum()

isnumric()

isalpha()

isdecimal()

join()

>>> '   spacious   '.lstrip()
'spacious   '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'


partition()
replace()
split()
splitlines()
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']


strip()
maketrans()
translate()
zfill()
format()

String 去掉回车/换行/空格''.join(s.split())


LIst

Lists also supports operations like concatenation:

>>> squares[:]
[1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

list 可以更改

squares[0] = 10

list 可以追加

>>> squares[0] = 123
>>> squares[:]
[123, 4, 9, 16, 25]
>>> squares.append(216)
>>> squares
[123, 4, 9, 16, 25, 216]
>>>

 

list 支持赋值

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = []
>>> letters
[]

支持嵌套list

>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']


More Control Flow Tools

if elif else

for x in words:

for i in range(5)

for循环后可以加else

while 也可以配合else

range(0, 10, 3)
   0, 3, 6, 9

break /continue

The pass statement does nothing. 

只是语法的需要
class a:
pass


def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return 

方法参数中通过* 和** 指定接收元组和列表


>>>
def concat(*args, sep="/"):
...    return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'

Documentation String
__doc__
Annotation
__anootations__
 

posted on 2014-10-21 12:03  ukouryou  阅读(180)  评论(0编辑  收藏  举报

导航