Detailed notes of the basic version of Python entry for horse soldiers at station B (1)

Preface: This article is the author's notes when learning python at station B, so I will also write some personal thoughts. The teaching video of station B I watched is the first teaching video in the python tag. I hope the notes will be useful to everyone .

1. First, the explanation and examples of the print function

  1. print can directly output the string, but it is wrong to use quotation marks to enclose the string.
print('hello world')
#print('hello world')		这个就是错误的

2. Print can directly output numbers and expressions, without quotation marks, just write directly

print(520)
print(95.2)
print(3+1)

3. Print can save the output value to a file, because print has the file attribute, so the output value can be output to file instead of output to the display

fp=open('./text.txt','a+')
print('hello world',file=fp)
fp.close()

4. After the print output, it will automatically wrap, so if you want not to wrap, you need to output everything in a print

print('hello','world')

2. Explanations and examples for escape characters

1. Escape character:
\n: stands for newline, equivalent to newline

print('hello\nworld')

\r" stands for carriage return, which is equivalent to returning to the first character of this line, the previous one is equivalent to not being written, equivalent to return

print('helloooo\rworld')

\b: It means backspace, delete one cell forward, which is equivalent to backspace

print('hello\bworld')
print('helloo\bworld')

\t: It stands for tab. Generally, it is based on four characters as a unit. A few are missing and a few are filled. If there are no missing, just write four. For example, in the following example, you will find that the number of spaces between them are different

print('hell\tworld')
print('hello\tworld')
print('helloo\tworld')
print('hellooo\tworld')

\: In the string, if you want to use \, you have to use the escape character \, one \ needs one escape, two slashes need two \

print('http:\\www.baidu.com')   #仅仅只是用来举例,不表示正确的网址,正确的网址下面应该是//,不是\\

print('http:\\\\www.baidu.com')

#': In the string, if you need to use', you need to add an escape character

print('the teacher said\'good morning\'')
#print(print('the teacher said'good morning''),这样是会报错的

#r: In the string, if you need to use its original meaning without expressing it as the value behind the escape character, you need to add r to represent the value of the original character, but there is a condition, after using R, The last character cannot be, the aunt can be multiple \

print(r'http:\\\\www.baidu.com')
#print(r'http:\\\\www.baidu.com\')	因为这个字符串末尾是\,所以这句话是错误的,结尾不能是\
print(r'http:\\\\www.baidu.com\\')

Three, binary and character encoding

There are mainly ascll encoding and GBK encoding. What are the utf-8 encoding and unicode encoding? The difference and purpose of these four encodings are as follows.
acsll encoding uses 128 characters to represent common characters in English, and cannot represent Chinese characters or languages ​​of other countries, China So I made my own country’s GBK encoding to represent the commonly used characters in China, but GBK contains incomplete characters. China later introduced GB18030 to represent more Chinese characters. Unicode is used to analyze the different encodings of various countries. And set a prescribed standard, but this standard is not very good, because it stipulates that all characters are parsed by two bytes, which will be very slow, so what utf-8 standard has been introduced , Let English one byte to parse, Chinese three bytes to parse, to help the standard of Unicode, in general, if the encoding is not specified, it is the default unicode

4. Identifiers and keywords:

1. Python's identifier naming rules are the same as C, which can be memorized together

2.How to view keywords in python?

 import keyword
 print(keyword.kwlist)

Guess you like

Origin blog.csdn.net/qq_43511094/article/details/113062435