python练习5.27

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变量的顺序,会导致元组的元素顺序改变
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值