Python study notes (1): variables

1. Single-line comments and multi-line comments

The Python language allows you to insert null characters and comments anywhere, but cannot be inserted between identifiers and strings.

There are two forms of comments for Python source code:

  • Single line comment
  • Multi-line comments

Python uses the pound sign (#) to indicate the beginning of a single line comment, and the code following the # sign until the end of this line will be ignored by the interpreter. Single-line comment is to comment a line of code in the program. Put the # sign before the content that needs to be commented in the Python program.

Multi-line comments refer to commenting out multiple lines of code in the program at one time. Use three single quotation marks or three double quotation marks to enclose the content of the comment in a Python program.

As shown in the following code, comment_test.py

# 这是一行简单的注释
print("Hello World!")
'''
这里面的内容全部是多行注释
Python语言真的很简单
'''
# print("这行代码被注释了,将不会被编译、执行!")
"""
这是用三个双引号括起来的多行注释
Python同样是允许的
"""

2. Variable

2.1 Python is a weakly typed language

Python is a weakly typed language. Weakly typed languages ​​have two typical characteristics:

  • Variables can be assigned directly without declaration: assigning a value to a non-existent variable is equivalent to defining a new variable.
  • The data type of a variable can be changed dynamically: the same variable can be assigned an integer value for a while, and a string for a while

For specific usage, refer to the following code, weak_type.py

# 定义一个数值类型变量
a = 5
print(a)
# 重新将字符串赋值给a变量
a = 'Hello, cat!'
print(a)
# 查看此时变量a的类型
print(type(a))

2.2 Use the print function to output variables

The detailed syntax format of print(function is as follows:

print(value, ..., sep=' ', end='\n', file='sys.stdout', flush=False)
  • The value parameter can accept any number of parameters or values, so the print() function can output multiple values
  • When using the print() function to output multiple variables, the print() function separates multiple variables with spaces by default. If you want to change the default separator, you can use the sep parameter to set
  • The output of the print() function will automatically wrap. This is because the default value of the end parameter of the print() function is "\n". If you want the print() function to not wrap after the output, you only need to reset the end parameter. can
  • The file parameter specifies the output destination of the print() function. The default value of the file parameter is sys.stdout, which represents the standard output of the system, which is the screen. You can also change the parameters to make the print() function output to the specified file

The specific usage of the print() function refers to the following code, print_test.py

user_name = 'Charlie'
user_age = 8
#同时输出多个变量和字符串,默认以空格隔开多个变量
print("姓名:", user_name, "年龄:", user_age)

#同时输出多个变量和字符串,指定分隔符
print("姓名:", user_name, "年龄:", user_age, sep='|')

#设置end参数,指定输出之后不再换行
print(40, '\t', end="")
print(50, '\t', end="")
print(60, '\t', end="")

#设置file参数,指定print()函数的输出目标
#打开文件以便写入
f = open("poem.txt", "w")
print('不敢高声语', file=f)
print('恐惊天上人', file=f)
f.close()

2.3 Naming rules for variables

The identifier of the Python language must start with a letter, an underscore _, and can be followed by any number of letters, numbers, and underscores _. The letters here are not limited to 26 English letters, and can include Chinese characters, Japanese characters, etc.

When using identifiers, you need to pay attention to the following rules:

  • The identifier can be composed of letters, numbers, and underscores _, which cannot start with a number;
  • Identifiers cannot be Python keywords, but can contain keywords;
  • The identifier cannot contain spaces;

2.4 Python keywords and built-in functions

It is not recommended to use Python keywords and built-in functions as variable names.

You can view the keywords contained in Python through the following program, as shown below:

# 导入keyword模块
import keyword
# 显示所有关键字
print(keyword.kwlist)

 

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/109501160