Python entry basic grammar knowledge 1

Basic knowledge of Python syntax

On the road of learning Python, no matter what knowledge and skills are learned, basic knowledge must be very important. Starting from today, I will share some of my learning experience and knowledge. I hope to share with you all. If you have any questions, I hope you will not hesitate to enlighten me (* ̄︶)  ̄)

I won’t introduce Python here. I believe those who learn it must also understand it. Let’s go straight to the topic.

  1. The print function
    print function in Python can output integers (int), floating-point numbers (float), character strings (str), expressions containing operators, and can also output data to files. For details, see the following code:
# print可以直接输出整数、浮点数类型
print(519)
print(81.5)

# print可以输出字符串 字符串需要带引号,否则会报错
print('helloworld')
print("helloworld")

# print可以输出含有运算符的表达式
print(2 + 1)

# print可以将数据输出到文件中  注意点:①所指定的盘符需要存在; ②使用file=xxx的形式,不然数据写不了
fp = open('E:/text.txt', 'a+')  # 输出到E盘中的text,’a+‘表示如果没有这个文件就会创新建,存在就会在这个文件内容的后面继续追加
print('helloworld', file=fp)  # 要输出helloworld,输出到fp
fp.close()

# 不进行换行输出(输出内容在一行当中) 字符串中间中英文逗号分隔
print('hello', 'world', 'python')

The results are as follows:

E:\Python\python.exe E:/py/基础语法/函数/print函数.py
519
81.5
helloworld
helloworld
3
hello world python

Process finished with exit code 0
  1. Escape character The
    escape character is: backslash (\) + the first letter that needs to be escaped.
    When there are special purpose characters such as backslash, single quotation mark, double quotation mark in the string, backslash must be used Use the bar to escape these characters; when the string contains carriage return, line feed, tab, and backspace, it is also necessary to use escape characters. See the following code for details:
# 转义字符
print('hello\nworld')  # \ +转移功能的首字母   n-->newline的首字母表示换行
print('hello\tworld')  # \t 一个tab键的字符
print('helloooo\tworld')  # \t  是否重开一个制表位取决于前面的是否占满了一个制表位
print('hello\rworld')  # r是return 回车 world将hello进行了覆盖
print('hello\bworld')  # \b是退一个格,所以hello的o没了

print('http:\\www.baidu.com')   # 需要输出\需要输入两个\\,因为其中一个是转义字符
''' 
    \' \" 的结果是输出单引号(')和双引号(“)   
    在其前面加上了\相当于使其不再是字符串的边界,而是字符串中需要输出的内容
    例子如下
'''
print('老师说:\'大家好。\'')
print('老师说:\"大家好。\"')

The results are as follows:

E:\Python\python.exe E:/py/基础语法/else/转义字符.py
hello
world
hello	world
helloooo	world
world
hellworld
http:\www.baidu.com
老师说:'大家好。'
老师说:"大家好。"

Process finished with exit code 0
  1. Original character The
    original character is to hope that the escape character in the string will not work, see the following code for details:
print(r'hello\nworld')
print(R'hello\nworld')

# 最后一个字符不能是反斜杠\  如下
# print(r'hello\nworld\')
print(r'hello\nworld\\')

The results are as follows:

E:\Python\python.exe E:/py/基础语法/else/转义字符.py
hello\nworld
hello\nworld
hello\nworld\\

Process finished with exit code 0

The first time I write a blog, the layout is not very beautiful. I hope to forgive me. I will learn and improve slowly in the future. Thank you for your time to watch, comment, bookmark, and like the classmates and seniors.

Guess you like

Origin blog.csdn.net/qq_45227014/article/details/109398814