python3基础测试之I/O、序列

一、I/O
(一)print
>>> print(str("carl"))
carl
>>> print(repr("carl"))
'carl'
>>> print("hello" + ' ' + "carl")
hello carl
>>> print("hello" + ' ' + "Mr.{}".format("carl"))
hello Mr.carl
>>> print("hello" + ' ' + "Mr.{0}".format("carl"))
hello Mr.carl
>>> print("hello" + ' ' + "Mr.{name}".format(name = "carl"))
hello Mr.carl
>>> print("hello" + ' ' + "Mr.{name}".format(name = "carl") + ' ' + "value:{val}".format(val = 1))
hello Mr.carl value:1
>>> print( ("hello" + ' ' + "Mr.{name}" + ' ' + "value:{val}").format(name = "carl", val = 1))
hello Mr.carl value:1
>>> a = 2
>>> print("a = {}".format(a))
a = 2
>>> print("a = {a}".format(a=a))
a = 2
(二)input
>>> val = input("val = ")
val = 1
>>> type(val)
<class 'str'>
>>> val
'1'
>>> int(val)
1
>>> val
'1'
>>> val = eval(input("val = "))
val = 1
>>> type(val)
<class 'int'>
>>> val
1


>>> a, b, c = eval(input("input the values of a, b, c:"))
input the values of a, b, c:1, 2, 3
>>> a
1
>>> b
2
>>> c
3
二、序列
(一)str
(二)list
(三)tuple
(四)dict
>>> str_test = "string"
>>> list_test = [1, 2, 3]
>>> tuple_test = (4, 5, 6)
>>> dict_test = {"name":"Carl", "age":"23"}
//str测试
>>> tmp = str(list_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> tmp = str(tuple_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> tmp = str(dict_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
//list 测试
>>> tmp = list(str_test)
>>> tmp
['s', 't', 'r', 'i', 'n', 'g']
>>> tmp = list(tuple_test)
>>> tmp
[4, 5, 6]
>>> tmp = list(dict_test)
>>> tmp
['name', 'age']
//tuple 测试
>>> tmp = tuple(str_test)
>>> tmp
('s', 't', 'r', 'i', 'n', 'g')
>>> tmp = tuple(list_test)
>>> tmp
(1, 2, 3)
>>> tmp = tuple(dict_test)
>>> tmp
('name', 'age')
//dict 测试
>>> tmp = dict(str_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 1; 2 is required
>>> tmp = dict(list_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
>>> tmp = dict(tuple_test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence

小结:
(1)symble:str:“”;list:[ ];tuple:( );dict:{ }。
(2)str、list、tuple和dict都是类;同时,str和dict的对象只能是str,list和tuple的对象可以是其他三个。

(五)序列方法

三、语句

四、函数

五、抽象、类

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值