1.q3:
'''Question:
With a given integral number n, write a program to generate a dictionary that contains
(i, i*i) such that is an integral number between 1 and n (both included).
and then the program should print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}'''
#integral=input("please input an integral!")
integral=8
dic={}
z=1
while z <= integral:
dic[f"{z}"]=z*z
z=z+1
print(dic)
总结:利用变量的代表值用 f"{}"
2.q4:
"""Question:
Write a program which accepts a sequence of comma-separated numbers from console
and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')"""
#方法一
l=[]
t=()
while True:
z=input()
if z == "q":
break
l.append(z)
yuan=(z,)
t=t+yuan
print(l)
print(t)
input()
#方法二
values=raw_input()
l=values.split(",")
t=tuple(l)
print l
print t
总结:元组的连接:用两个元组作加法
字符串的分割用split()其中可以根据逗号,空白符分割,以及可以确定分割数量,分割后是列表
tuple可以将列表转换成元组
其中t=t+yuan 改变t和yuan变量的顺序,会导致元组的元素顺序改变