Basic Python functions-input() and print()


Preface

Python is a simple, easy to learn, free, and open source cross-platform programming language that supports imperative and functional programming and fully object-oriented programming . For beginners, Python can get started quickly , but it is not easy to learn more. In recent years, the Python programming language has been loved by people from all walks of life, and more and more people have joined the team of learning Python.
Why is Python so popular?
There are two reasons: On the one hand, due to the grammatical simplicity of the Python language , users can focus more on business logic rather than the language itself. On the other hand is the powerful data processing capabilities of Python .


One, Python

Python organizes code with objects as the core, supports multiple programming paradigms, uses dynamic types, and automatically recycles memory. It has both a powerful standard library and a rich third-party extension package.
Python can be used in the following areas:
1. Web and Internet development
2. Scientific computing and statistics
3. Artificial intelligence
4. Desktop interface development
5. Software development
6. Back-end development
7. Web crawler

Let us briefly understand the basics of Python language


The running environment of the following code is (IDLE(Python3.8 64-bit))

Two, the console input input () and output print ()

1. Data input input()

Python provides the input() function for data input. The feature of this function is that no matter what the user inputs, the return value type of this function is a string type .

The code is as follows (example):

#input(prompt = none,/)
#其中prompt表示提示信息(“请输入x的值:”),有提示信息则会显示,如果没有则默认为空。斜线表示该函数只接受位置参数而不接受关键参数,但Python中并不允许用户自定义这样的函数。
x = input("请输入X的值:")
print("x + ",x)
print(type(x))

No matter what number or string the user enters in the above code, the final return value type of X is a string.
What if we want to get other types of data? We can use a series of functions such as type conversion functions int() and float() to achieve this. In some cases, we can also use the eval() function to calculate the value of the expression represented by the string.

#int([x])
x = (int)(input("请输入X的值:"))
#对于int()如果期望得到整数时,不能输入带小数点的值,如2.0 否则会出错:ValueError: invalid literal for int() with base 10: '2.0'
print(type(x))
print("x = ",x)
#float同理

#eval(source,globals = None,locals = None,/)
#其中source为字符串,globals为dictionary对象,locals为任何map对象。
x = eval(input("请输入X的值:"))
y = eval(input("请输入Y的值:"))
sum = eval(input("请输入:"))
print(type(x))
print(type(y))
print(type(sum))
print("x = ",x,"\ny = ",y,"\n1 + 2 = ",sum)

Insert picture description here

2. Data output print()

The print() code format is as follows (example):

print(value,..(可以有多个值).., sep=' ', end='\n', file=sys.stdout, flush = False)
value:表示需要输出的对象(输出的元素),可以有多个
sep: 输入时对象之间的间隔符(元素间的插入值),默认为空格
end: 元素结尾插入值,表示输出以何值结尾,默认为换行符 ‘\n’
file: 表示元素输出位置; 可以输出到文件,默认为sys.stdout(标准输出)。对于file指定的对象必须要有“写”的方法
flush:表示是否将缓存里的内容强制刷新并输出,默认为false
print("我是小小王权")
print("我是"+"小小王权")
#第二行的“+”在这里表示字符串的连接
print("我","是","小","小","王","权")
print("我","是","小","小","王","权",sep = '( >_< )',end = '\n')
print("我","是","小","小","王","权",sep = '*')
print("我是")
print("小小王权")
print("我是",end = '@')
print("小小王权")
with open('C:\\text\\wnagquan.txt','w') as f:
	print("小小王权",fule = f)
#11、12行表示将输出的 小小王权 写入到C盘的test文件中的wnagquan.txt文件
#C:\\text\\wnagquan.txt该路径读者可自己选择

Insert picture description here


Concluding remarks

The common functions in python and basic functions int(), float(), etc. will be introduced in detail later.

Guess you like

Origin blog.csdn.net/weixin_46658699/article/details/109659873