程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python experiment II combined data

編輯:Python

2022.5.18 Afternoon experiment

Experiment two Combined data

List of articles

    • Preface
    • Topic 1
    • Topic two
    • Topic three

Preface

This article is 【Python Language foundation 】 Column articles , Mainly the notes and exercises in class
Python special column Portal
The experimental source code has been in Github Arrangement

Topic 1

Use two methods to merge data from two lists

Problem analysis

List together , You can use + Number or extend() Method

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 18 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
leaders_1 = [ 1, 2 ]
leaders_2 = [ 3, 4 ]
full_leaders_list = leaders_1 + leaders_2
print ( full_leaders_list )
leaders_1.extend ( leaders_2 )
print ( leaders_1 )

result

Topic two

set1={2,5,9,1,3},set2={3,6,8,2,5}, Call the set operator or function to complete the following functions :

  1. towards set1 Add a new element to 7
  2. seek set1 and set2 Union
  3. seek set1 and set2 Intersection
  4. seek set1 and set2 The difference between the set
  5. Determine the given keyword key=4 Whether in set1 or set2 in

Problem analysis

Use add() Method to add new elements , Using set operators | 、& 、- , Do the intersection operation ,item in set Judge keywords

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 18 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
set1 = {
2, 5, 9, 1, 3}
set2 = {
3, 6, 8, 2, 5}
set1.add ( 7 )
print ( " Additive elements 7 The set after is :", set1 )
print ( " aggregate set1 and set2 The union of is :", set1 | set2 )
print ( " aggregate set1 and set2 The intersection of is :", set1 & set2 )
print ( " aggregate set1 and set2 The difference set of is :", set1 - set2 )
print ( " keyword key = 4 Is in collection :", (4 in set1) or (4 in set2) )

result

Topic three

Will be a class of students 《Python Programming 》 The results of this course are kept in a dictionary , The student number is the key (key), The score is a value (value). Achieve the following functions :

  1. Add student grades to the dictionary
  2. Modify the student grades specified in the dictionary
  3. Delete the specified student grade
  4. Query the grades of specified students
  5. Count the student's scores , Such as the highest score 、 Lowest score 、 Average grade

Problem analysis

Use built-in functions directly

Delete list.pop()

Inquire about list.get()

The highest max(list.valuse())

Lowest score min(list.valuse())

average max(list.valuse())/len(score)

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 18 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
score = {
'001': 96, '002': 98, '003': 92, '004': 93, '005': 94}
print ( " The initial student grade is :", score )
score[ '006' ] = 100 # add to 
print ( " Add... To the dictionary 006 Student No. 1 scored :", score )
m_num = input ( " Student ID of the student who modified the grade " )
m_score = int ( input ( " It is amended as follows " ) )
score[ m_num ] = m_score # modify 
print ( " After revising the students' grades in the dictionary :", score )
delete = input ( " Enter and delete student id " )
score.pop ( delete ) # Delete 
print ( " After deleting student grades :", score )
query = input ( " Enter the student number of the query student " )
print ( " The number is %s The score of is : %d "%(query, score.get ( query )) ) # Inquire about 
print ( " The highest score is :", max ( score.values () ) )
print ( " The lowest score is :", min ( score.values () ) )
print ( " The average is divided into :", sum ( score.values () )/len ( score ) )

result


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved