Python语言基础与应用 (P16)上机练习:基本数据类型,,本文是笔者在学习MO


本文是笔者在学习MOOC课程《Python语言基础与应用》 (北京大学-陈斌)中根据上机课时的要求写下在代码

课程总链接:

中国大学MOOC

B站

本节课链接

数值基本运算: 33和7
+, -, *, /, //, %, **
hex(), oct(), bin()

 1 Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32 2 Type "help", "copyright", "credits" or "license" for more information. 3 >>> 33+7 4 40 5 >>> 33-7 6 26 7 >>> 33*7 8 231 9 >>> 33/710 4.71428571428571411 >>> 33//712 413 >>> 33%714 515 >>> 33**716 4261844297717 >>> 7**3318 773099371970744452413709440719 >>> 33**3320 12911004008776102783961602993466453553933718338051321 >>> 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 True 7 >>> bool(0) 8 False 9 >>> bool(‘abc‘)10 True11 >>> int(‘abc‘)12 Traceback (most recent call last):13   File "<stdin>", line 1, in <module>14 ValueError: invalid literal for int() with base 10: ‘abc‘15 >>> int(‘a‘)16 Traceback (most recent call last):17   File "<stdin>", line 1, in <module>18 ValueError: invalid literal for int() with base 10: ‘a‘19 >>> float(‘abc‘)20 Traceback (most recent call last):21   File "<stdin>", line 1, in <module>22 ValueError: could not convert string to float: ‘abc‘23 >>> float(1)24 1.025 >>> str(None)26 ‘None‘27 >>> int(None)28 Traceback (most recent call last):29   File "<stdin>", line 1, in <module>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 "<stdin>", line 1, in <module>34 ValueError: invalid literal for int() with base 10: ‘None‘35 >>> int(1.2)36 137 >>> int(False)38 039 >>> int(True)40 141 >>> float(‘‘)42 Traceback (most recent call last):43   File "<stdin>", line 1, in <module>44 ValueError: could not convert string to float:45 >>> bool(‘‘)46 False47 >>> 1 is None48 False49 >>> 0 is None50 False51 >>> ‘‘ is None52 False53 >>> 1==1.254 False55 >>> False is None56 False57 >>> True is None58 False

字符串基本操作
+, *, len(), [], in
ord(), chr()
含有中文的字符串

 1 >>> a=‘Congratulations‘ 2 >>> b=‘misunderstandings‘ 3 >>> a+b 4 ‘Congratulationsmisunderstandings‘ 5 >>> a+‘ ‘+b 6 ‘Congratulations misunderstandings‘ 7 >>> len(a) 8 15 9 >>> len(b)10 1711 >>> c in a12 Traceback (most recent call last):13   File "<stdin>", line 1, in <module>14 NameError: name ‘c‘ is not defined15 >>> ‘c‘ in a16 False17 >>> ‘s‘ in b18 True19 >>> ‘C‘ in a20 True21 >>> [a]22 [‘Congratulations‘]23 >>> ord(‘a‘)24 9725 >>> chr(86)26 ‘V‘27 >>> ord(a)28 Traceback (most recent call last):29   File "<stdin>", line 1, in <module>30 TypeError: ord() expected a character, but string of length 15 found31 >>> c=‘你好‘32 >>> d=‘国‘33 >>> len(c)34 235 >>> len(d)36 137 >>> ord(d)38 2226939 >>> 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 "<stdin>", line 1, in <module>TypeError: must be str, not int>>> s3.index(‘5‘)Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found>>> 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 >>> list1 4 [‘Mike‘, ‘and‘, ‘Tom‘] 5 >>> tuple1=tuple(list1) 6 >>> tuple1 7 (‘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 "<stdin>", line 1, in <module>17 TypeError: ljust() takes at least 1 argument (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

Python语言基础与应用 (P16)上机练习:基本数据类型

评论关闭