本文是笔者在学习MOOC课程《Python语言基础与应用》 (北京大学-陈斌)中根据上机课时的要求写下在代码
课程总链接:
本节课链接
数值基本运算: 33和7
+, -, *, /, //, %, **
hex(), oct(), bin()
1 Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64bit (AMD64)] :: Anaconda, Inc. on win322 Type "help", "copyright", "credits" or "license" formore information.3 >>> 33+7
4 40
5 >>> 33-7
6 26
7 >>> 33*7
8 231
9 >>> 33/7
10 4.714285714285714
11 >>> 33//7
12 4
13 >>> 33%7
14 5
15 >>> 33**7
16 42618442977
17 >>> 7**33
18 7730993719707444524137094407
19 >>> 33**33
20 129110040087761027839616029934664535539337183380513
21 >>> hex(33)22 '0x21'
23 >>> hex(7)24 '0x7'
25 >>> oct(7)26 '0o7'
27 >>> oct(33)28 '0o41'
29 >>> bin(33)30 '0b100001'
31 >>> bin(7)32 '0b111'
类型转换
1, 0, 'abc', None, 1.2, False, ''
str(), bool(), int(), float()
is None, ==, !=
1 >>> str(1)2 '1'
3 >>>str(0)4 '0'
5 >>> bool(1)6 True7 >>>bool(0)8 False9 >>> bool('abc')10 True11 >>> int('abc')12 Traceback (most recent call last):13 File "", line 1, in
14 ValueError: invalid literal for int() with base 10: 'abc'
15 >>> int('a')16 Traceback (most recent call last):17 File "", line 1, in
18 ValueError: invalid literal for int() with base 10: 'a'
19 >>> float('abc')20 Traceback (most recent call last):21 File "", line 1, in
22 ValueError: could not convert string to float: 'abc'
23 >>> float(1)24 1.0
25 >>>str(None)26 'None'
27 >>>int(None)28 Traceback (most recent call last):29 File "", line 1, in
30 TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
31 >>> int('None')32 Traceback (most recent call last):33 File "", line 1, in
34 ValueError: invalid literal for int() with base 10: 'None'
35 >>> int(1.2)36 1
37 >>>int(False)38 039 >>>int(True)40 1
41 >>> float('')42 Traceback (most recent call last):43 File "", line 1, in
44 ValueError: could notconvert string to float:45 >>> bool('')46 False47 >>> 1 isNone48 False49 >>> 0 isNone50 False51 >>> '' isNone52 False53 >>> 1==1.2
54 False55 >>> False isNone56 False57 >>> True isNone58 False
字符串基本操作
+, *, len(), [], in
ord(), chr()
含有中文的字符串
1 >>> a='Congratulations'
2 >>> b='misunderstandings'
3 >>> a+b4 'Congratulationsmisunderstandings'
5 >>> a+' '+b6 'Congratulations misunderstandings'
7 >>>len(a)8 15
9 >>>len(b)10 17
11 >>> c ina12 Traceback (most recent call last):13 File "", line 1, in
14 NameError: name 'c' is notdefined15 >>> 'c' ina16 False17 >>> 's' inb18 True19 >>> 'C' ina20 True21 >>>[a]22 ['Congratulations']23 >>> ord('a')24 97
25 >>> chr(86)26 'V'
27 >>>ord(a)28 Traceback (most recent call last):29 File "", line 1, in
30 TypeError: ord() expected a character, but string of length 15found31 >>> c='你好'
32 >>> d='国'
33 >>>len(c)34 2
35 >>>len(d)36 1
37 >>>ord(d)38 22269
39 >>> chr(83475)40 '𔘓'
41 >>> chr(22270)42 '图'
43 >>> chr(24343)44 '弗'
字符串高级操作
s='abcdefg12345'
切片:获得defg12,获得fg12345,获得54321,
获得aceg2
>>> s='abcdefg12345'
>>> s1=s[3:9]>>>s1'defg12'
>>> s2=s[5:]>>>s2'fg12345'
>>> s3=s[-1:-6]>>>s3''
>>> s3=s[-1:-6:1]>>>s3''
>>>s'abcdefg12345'
>>> s3=s[-1:]>>>s3'5'
>>> s4=s[0:9:2]>>>s4'aceg2'
>>> s3=s[::-1]>>>s3'54321gfedcba'
>>> s3=s[-1:-6]>>>s3''
>>> s3=s[-1:-6:]>>>s3''
>>> s3=s[-5]>>>s3'1'
>>> s3=s[-5::]>>>s3'12345'
>>> s3=s[-6:-1:-1]>>>s3''
>>> s3.index(5)
Traceback (most recent call last):
File"", line 1, in TypeError: must be str,notint>>> s3.index('5')
Traceback (most recent call last):
File"", line 1, in ValueError: substringnotfound>>> s.index('5')11
>>> s.index('1')7
>>> s3=s[11:7:-1]>>>s3'5432'
>>> s3=s[11:8:-1]>>>s3'543'
>>> s3=s[11:6:-1]>>>s3'54321'
>>>
>>> s3=s[-1:-6:-1]>>>s3'54321'
>>> s3=s[-1:6:-1]>>>s3'54321'
通常一个切片操作要提供三个参数 [start_index: stop_index: step]
start_index是切片的起始位置
stop_index是切片的结束位置(不包括)
step可以不提供,默认值是1,步长值不能为0,不然会报错ValueError。
当 step 是正数时,以list[start_index]元素位置开始, step做为步长到list[stop_index]元素位置(不包括)为止,从左向右截取,
start_index和stop_index不论是正数还是负数索引还是混用都可以,但是要保证 list[stop_index]元素的【逻辑】位置
必须在list[start_index]元素的【逻辑】位置右边,否则取不出元素。
比如下面的几个例子都是合法的:
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[1:5]
[1, 2, 3, 4]
>>> alist[1:-1]
[1, 2, 3, 4, 5, 6, 7, 8]
>>> alist[-8:6]
[2, 3, 4, 5]
当 step 是负数时,以list[start_index]元素位置开始, step做为步长到list[stop_index]元素位置(不包括)为止,从右向左截取,
start_index和stop_index不论是正数还是负数索引还是混用都可以,但是要保证 list[stop_index]元素的【逻辑】位置
必须在list[start_index]元素的【逻辑】位置左边,否则取不出元素。详见链接。
t='Mike and Tom'
split拆分
upper/lower/swapcase修改大小写
ljust/center/rjust排版30位宽度左中右对齐
replace将Mike替换为Jerry
1 >>> t='Mike and Tom'
2 >>> list1=t.split(' ')3 >>>list14 ['Mike', 'and', 'Tom']5 >>> tuple1=tuple(list1)6 >>>tuple17 ('Mike', 'and', 'Tom')8 >>>t.upper()9 'MIKE AND TOM'
10 >>>t.lower()11 'mike and tom'
12 >>>t.swapcase()13 'mIKE AND tOM'
14 >>>t.ljust()15 Traceback (most recent call last):16 File "", line 1, in
17 TypeError: ljust() takes at least 1argument (0 given)18 >>> t.ljust(1)19 'Mike and Tom'
20 >>> t.ljust(3)21 'Mike and Tom'
22 >>> t.ljust(50,'*')23 'Mike and Tom**************************************'
24 >>> t.rjust(50,'*')25 '**************************************Mike and Tom'
26 >>> t.center(30,'*')27 '*********Mike and Tom*********'
28 >>> t.replace('Mike','Jerry')29 'Jerry and Tom'
30 >>>t31 'Mike and Tom'
swapcase() 方法用于对字符串的大小写字母进行转换。
Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
语法
ljust()方法语法:
str.ljust(width[, fillchar])
参数
width -- 指定字符串长度。
fillchar -- 填充字符,默认为空格。
返回值
返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
实例
以下实例展示了ljust()的使用方法:
str = "this is string example....wow!!!";
print str.ljust(50, '0');
以上实例输出结果如下:
this is string example....wow!!!000000000000000000