一、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的对象可以是其他三个。
(五)序列方法
三、语句
四、函数
五、抽象、类