0
点赞
收藏
分享

微信扫一扫

python输入输出的一点小知识

穆熙沐 2022-07-14 阅读 54


1、自定义分割符

print("hello","World","name",sep="_")

python输入输出的一点小知识_自定义

2、导入重命名解决

from somemodule import *

例如:

import math as foobar
print(foobar.sqrt(4))

python输入输出的一点小知识_自定义_02

3、序列解包(可迭代对象解包)

value=1,2,3  # 为元组结构<class 'tuple'>
x,y,z=value
print(y) # 2

3.1用*+变量,收集多余的值

value = 1, 2, 3, 4, 5, 2
x, y, *z = value
print(z)

python输入输出的一点小知识_字符串_03

3.1.2 带*号的变量返回的值是一个列表,在变量和值个数相同时也是这样

value = 1, 5, 2
x, *y, z = value
print(y)

python输入输出的一点小知识_赋值_04

4 不输入时

name=input("Enter your name") or  '<Unknow>'
print(name)

python输入输出的一点小知识_自定义_05

5、断言assert

age=-1
assert 0<age<100 ,'Your age must be realistic'

python输入输出的一点小知识_自定义_06

6、一次输入多个数字

  1. 同时输入多个字符串,字符串间以逗号间隔
    a,b,c=input(‘三个字符串:’).split(’,’) #得到字符串

a,b,c=eval(input(‘三个数字:’))# 得到整数

2. 同时输入多个数值,字符串间以空格间隔
a,b,c=map(eval,input(‘三个数字:’).split()) # 得到整数
a,b,c=input(‘三个字符串:’).split() #得到字符串

由于input( )输出的是用空格分开的字符串,split( )会分割开各个值并放到列表中,此时在列表中的值是字符串,如果要用于运算必须在map( )中利用int( )或者float( )等处理,再赋值。如果需要赋值的值都是字符串的话就没必要用map函数了。


举报

相关推荐

0 条评论