复制代码
# coding:utf-8
1.Python是如何进行类型转换的?
函数描述
int(x[, base]) 将x转换为一个整数
long(x[, base]) 将x转换为一个长整数
float(x) 将x转换到一个浮点数
complex(real[, imag]) 创建一个复数
str(x)将对象x转换为字符串
repr(x)将对象x转换为表达式字符串
eval(str)用来计算在字符串中的有效Python表达式, 并返回一个对象
tuple(s)将序列s转换为一个元组
list(s)将序列s转换为一个列表
chr(x)将一个整数转换为一个字符unichr(x)将一个整数转换为Unicode字符
ord(x)将一个字符转换为它的整数值
hex(x)将一个整数转换为一个十六进制字符串
oct(x)将一个整数转换为一个八进制字符串
2.Python里面如何实现tuple和list的转换?
t = tuple(list)
t = list(l)
3.Python中pass语句的作用是什么?
pass语句什么也不做,一般作为占位符或者创建占位程序,pass语句不会执行任何操作
4.遍历字符串
a = "ABCDEFG"
for i in (a): print(i)
结果:A B C D E F G
5.排序
a = [1, 3, 2, 6, 9, 4]
a.sort()
print(a)
结果:1,2,3,4,6,9
6.去重遍历打印
list = [1, 2, 3, 4, 7, 7, 8, 3, 5, 6, 7, 6]
new_list = []
for i in list:
if i not in new_list: new_list.append(i)
print(new_list)
结果:1,2,3,4,5,6,7,8
7.翻转list
def Reverse(lst): lst.reverse()
return lst
lst = [11, 11, 12, 13, 14, 15]
print(Reverse(lst))
结果:15,14,13,12,11,11
8.翻转字符串
s = '1234'
print(s[::-1])
结果:4321
9.算出n的累计的积, 比如(n=5, 1 * 2 * 3 * 4 * 5 = 120)
def sum_cycle(n): sum = 1
for i in range(1, n + 1):
sum = sum * i
print(sum)
sum_cycle(5)
结果:120
10.打印水仙花
for flower in range(0, 1000):
a = flower // 100
b = flower // 10 % 10
c = flower % 10
if flower == a ** 3 + b ** 3 + c ** 3:
print(flower)
结果:0 1 153 370 371 407
12.随机匹配 members
members = {'a','b','c','d','e','f','g','h','i','j','k'}
def comrade(members):
print(members.pop(),end='')
print('和',end='')
print(members.pop())
try:
while len(members)>0:
comrade(members)
except KeyError:
print(' :该成员单个数,没有匹配到战友')
13,五个人喜欢打游戏,问即打游戏有打篮球的人都有哪些?
play_basketball = ['a','b','c','d','e']
play_game = ['a','b','c','f','g']
both_play=[]
for name in play_basketball:
if name in play_game:
both_play.append(name)
print(both_play)
结果:
['a', 'b', 'c']
14.截取指定字符串
str="abcdefg"
print(str[1:3])
结果:bc