前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python教程: Python 介绍

Python教程: Python 介绍

作者头像
py3study
发布2020-01-07 15:05:49
2.5K0
发布2020-01-07 15:05:49
举报
文章被收录于专栏:python3python3

Python教程: Python 介绍

1、Python 命令行解释提示符下

输入control + p 命令提示符向上查找

输入control + n 命令提示符向下查找

2、在交互模式中,最后打印的表达式的值被赋予给变量_

3、在字符串第一个引号前添加r字符,可以避免通过\转义字符

print r'C:\some\name'

4、使用三个引号包含的字符串可以跨越多行

“””…””"

‘’’…’''

注:字符串的首行将自动包含行的结尾换行符,通过在行首添加\可以避免

print """\

Usage: thingy [OPTIONS]

     -h                        Display this usage message

     -H hostname               Hostname to connect to

"""

5、字符串连接 (+, *)

'abc' + 'def'# 字符串连接,可以连接变量

'abc' * 3 # 字符串重复

'Py' 'thon'# 两个字符串字面值自动连接,不包括变量或表达式

# 字符串连接

>>> text = ('Put several strings within parentheses '

            'to have them joined together.')

>>> text

'Put several strings within parentheses to have them joined together.'

6、字符串索引

字符串的下标从0开始索引,字符串是没有分割字符的类型,一个字符是一个简单的长度为1字符串

>>> word = 'Python'

>>> word[0]  # character in position 0

'P'

7、负数从字符串右侧开始计数

>>> word[-1]  # last character

'n'

注:-0相当于0,负数从-1开始

8、字符串支持切片,索引获取单个字符,切片获取子字符串

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

'Py'

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

'tho'

注:切片的开始参数总是被包含,结尾总是被排除的。

9、字符串切片默认值,第一个索引省去默认为0,第二个索引省去默认为切片的长度;

>>> word[:2]  # character from the beginning to position 2 (excluded)

'Py'

>>> word[4:]  # characters from position 4 (included) to the end

'on'

>>> word[-2:] # characters from the second-last (included) to the end

'on'

10、最简单理解字符串切片原理是记住字符之间的位置,左侧的字符是0,右侧的字符是n就是索引n:

 +---+---+---+---+---+---+

 | P | y | t | h | o | n |

 +---+---+---+---+---+---+

 0   1   2   3   4   5   6

-6  -5  -4  -3  -2  -1

11、使用较大的索引将会出现如下错误

>>> word[42]  # the word only has 7 characters

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: string index out of range

12、Python字符串是不可以被修改的,给字符串索引位置赋值将会出现如下错误!

>>> word[0] = 'J'

  ...

TypeError: 'str' object does not support item assignment

>>> word[2:] = 'py'

  ...

TypeError: 'str' object does not support item assignment

# 如果需要,你可以创建一个新的字符串。

13、Python 2.0以后引入了新的存储文本的数据类型,Unicode对象。他可以很好的存储、维护Unicode数据并提供自动转换。

Unicode常被用来解决国际化。

14、Unicode字符串的建立

>>> u'Hello World !'

u'Hello World !'

# 字符串前面的小写的u是被支持用来创建Unicode字符的,如果你想使用特殊字符,请参考Unicode-Escape。例如:

>>> u'Hello\u0020World !'

u'Hello World !'

注:\u0020表示Unicode字符0x0020(空格)

15、原始模式字符串,字符串引号前添加'ur'前缀,Python使用Raw-Unicode-Escape编码。如果使用了不对等条件的反斜线将退出\uXXXX转换。

>>>

>>> ur'Hello\u0020World !'

u'Hello World !'

>>> ur'Hello\\u0020World !'

u'Hello\\\\u0020World !'

主:原始模式是非常实用的,例如:正则表达式需要原始模式

除了Python标准的编码,Python提供了完整的方式从一个已知的编码建立Unicode编码。

16、内建函数unicode()提供访问所有已注册的Unicode编码(C0ders and DECoders).一些已知的编码Latin-1, ASCII, UTF-8, and UTF-16能被转换。

字符2个变量长度的编码存储Unicode字符在一个或者更多的字节。默认编码通常被设置为ASCII, 通过传递字符区间在0-127,传递其他字符将被拒绝并产生一

个错误,当一个Unicode字符打印、写到一个文件、或通过str()转换,转换将空间使用默认编码。

>>>

>>> u"abc"

u'abc'

>>> str(u"abc")

'abc'

>>> u"ü"

u'\xe4\xf6\xfc'

>>> str(u"ü")

Traceback (most recent call last):

  File "<stdin>", line 1, in ?

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

通过使用指定编码可以转换一个Unicode字符串到一个8位的字符串,Unicode提供一个encode()方法带一个编码名字的参数,小写的字母编码是被推荐的。

>>>

>>> u"ü".encode('utf-8')

'\xc3\xa4\xc3\xb6\xc3\xbc'

17、如果你有一个指定编码的数据想要产生一个相等的Unicode字符串,你可以使用unicode()方法,第二个参数带上编码名称。

>>>

>>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')

u'\xe4\xf6\xfc'

18、列表

列表通过在尖括号之间使用逗号分隔值,列表可以包含不同类型的值,但同行是相同的。

>>> squares = [1, 4, 9, 16, 25]

>>> squares

[1, 4, 9, 16, 25]

19、列表和字符串一样属于序列类型,同样可以被索引和切片

>>> squares[0]  # indexing returns the item

1

>>> squares[-1]

25

>>> squares[-3:]  # slicing returns a new list

[9, 16, 25]

20、返回一个新列表的拷贝

>>>

>>> squares[:]

[1, 4, 9, 16, 25]

21、列表支持操作符

>>> squares + [36, 49, 64, 81, 100]

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

22、不像字符串,列表可以改变自己的内容

>>> cubes = [1, 8, 27, 65, 125]  # something's wrong here

>>> 4 ** 3  # the cube of 4 is 64, not 65!

64

>>> cubes[3] = 64  # replace the wrong value

>>> cubes

[1, 8, 27, 64, 125]

23、列表可以使用append()方法在结尾添加元素

>>> cubes.append(216)  # add the cube of 6

>>> cubes.append(7 ** 3)  # and the cube of 7

>>> cubes

[1, 8, 27, 64, 125, 216, 343]

24、列表的切片可以被赋值,甚至可以改变列表的长度和清空列表

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

>>> letters

['a', 'b', 'c', 'd', 'e', 'f', 'g']

>>> # replace some values

>>> letters[2:5] = ['C', 'D', 'E']

>>> letters

['a', 'b', 'C', 'D', 'E', 'f', 'g']

>>> # now remove them

>>> letters[2:5] = []

>>> letters

['a', 'b', 'f', 'g']

>>> # clear the list by replacing all the elements with an empty list

>>> letters[:] = []

>>> letters

[]

25、内建的len()函数可以被应用到列表

>>>

>>> letters = ['a', 'b', 'c', 'd']

>>> len(letters)

4

26、列表可以嵌套列表(建立一个列表包含其他列表)

>>>

>>> a = ['a', 'b', 'c']

>>> n = [1, 2, 3]

>>> x = [a, n]

>>> x

[['a', 'b', 'c'], [1, 2, 3]]

>>> x[0]

['a', 'b', 'c']

>>> x[0][1]

'b'

# /doc/tutorial/introduction.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档