[Python | Basic Grammar] 01, Literals, Comments, Variables, Data Types and Conversion

Table of contents

1. Literal volume

1.1 What is a literal

1.2 Commonly used value types

1.3 Strings

1.4 How to write them in code

1.5 Summary

2. Notes 

2.1 The role of annotations

2.2 Classification of annotations

2.3 Annotation in practice

2.4 Summary

2.5 thinking

3. Variables 

3.1 What is a variable

3.2 Case - simulated wallet

3.3 Characteristics of variables

3.4 Thinking

3.5 Summary

4. Data type

4.1 Introduction

4.2 type() statement

4.3 How to use the type() statement

4.4 Thinking

4.5 Summary

5. Data type conversion

5.1 Why convert types

5.2 Common Conversion Statements

5.3 Notes on type conversion

5.4 Summary


1. Literal volume

1.1 What is a literal

Literal quantity: In the code, the fixed value written down is called literal quantity .

1.2  Commonly used value types

There are 6  types of values ​​(data) commonly used in  Python  :

1.3 Strings

        String (string ), also known as text, is composed of any number of characters such as Chinese, English, various symbols, numbers, etc. So it is called a string of characters.

like:

  • "IT Programmer "
  • "Hello World"
  • "!@#$%^&"
  • "The number number is: 003032"

Note: In Python  , strings need to be surrounded by English double quotation marks ("), and those surrounded by quotation marks are all strings.

1.4  How to write them in code

print(666)
print(13.14)
print("你好世界")

operation result:

1.5 Summary

1. Grasp the meaning of literal quantity

In the code, the fixed value written in the code is called a literal.

2. Common literal types

We currently understand: integers, floating-point numbers, and strings.

3. How to complete the output of various literals based on  the print  statement

print( literal ) , such as:

  • print(10) , output the integer 10
  • print(13.14) , output floating point number 13.14
  • print("Hello world") , output string: hello world

2. Notes 

2.1 The role of annotations

Comment: The text explaining the program code in the program code.

Function: Comments are not programs and cannot be executed . They just explain the program code so that others can understand the function of the program code, which can greatly enhance the readability of the program.

2.2  Classification of annotations

Single-line comments: start with # , and all the text on the right of # is used as an explanation, not the actual program to be executed, and serves as an auxiliary explanation.

Multi-line comment: Use a pair of three double quotation marks ("""comment content""") to explain the function and use of a piece of code, and support line breaks.

2.3 Annotation in practice

Add the code as shown in the figure

  • single-line comments and
  • multiline comment

After adding comments, execute the program to verify whether the comments have an impact on the program.

2.4 Summary

1. What is the function of comments?

Comments are explanatory statements in the code, which are used to annotate the content of the code.

Comments are not code and will not be executed by the program.

2. How to define a single-line comment?

Defined by the # sign , all content on the right side of the # sign is used as a comment;

It is recommended that there be a space between the # sign and the content of the comment;

Single-line comments are generally used to explain a line or a small section of code.

3. How to define multi-line comments?

It is defined by a pair of three quotation marks ( """ comment content """ ) , inside the quotation marks are comments, which can be wrapped;

Multi-line comments generally explain: Python  files, classes or methods.

2.5 thinking

1.  Think about whether the second  print  statement will be executed? 

3. Variables 

3.1  What is a variable

Variable: An abstract concept that can store calculation results or represent values ​​when the program is running .

Simply put, variables are used to record data when the program is running.

3.2 Case - simulated wallet

# 定义一个变量,用来记录钱包余额
money = 50
# 通过 print 语句,输出变量记录的内容
print("钱包还有:", money)

# 买了一个冰淇淋,花费 10 元
money = money - 10
print("买了冰淇淋花费 10 元,还剩余:", money, "元")

operation result: 

3.3  Characteristics of variables

        Variable, as can be seen from the name, means that the " quantity " is variable. Therefore, the characteristic of variables is that the data stored in variables can be changed.

3.4 Thinking

1. Why do we have to use variables? It's all output content, can't you just output it directly?

The purpose of variables is to store data about a running process. The purpose of storage is to: reuse

3.5 Summary

1. What is a variable and what does it do?

Variables are used to record data when the program is running.

2. What is the definition format of variables?

variable name = variable value

3. What are the characteristics of the variables?

 The value of a variable can change.

4.  How does the print statement output multiple content?

print(content 1, content 2, ..., content N)

5. How to do subtraction in Python  ?

Use the symbol - to complete the subtraction operation;

Extended: add ( + ), subtract ( - ), multiply ( * ), divide ( / )

4. Data type

4.1 Introduction

        When learning literals, we learned that data has types. Currently in the introductory stage, we mainly come into contact with the following three types of data types:

The three English words string , int , and float  are the standard names of types.

4.2  type()  statement

So, here comes the question, how to verify the type of data? We can get the type of data through the type() statement:

Syntax: type (data of the type being viewed )

4.3  How to use the type()  statement

# 方式 1:使用 print 直接输出类型信息
print(type(666))
print(type(11.345))
print(type("你好世界"))

# 方式 2: 使用变量存储 type() 语句的结果
string_type = type("你好世界")
int_type = type(666)
float_type = type(11.345)
print(string_type)
print(int_type)
print(float_type)

# 方式 3:使用 type() 语句,查看变量中存储的数据类型信息
name = "你好世界"
name_type = type(name)
print(name_type)

1. In  the print  statement, directly output the type information: 

str  is  an abbreviation for string  

2. Use variables to store  the result (return value) of type()  : 

The types checked above are all <literal> types. Can you check the data type stored in the variable ? 

Of course: yes

4.4 Thinking

1. Do variables have types? 

We can output the type through  type ( variable ) . Is this the type of variable or the type of data?

What is being viewed is: the type of data stored by the variable. Because the variable has no type, but the data it stores has.

We might say: string variables. But be aware that it's not that the variable is a string, it's that it stores: the string

4.5 Summary

1. What statement can be used to view the type of data?

type()

2. In the following code, the name_type  variable can store the type information of the variable  name  , because?

Because  the type()  statement will give the result (return value)

3. Does the variable have a type?

No, a string variable means that the variable stores a string rather than that the variable is a string.

5. Data type conversion

5.1  Why convert types

Data types can be converted to each other in specific scenarios, such as strings to numbers, numbers to strings, etc.

So, why do we convert them?

Data type conversion will be a function we will often use in the future.

like:

  • The number read from the file is a string by default, we need to convert it into a number type;
  • For the input()  statement to be learned later  , the default result is a string, and it needs to be converted if a number is needed;
  • Convert numbers to strings for writing out to external systems, etc.

5.2  Common Conversion Statements

Like the type()  statement learned earlier  , these three statements all have results (return values)

We can use  print  to output directly

or store the resulting value in a variable

# 将数字类型转换为字符串
num_str = str(11)
print(type(num_str), num_str)

float_str = str(11.345)
print(type(float_str), float_str)

# 将字符串类型转换为数字
num = int("11")
print(type(num), num)

num2 = float("11.345")
print(type(num2), num2)

# 整数转浮点数
float_num = float(11)
print(type(float_num), float_num)

# 浮点数转整数
int_num = int(11.345)
print(type(int_num), int_num)

operation result:

  

5.3  Notes on type conversion

Type conversion is not a panacea. After all, twisted melons will not be sweet. We need to pay attention to:

1. Any type can be  converted into a string by str() ;

2. The string must really be a number before the string can be converted to a number.

5.4 Summary

1. Any type can be converted to a string, right?

correct.

2. Strings can be converted to numbers at will, right?

Error, there must be only numbers in the string.

3. What will be lost when converting floating-point numbers to integers?

Loss of precision, that is, the fractional part.

Next article: [Python | Basic Grammar] 02, Identifiers, Operators, String Expansion, and Data Input_Stars.Sky's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_46560589/article/details/130357866