Python variables and data types

Python basic learning method:

In the early stage of Python learning, you will use some small cases/small functions to learn the basics of Python with the goal of understanding the code; practical problems;

Please study according to the ideas in the article. The article contains some exercises, which can be implemented in your own Python IDE. If you have never touched any Python tools, you can contact me.

Sample applet - enter student grades

Run the program below and follow the prompts to enter the grades of 3~4 students

#录入学生成绩
account_list = [] # 定义一个空列表
flag1 = 1
while flag1 == 1:
    print("=====开始录入学生成绩=====")
    single_account = []
    name = str(input("请输入一个学生姓名(按回车键继续):"))
    single_account.append(name)
    score = int(input("请输入该学生成绩(按回车键继续):"))
    single_account.append(score)
    flag1 = int(input("是否继续输入学生信息?(输入1继续;输入0结束)"))
    account_list.append(single_account)
    
print("已录入的学生成绩:")
print(account_list)

Output result:
insert image description here

First read the program carefully, understand it in your own way, and see what you can understand, such as an English word.

  • If print()it is translated into Chinese, it is printing, and its function is to print and display the things in the brackets;
    insert image description here
  • For example, input()the translation into Chinese is input, and its function is to display an input box to receive input, and input prompts can be filled in the brackets.

insert image description here

After reading the program, try to answer the following questions

  1. What features can you discover about the above program?

  2. Where are student grades stored?

Next, you will learn some basics of Python, with the goal of understanding the above program.

1 Python basic syntax

Python is a computer programming language, any programming language has its own set of syntax

To use Python, you need to understand the following three grammars
(1) Comments: Statements starting with symbols #are comments, and comments are for people to see. They can be any content, and will be ignored when the program is executed. insert image description here
(2) English letter case: All Python letters and symbols need to be written 英文输入法below . Python programs are case-sensitive, such as DDand ddare different in Python.

In the "Example Applet" while, are keywords in Python, and they are all lowercase. Keywords have a fixed usage and meaning in Python.

  • while 条件:It is used as the condition judgment of the loop. When the condition is met (this condition is true), enter the loop, execute the loop statement repeatedly , and exit the loop when the condition is not satisfied (this condition is false); let ’s
    see from the perspective of the flow chart whilehow is it like?
    insert image description here

(3) Code indentation: Python uses 缩进to organize code blocks, and uses 4 spaces for indentation. Indentation can well observe the logical structure of the program.
insert image description here
As shown in the figure, while 条件it represents a loop, and the loop structure is the content of the indented part, which is very easy to identify.

Simple small test : modify the following program correctly and run it

Tip: Pay attention to proper capitalization and indentation

#修改下方程序,使其能正确运行
D = 0
D = Input("请任意输入")
While D == 0:
Print(D) #print()表示输出某个内容

Check the "Sample Applet" again, do you understand a little more?

2 variables

=What does the "sample applet" do?
Its function is to assign =the value on the right to the variable on the left , =which is actually an assignment symbol
insert image description here

  • A variable is a concept used during the running of a program 存储, and its value can change.
  • A variable needs a variable name, which can be stored and extracted by 变量名storing and extracting the value in the variable, such as flag1 is the variable name,
  • Variable names in Python must be a combination of 大小写英文, 数字and 下划线, and cannot start with a number.
    insert image description here

Simple test : Please modify the following program correctly
Tip: Pay attention to the variable name

#修改下方程序,使其能正确运行
a = 3
a = a+1
A = 4
1c = a+A
print(1c)

Review the "Example Applet", what variables are there? , what is the difference between the variables?

3 data types

insert image description here

#运行程序,查看flag1的数据类型
flag1 = 1
print(type(flag1))  # type(变量名)可查看变量的数据类型

Output result:
<class 'int'>

3.1 Integer type (int), floating point type (float), string type (string)

Simple small test : Complete the following program as required

## 在下方创建一个num1变量,赋值为100,并运行程序


print("num1 = "+str(num1)+",数据类型为"+str(type(num1)))
## 在下方创建一个num2变量,赋值为3.14,并运行程序


print("num2 = "+str(num2)+",数据类型为"+str(type(num2)))
## 在下方创建一个str1变量,赋值为你好,并运行程序


print("str1 = "+str1+",数据类型为"+str(type(str1)))

Trying to understand what is going on in the print() statement +?insert image description here

Supplementary knowledge : data type conversion
str(a):converts the data type of variable a to string type

int(b):Convert b to an integer type, provided that the value in variable b can be converted, such as b="1", b=2.2

Can you now understand what these two lines of code do?
name = str(input("请输入一个学生姓名(按回车键继续):"))
score = int(input("请输入该学生成绩(按回车键继续):"))

3.2 list

#运行下方程序,输出变量account_list中的内容
account_list = [['张三', 88], ['李四', 90], ['王五', 100]]
print(account_list)

Output result: [['Zhang San', 88], ['Li Si', 90], ['Wang Wu', 100]]

#运行下方程序,查看account_list的数据类型
print(type(account_list))

Output result: <class 'list'>

Thinking: What are the characteristics of the account_list output above?

  • A list is the most frequently used data type in Python. A list is written between square brackets [], separated by 逗号elements
    insert image description here

  • The types of elements in the list can be different, it supports numbers, strings, lists can even contain lists (nested lists)
    insert image description here

  • Lists can be indexed and read
    insert image description here

Thinking: How to read Zhang San's score of 88 from the list account_list?

account_list = [['张三', 88], ['李四', 90], ['王五', 100]]
print(account_list[0][1])

insert image description here

  • You can use append(), pop() operations to add and delete list elements
#向列表添加元素,只会在列表末尾添加
s = [1,2,3,4,"a","b"]
s.append("c")
print(s)

Output result: [1, 2, 3, 4, 'a', 'b', 'c']

#从列表中删除元素,删除列表元素需要元素的索引位置
s.pop(1) 
print(s)

Output result: [1, 3, 4, 'a', 'b', 'c']

Simple small test : Complete the following program as required

list1 = [1,2,3,4,5]
#取出list1中的数值2与数值5,相加后的结果放入list1中


print(list1)
account_list = [['张三', 88], ['李四', 90], ['王五', 100]]
#向account_list中增加赵六的成绩88:["赵六",88]


print(account_list)
#将list2中的张三删除掉
list2 = ["张三","李四","王五"]


print(list2)

Review the "Sample Applet" to understand how the student's name and grade are stored using a list in the program

3.3 Boolean type

#运行程序,输出变量cc,ee,ff,gg的运行结果
cc = 1!=3
ee = 1 == 3
ff = 1>3
gg = 1<3
print("cc = "+str(cc))
print("ee = "+str(ee))
print("ff = "+str(ff))
print("gg = "+str(gg))

Output:
cc = True
ee = False
ff = False
gg = True

Thinking: Why do cc, ee, ff, gg have such results

  • The Boolean type has only two values, True (True) and False (False).
  • It is often used to judge whether the relationship between the two is established.

insert image description here

Note the difference =between and ==. ==Indicates equal to, !=not equal to, and other symbols are greater than and less than symbols in mathematics

Check the "Sample Applet" again, can you understand while flag==1its function?

Leave a small homework
(1) understand the "sample applet" according to the drawn flow chart;
(2) copy the "sample applet" according to the drawn flow
insert image description here
chart.

Guess you like

Origin blog.csdn.net/weixin_40062244/article/details/129972490