python实战训练---基础练习(18)

连接字符串II

题目 两个字符串连接程序。

程序分析 无。

a='guangtou'
b='feipang'
print(b+a)

访问类成员

题目 回答结果(结构体变量传递)。

程序分析 无。

if __name__ == '__main__':
    class student:
        x = 0
        c = 0
    def f(stu):
        stu.x = 20
        stu.c = 'c'
    a= student()
    a.x = 3
    a.c = 'a'
    f(a)
    print(a.x,a.c)

打印星号

题目 读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。

程序分析 无。

for i in range(3):
    print('*'*int(input('input a number: ')))

解码

题目 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

程序分析 无。

n=input()
n = str(n)
a=[]
for i in range(4):
    a.append(int(n[i])+5)
a[0],a[3]=a[3],a[0]
a[1],a[2]=a[2],a[1]
print ("".join('%s' %s for s in a))

列表详解

题目:
列表使用实例。

程序分析 无。

#list  
#新建列表  
testList=[10086,'中国移动',[1,2,4,5]]  
  
#访问列表长度  
print (len(testList)  )
#到列表结尾  
print (testList[1:])
#向列表添加元素  
testList.append('i\'m new here!')  
  
print (len(testList)  )
print (testList[-1]  )
#弹出列表的最后一个元素  
print (testList.pop(1)  )
print (len(testList)  )
print (testList  )
#list comprehension  
#后面有介绍,暂时掠过  
matrix = [[1, 2, 3],  
[4, 5, 6],  
[7, 8, 9]]  
print (matrix  )
print (matrix[1]  )
col2 = [row[1] for row in matrix]#get a  column from a matrix  
print (col2  )
col2even = [row[1] for row in matrix if  row[1] % 2 == 0]#filter odd item  
print (col2even)

猜你喜欢

转载自blog.csdn.net/xdc1812547560/article/details/107715533