练习1
1.1) 将 “123” 转换成整数
In [6]: a = "123"
In [7]: b=int(a)
In [8]: type(b)
Out[8]: int
1.2) 将 “9999999999999999999” 转换成长整数
In [10]: c = "9999999999999999999"
In [11]: type(c)
Out[11]: str
In [12]: d = long(c)
In [13]: type(d)
Out[13]: long
In [14]: d
Out[14]: 9999999999999999999L
1.3) 将 “3.1415926” 转换成一个浮点数
In [15]: e = "3.1415926"
In [16]: f = float(e)
In [17]: f
Out[17]: 3.1415926
In [18]: type(f)
Out[18]: float
1.4) 按要求操作字符串
现有以下字符串
字符串1:" abc deFGh&ijkl opq mnrst((uvwxyz "
字符串2:" ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(&YZ "
使用字符串的各种方法转换成如下方式
ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba
In [1]: s1 = " abc deFGh&*ijkl opq mnrst((uvwxyz "
In [2]: s2 = " ABC#DEF GH%IJ MNOPQ KLRS&&TUVWX(*&YZ "
In [3]: t1 = []
In [4]: t2 = []
In [5]: x = ""
In [6]: for a in s1.lower():
...: if a.isalpha():
...: t1.append(a)
...:
In [7]: t1.sort()
In [8]: t1.reverse()
In [10]: for a in s2:
...: if a.isalpha():
...: t2.append(a)
...:
In [11]: t2.sort()
In [12]: for a in t2:
...: x += a
...:
In [13]: for a in t1:
...: x += a
...:
In [14]: x
Out[14]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba'
练习2
现有列表
list1 = ['XXXX', 'b', 3, 'c', '&', 'a', 3, '3', 3, 'aa', '3', 'XXXX']
list2 = ['e', 'f', 'g']
要求对其做以下操作:
1. 取出 ‘XXXX’ 中间的部分,形成一个新的列表list3
In [1]: list1 = ['XXXX', 'b', 3, 'c', '&', 'a', 3, '3', 3, 'aa', '3', 'XXXX']
In [2]: list2 = ['e', 'f', 'g']
In [3]: list3 = list1
In [4]: list3.pop(0)
Out[4]: 'XXXX'
In [5]: list3.pop()
Out[5]: 'XXXX'
In [6]: list3
Out[6]: ['b', 3, 'c', '&', 'a', 3, '3', 3, 'aa', '3']
2. 对list3 做一下几部操作
1)删除特殊符号
In [6]: list3
Out[6]: ['b', 3, 'c', '&', 'a', 3, '3', 3, 'aa', '3']
In [8]: list3.pop(3)
Out[8]: '&'
In [9]: list3
Out[9]: ['b', 3, 'c', 'a', 3, '3', 3, 'aa', '3']
2)统计 3 在list3中出现的次数
In [11]: list3.count(3)
Out[11]: 3
In [12]: list3.count('3')
Out[12]: 2
3)用最简短的代码去除list3中 26个字母以外的元素(要求只能对list3操作)
In [64]: list3 = ['b', 3, 'c', 'a', 3, '3', 3, 'aa', '3']
In [65]: [ i for i in list3 if str(i).isalpha() ]
Out[65]: ['b', 'c', 'a', 'aa']
4)对list3排序
In [66]: list3
Out[66]: ['b', 3, 'c', 'a', 3, '3', 3, 'aa', '3']
In [67]: list3.sort()
In [68]: list3
Out[68]: [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c']
5)在末尾追加'd',并把list2追加到list3
In [80]: list3.append('d')
In [82]: list3.append(list2)
In [83]: list3
Out[83]: [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g']]
练习3
现有两个变量
a = ('h',)
b = ('h')
1)将a和b分别追加到上一题的list3中,观察有什么区别
In [84]: a = ('h',)
...: b = ('h')
...:
In [85]: a
Out[85]: ('h',)
In [86]: b
Out[86]: 'h'
In [87]: list3
Out[87]: [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g']]
In [88]: list3.append(a)
In [89]: list3
Out[89]: [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], ('h',)]
In [90]: list3 = [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g']]
In [91]: list3.append(b)
In [92]: list3
Out[92]: [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], 'h']
2)将1生成的list3转换成元组(扩展:自己搜索方法)
In [92]: list3
Out[92]: [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], 'h']
In [93]: tuple(list3)
Out[93]: (3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], 'h')
In [94]: list3 = [3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], ('h',)]
In [95]: tuple(list3)
Out[95]: (3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], ('h',))
3)打印出只有一个元素'h'的元组,在2中生成的元组中的索引
In [96]: tuple(list3).index(('h',))
Out[96]: 11
网友评论