Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the list, tuple and condition judgment in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

How to analyze the list, tuple and condition judgment in Python, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

1. List: a built-in data type in list# 1.list:Python, list; # 2.list is an ordered collection in which elements can be added and removed at any time # Eg: list the names of all the students in the class studentNames = ['Willard','ChenJD','ChenXiaoBao'] print ("names of all students in the class:", studentNames) print ("- -") # 3. To get the number of list elements, the len () function studentNamesLen = len (studentNames) print ("the number of elements of studentNames is:", studentNamesLen) print ("-") # 4. Use the index to access elements at every location in the list The index in Python starts at 0 ("the first element of studentNames is:", studentNames [0]) print ("the last element of studentNames is:", studentNames [- 1]) print ("the last element of studentNames is:" StudentNames [len (studentNames)-1]) print ("-") # Tips:# index values cannot be out of range # 5.list is a mutable ordered list You can append and delete elements and replace element # an in list. Add element print ("list before inserting element:", studentNames) studentNames.append ("ChenBao") print at the end of the list ("list after adding an element at the end of the studentNames:" StudentNames) print ("-") # b. Insert element print ("list before inserting element:", studentNames) studentNames.insert (3, "LinWenYu") print ("insert element at index number 3:" StudentNames) print ("-") # c. Delete the element print at the end of list ("list before deleting elements:", studentNames) studentNames.pop () print ("list after deleting elements:", studentNames) print ("-") # d. Delete the element print at the specified location ("list before deleting specified element:", studentNames) studentNames.pop (3) print ("list after deleting specified element:", studentNames) print ("-") # e. Replace an element with another element print ("list before replacing element:", studentNames) studentNames [2] = "ChenBao" print ("list after replacing element:" StudentNames) print ("- -") print ("-- -") # f. The element data types in the list can be different informationList1 = ["willard", 18170] informationList2 = [["willard", 18170], ["ChenJD", 18168]] print ("content of informationList1:", informationList1) print ("content of informationList2:" InformationList2) print ("-") # g. Empty list emptyList = [] print ("this is an empty list:", emptyList) print ("-") # h. List type scoreList = [10099999998] print ("list type is:", type (scoreList))

# result output:

Names of all the students in the class: ['Willard',' ChenJD', 'ChenXiaoBao']

The number of elements in studentNames is: 3

The first element of studentNames is: Willard

The last element of studentNames is: ChenXiaoBao

The last element of studentNames is: ChenXiaoBao

List before element insertion: ['Willard',' ChenJD', 'ChenXiaoBao']

List after adding an element at the end of the studentNames: ['Willard',' ChenJD', 'ChenXiaoBao',' ChenBao']

List before element insertion: ['Willard',' ChenJD', 'ChenXiaoBao',' ChenBao']

Insert the element at index number 3: ['Willard',' ChenJD', 'ChenXiaoBao',' LinWenYu', 'ChenBao']

List before deleting the element: ['Willard',' ChenJD', 'ChenXiaoBao',' LinWenYu', 'ChenBao']

List after deleting elements: ['Willard',' ChenJD', 'ChenXiaoBao',' LinWenYu']

List before the specified element is deleted: ['Willard',' ChenJD', 'ChenXiaoBao',' LinWenYu']

List after deleting the specified element: ['Willard',' ChenJD', 'ChenXiaoBao']

List before replacing elements: ['Willard',' ChenJD', 'ChenXiaoBao']

List after replacing elements: ['Willard',' ChenJD', 'ChenBao']

Content of informationList1: ['willard', 18170]

Content of informationList2: [['willard', 18,170], [' ChenJD', 18,168]]

This is an empty list: []

The list type is:

two。 Tuple: tuple# 1.tuple: tuple, once initialized Cannot modify studentNames = ("Willard", "ChenJD", "ChenBao") print ("studentNames tuple:", studentNames) print ("-") # 2.tuple has no append (), insert () method # 3.tuple can get elements by index; # 4. Define empty tuple:emptyTuple = () print ("this is an empty tuple:", emptyTuple) print ("- -") # 5. A tupleoneElementTuple = (1,) # that defines only one element cannot be defined as: oneElementTuple = (1) print ("this is a tuple with only one element:", oneElementTuple) print ("-") # 6. Tuple type studentNames = ("Willard", "ChenJD", "ChenBao") print ("tuple type is:", type (studentNames))

# result output:

StudentNames tuple: ('Willard',' ChenJD', 'ChenBao')

This is an empty tuple: ()

This is a tuple with only one element: (1,)

The tuple type is:

3. Conditional judgment # if syntax: if the if statement determines that it is True, then execute the statement block after if, otherwise, nothing will be done; # 1. Example: score grading score = int (input ("Please enter your score:") if score > 100or score

< 0: print("您的输入有误,请重新输入!")if score >

= 90 and score = 80 and score

< 90: print("成绩等级为B")if score >

= 70 and score

< 80: print("成绩等级为C")if score >

= 60 and score

< 70: print("成绩等级为D")if score >

= 0 and score

< 60: print("成绩等级为E") # 结果输出: 请输入您的分数:100 成绩等级为A ------------------ 请输入您的分数:-1 您的输入有误,请重新输入! # if-else语法:如果if语句判断是True,则执行if后的语句块;# 否则,执行else语句后的语句块;# 2.实例:判断输入的账号密码是否正确userName = input("请输入您的账号名称:")password = input("请输入您的密码:")if ((userName == "Willard") and (password == "JD584520")): print("账号密码输入正确,登录成功!")else: print("账号或密码输入错误,登录失败!") # 结果输出: 请输入您的账号名称:Willard 请输入您的密码:JD584520 账号密码输入正确,登录成功! --------------------------- 请输入您的账号名称:Willard 请输入您的密码:jd584520 账号或密码输入错误,登录失败! # if-elif-else语法:if : elif : elif : else: # 实例3:使用if-elif-else判断成绩等级score = int(input("请输入您的分数:"))if score >

100 or score

< 0: print("您的输入有误,请重新输入!")elif score >

= 90 and score = 80 and score

< 90: print("成绩等级为B")elif score >

= 70 and score

< 80: print("成绩等级为C")elif score >

= 60 and score

< 70: print("成绩等级为D")else: print("成绩等级为E") # 结果输出: 请输入您的分数:60 成绩等级为D -------------------- 请输入您的分数:-1 您的输入有误,请重新输入! # 小实例:# 综合实例print("欢迎来到华中科技大学成绩查询网!")print("请输入您的账号密码进行登录!")print("---------------------------------")userName = input("请输入您的账号:")password = input("请输入您的密码:")if ((userName == "Willard") and (password == "JD584520")): print("账号密码正确,登录成功!") print("请您输入您的分数,查询对应的分数等级!") score = int(input("请输入您的分数(0-100):")) if score >

100 or score

< 0: print("您的输入有误,请重新输入!") elif score >

= 90 and score = 80 and score

< 90: print("成绩等级为B") elif score >

= 70 and score

< 80: print("成绩等级为C") elif score >

= 60 and score < 70: print ("grade D") else: print ("grade E") else: print ("account or password entered incorrectly, login failed!") Print ("Please re-enter your account password!")

# result output:

# enter 1:

Welcome to Huazhong University of Science and Technology Achievement query Network!

Please enter your account password to log in!

-

Please enter your account number: Willard

Please enter your password: JD584520

The account password is correct and the login is successful!

Please enter your score and query the corresponding score level!

Please enter your score (0-100): 100

Grade A

# enter 2:

Welcome to Huazhong University of Science and Technology Achievement query Network!

Please enter your account password to log in!

-

Please enter your account number: Willard

Please enter your password: jd584520

The account number or password was entered incorrectly, login failed!

Please re-enter your account password!

# enter 3:

Welcome to Huazhong University of Science and Technology Achievement query Network!

Please enter your account password to log in!

-

Please enter your account number: Willard

Please enter your password: JD584520

The account password is correct and the login is successful!

Please enter your score and query the corresponding score level!

Please enter your score (0-100): 101

Your input is wrong, please re-enter it!

Note: the above code has been verified, but it is not the code deployed in the production environment, just some small Demo to illustrate the relevant knowledge of Python, please skip it!

What are the advantages of Python: 1. Easy to use. Compared with traditional languages such as Java, Java and C #, Python has less strict requirements on code format. 2. Python is open source and everyone can see the source code and can be ported to many platforms. 3. Python is object-oriented and can support both process-oriented programming and object-oriented programming. 4. Python is an explanatory language, the program written by Python does not need to be compiled into binary code, and can be run directly from the source code; 5. Python is powerful and has many modules, which can basically achieve all the common functions.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report