首页 > 游戏资讯 >  > 

基础python编程题(python编程入门题)

比如输入 123,s 接收的是一个字符串,即s="123"

基础python编程题(python编程入门题)基础python编程题(python编程入门题)


基础python编程题(python编程入门题)


for开始

c='1'

eval(c) 即eval('1') ,返回1

template[1] = '一'

end="",打印完,结尾添加空字符,就不会换行

所以,打印的是"一二三"

6.(1) 编程:将列表的元素按逆序重新存放。

def rrseList(list):

list.rrse()

return list

(2) 编程:将列表中的偶数变成其平方值,奇数保持不变。

def squareEvenNumber(list):

for i in range(len(list)):

if list[i] % 2 == 0:

list[i] = list[i] list[i]

return list

(3) 编程:生成包含100个100以内的随机正整数的元组,统计每个数出现的次数。

import random

def countRandom():

nums = tuple(random.randint(0,100) for i in range(100))

count = {}

for i in nums:

if i not in count:

count[i] = 1

else:

count[i] += 1

return count

(4) 编程:输入5 X 5 的矩阵a,完成下列要求:

a. 输出矩阵a

b. 将第2行和第5行元素对调后,再重新输出a

def matrixChange(matrix):

row_2 = matrix[1]

row_5 = matrix[4]

matrix[1] = row_5

matrix[4] = row_2

return matrix

matrix = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]

print('输出矩阵a:')

for row in matrix:

for col in row:

print(col, end=' ')

print()

matrix = matrixChange(matrix)

print('将第2行和第5行元素对调后,再重新输出a:')

for row in matrix:

for col in row:

print(col, end=' ')

print()

7.(1)

week_dict = {"Monday": 1, "Tuesday": 2, "Wednesday": 3, "Thursday": 4, "Friday": 5, "Saturday": 6, "Sunday": 7}

keys = list(week_dict.keys())

values = list(week_dict.values())

key_values = list(week_dict.s())

print(keys)

print(values)

print(key_values)

(2)

student_dict = {}

total = 0

lowest = 11

highest = 0

for i in range(10):

name = input("Please enter the student's name: ")

score = int(input("Please enter the student's score: "))

student_dict[name] = score

total += score

if score < lowest:

lowest = score

if score > highest:

highest = score

print("The highest score is", highest)

print("The lowest score is", lowest)

(3)

import random

A = set()

B = set()

for i in range(10):

A.add(random.randint(0, 10))

B.add(random.randint(0, 10))

print("The set A is", A)

print("The set B is", B)

print("The length of A is", len(A))

print("The length of B is", len(B))

print("The union of A and B is", A | B)

print("The intersection of A and B is", A & B)

print("The difference of A and B is", A - B)

# -- coding: utf-8 --

python3环境

如果是py2的话,把print 后面的括号去掉

这个题适合用Python思维做,也就是批量处理每个字符。

无非两个要点:判断是否小写;统计小写字母个数

判断可以用filter

统计个数可以用set

len(set(filter(lambda x:x == x.lower(), astr)))

astr是你的字符串变量

source = 'AabcBB'

count = sum(map(lambda x:0 if x<'a' or x>'z' else 1, source))

print(count)

该为组合数学中的卡特兰数,其通式为C(2n,n)-C(2n,n-1)

这里采用递推关系求解,即动态规划的方法

设n对父子有d[n]种出场策略,注意初值d[0]=1

因为每个孩子前面必有一个父亲与之对应

对于i对父子,遍历第j个孩子,该孩子前面有j-1个孩子,对应d[j-1]种出场策略

后面有i-j个孩子,对应d[i-j]种出场策略,则d[i]+=d[j-1]d[i-j],最终d[n]即为所求

python代码如下:

n = int(input())

d = [0] (n+1)

d[0] = 1

for i in range(n+1):

for j in range(i+1):

d[i] += d[j-1] d[i-j]

print(d[n])

运行结果如下:

望采纳~

def strProc(strs,word,rep="XXXX"):

....print(word,'共出现:',strs.count(word),'次')

....print("替换后:",strs.replace(word,rep))

if __name__ == '__main__':

....s = 'Good good study and ay day up!'

....print(strProc(s,'good'))

这个是区分大小写的,如果不区分,要先把字符串和查找的词统一转成大写或小写。

class Time:

def __init__(self, hours, minutes, seconds):

self.__hours = hours

self.__minutes = minutes

self.__seconds = seconds

def hours(self):

return self.__hours

def minutes(self):

return self.__minutes

def seconds(self):

return self.__seconds

def __add__(self, other): # 定义加法行为

pass

def __sub__(self, other): # 定义减法行为

pass

def __eq__(self, other): # 定义等于号行为

pass

def __lt__(self, other): # 定义小于号行为

pass写出大致框架,自行完善后面的四个魔法方法

dic_student = {}

count = 0

while count < 5:

name = input()

age = input()

dic_student[name] = age

count += 1

for i, j in dic_student.s():

print('{}t{}'.format(i, j))

版权声明:本文内容由互联网用户自发贡献。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 836084111@qq.com,本站将立刻删除。