Python基础学习

今天遇到了的问题

Missing parentheses in call to ‘print‘

Python2和Python3不兼容原因Python3只能用print(....)

str不是迭代器,不能直接用for循环,最佳处理办法是遍历索引

输入
输入单个值
int(input())
确定个数的值
a, b, c = map(int, input().split())
未知个数的一行(一维数组)
arr = list(map(int, input().split()))
arr = list(int(i) for i in input().split())

int(i) for i in input().split()

[expression for iter_val in iterable if condition]列表解析
map(int, input().split())
map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。
map(func, Iist)
二维数组输入

n = int(input())
arr = []
for i in range(n) :
    arr.append(list(map(int, input().split())))

未声明有终止的输入

import sys
for n in sys.stdin:
	if n == '\n'
		break
	print(n)

更好的写法

try:
    while True:
        s = input()
except:
    pass

输出格式

  1. 四舍五入保留两位小数
    “{:.2f}”.format(math.pi)
    round(v, 2)
    ‘%.2f’ %(v)
  2. 保留两位小数
    a = str(v).split(‘.’)
    a[0] + a[1][:3]
posted @   chanxe  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示