10 Python input and output and file operations

10 Input and output and file operations

1. Input and output

1. Input

Python provides the input() built-in function to read a line of text from standard input. The
input() function can also receive a python expression as input and return the result of the operation

The return value of the input function is always a string, when we need to return other types, use type conversion such as: int(input())

The eval() function is used to execute a string expression and return the value
of the expression.

sum=eval(sum)
print(sum)
在键盘上输入2+5 输出为7 sum实际为字符串

Methods to accept multiple input and output from users:

a,b = eval(input("请输入两个数,用逗号分隔开"))

Using the eval function, you can achieve multi-variable input

2. Output

  • Use the print() function to add a string in parentheses to output the specified text to the screen
  • The print() function can also accept multiple strings. Use and separate them to form a series of output
    print('sdf','sdf','gbf')
    print() will print each string in turn, and the comma will be converted Output as a space
  • The print function can print integers or directly calculate the result
  • The print function can also accept multiple strings, separated by, can be connected into a string of output
print('100 + 200 =',100+200)
结果为100 + 200 = 300

Formatted output

Insert picture description here
1. Base
%o octal oct
%d decimal dec
%x hexadecimal hex

print('%o'% 20)  24 
print('%d'% 20)  20 
print('%x'% 20)  14

2. Floating point number output
a. Formatted output
Insert picture description here

b. Built-in round() function

round(number[,n_digits])
参数:
number-这是一个数字表达式
n_digits 表示从小数点到最后四舍五入的位数,默认为0,即不指定位数时,返回一个整数,而且是最靠近的整数。
返回值:该方法返回x的小数点舍入为n位数后的值

Specify the number of decimal places. The general principle of rounding.
If it happens to be .5, look at the number before 5, discard the odd number (
round down), and round(2.675,2) for even numbers. Round(2.675,2) should be 2.68 but 2.67 is related to machine precision

2. The basic operation of the file

  • ASCII code
  • Unicode is
    extended from ASCII, an industry standard in the computer field, including character sets and encoding schemes. Usually two bytes are used to represent a character. The original English encoding is changed from single byte to double byte. You only need to change the high All bytes are filled with zeros. For English letters. There is storage waste
  • UTF-8 variable-length Unicode implementation. 1 to 4 bytes are used to represent a symbol, and the byte length varies according to different symbols.

file type

  • Text files are stored in ASCII
  • Binary files are directly composed of 0 and 1, and there is no uniform character encoding

File operation

  • open a file
    • Establish the association between the files on the disk and the objects in the program.
  • File operations
    • Read
    • Write
    • Positioning
    • Other: append. Calculation etc.
  • Close file
    • Cut the connection between the file and the program
    • Write to disk and release file buffer

1. Open the file

<变量名>=open(<文件名>,<打开模式>)
文件名可以是文件的实际名字,也可以是包含完整路径的名字。
如果要操作的文件和.py在同一个目录下,只需要写文件名即可。如果不是需要写出绝对路径+文件名。
文件的打开模式有只读 写入 追加等。这个参数是非强制的。默认文件访问模式为只读。(r)

Insert picture description hereInsert picture description hereInsert picture description here
2. File writing
Insert picture description here
3. Context management statement with
In actual development, you should first consider using the context management statement with when reading and writing files. The keyword with can automatically manage resources. No matter what the reason is, jumping out of the with block can always ensure that the file is closed correctly . In addition to file operations, the with keyword can also be used for data connections, network connections or similar occasions, when reading and writing file content, the usage of the with statement is as follows.

with open(filename,mode,encoding) as fp:
	#这里写通过文件对象fp读写文件内容的语句块

Guess you like

Origin blog.csdn.net/bj_zhb/article/details/104807612