输入输出
- 输出:
print(对象1,对象2,...,对象n,sep=' ',end='\n')
sep
表示输出对象之间的分隔符,默认为空格,参数end的默认值为''\n
,表示print()函数
输出完成后自动换行
x,y = 4,5
print(x,y,sep = ',')
print(x)
print(y)
for i in range(1,5):
print(i,end = ' ')
lst = input('Input:').split(',')
print(lst)
b = [eval(item) for item in lst]
print(b)
Input:12,3,34,678
['12', '3', '34', '678']
[12, 3, 34, 678]
- 输入:
input()函数
,input
返回的类型是string
,常常需要int()
,float()
这样的函数把返回的类型转成我们需要的类型,也可以通过eval()
这个函数当成有效的python表达式返回和计算结果
x,y = input('Input:').split(',')
print(x,y)
x,y = eval(input("Input:"))
print(x,y)
lst = list(eval(input("Input:")))
print(lst)
lst = eval(input("Input:"))
print(lst)
Input:abc,def
abc def
Input:123,456
123 456
Input:12,3,4,567
[12, 3, 4, 567]
Input:[12,3,4,567]
[12, 3, 4, 567]
- 注释:以#号开头
- 续行符:\,一条语句很长分几行来写,小括号,中括号,花括号,三引号不需要续行符
- 缩进了同样个数的空格代表是他们是一个语句块
函数式编程
- 基本函数:
map()
、reduce()
、filter()
、算子(operator):lambda
>>> lst = [3,2,5,8,1]
>>> list(map(lambda x:x*2,lst))
[6, 4, 10, 16, 2]
>>> lst = [1,2,3,4,5,6]
>>> list(filter(lambda x:x%2 == 0,lst))
[2, 4, 6]
>>> from functools import reduce
>>> lst = [1,2,3,4,5]
>>> reduce(lambda x,y:x + y,lst)
15
>>> list(map(lambda x:x*2,lst))
[6, 4, 10, 16, 2]
>>> lst = [1,2,3,4,5,6]
>>> list(filter(lambda x:x%2 == 0,lst))
[2, 4, 6]
>>> from functools import reduce
>>> lst = [1,2,3,4,5]
>>> reduce(lambda x,y:x + y,lst)
15
>>> lst = ["12","3.45","678"]
>>> list(map(eval,lst))
[12, 3.45, 678]
>>> lst1 = list(map(eval,lst))
>>> list(map(str,lst1))
['12', '3.45', '678']
>>> lst = ['abc','def']
>>> list(map(upper,lst))
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
list(map(upper,lst))
NameError: name 'upper' is not defined
>>> list(map(lambda word:word.upper(),lst))
['ABC', 'DEF']