[Python brush questions] Python from 0 to entry 3 | loop, conditional review, tuple entry, dictionary entry

foreword

- This issue is Python from 0 to entry 3 | loop, conditional review, tuple entry, dictionary entry, if you don't understand, you can comment and discuss!

  • Next, I will continue to update the Python brush question series, the database from 0 to the entry brush question series, and so on.
  • This series of articles uses Nioke's core code mode to provide case code to help you complete matching questions in the learning process from 0 to entry~
  • I recommend to everyone an artifact for writing questions and interviews . I also use this artifact to study! ~The link is as follows: Brush the question artifact jump link
  • The artifact not only has a beautiful web interface, but it is also very easy to use and get started! It is very suitable for beginners to study systematically!
  • Novices can use this artifact to do daily quizzes, read the facebooks of big factories, learn basic computer knowledge, and communicate face-to-face with Daniels~ The pictures of the quizzes have been placed below~
    insert image description here

Q1: Team grouping

Problem description: Create a list group_list containing the strings 'Tom', 'Allen', 'Jane', 'William', 'Tony' in turn representing the names of members of this group. There are currently three tasks that they need to complete. According to the complexity of different tasks and the actual situation, they need to send 2 people, 3 people, and 2 people to complete them. They decide to allocate tasks by slicing the list.
Use the print() statement and slice to print the first two elements of the list group_list to represent the name of the person who did the first task,
then use the print() statement and slice to print the middle three elements of the list group_list to represent the second task The name of the person doing the first task,
and then use the print() statement and slicing to print the list. The last two elements of group_list represent the name of the person who did the third task.

Output description:

按照题意输出
['Tom', 'Allen']
['Allen', 'Jane', 'William']
['William', 'Tony']

Case code:

group_list=[ 'Tom', 'Allen', 'Jane', 'William', 'Tony' ]
print(group_list[0:2])
print(group_list[1:4])
print(group_list[-2:])

Q2: Repeat registration is prohibited

Problem description:
Create a list current_users that contains the strings 'Niuniu', 'Niumei', 'GURR', and 'LOLO' in sequence, and then create a list that contains the strings 'GurR', 'Niu Ke Le', 'LoLo', and ' Tuo Rui Chi's list new_users, use for loop to traverse new_users, if the new user name traversed is in current_users, use print() statement to output a string similar to 'The user name GurR has already been registered! Please change it and try again!' statement, otherwise use the print() statement to output a statement similar to the string 'Congratulations, the user name Niu Ke Le is available!'. (Note: Username comparisons are not case-sensitive)

Output description: You can output according to the title description.

Case code:

current_users = ['Niuniu','Niumei','GURR','LOLO']
new_users = ['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']
current_users_up = [i.upper() for i in current_users]
for i in new_users:
  if i.upper() in current_users_up:
    print('The user name {} has already been registered! Please change it and try again!'.format(i))
  else:
    print('Congratulations, the user name {} is available!'.format(i))

Q3: Tuple - Niu Ke Games

Problem description:
Please create a tuple my_tuple that contains the strings 'Tom' and 'Andy' in sequence, first use the print() statement to print the string 'Here is the original tuple:' on one line, and then use a for loop to convert the tuple of my_tuple The content is printed; please use the try-except code block to execute the statement my_tuple[1] = 'Allen', if a TypeError error occurs, first output a newline, and then use the print() statement to print the string "my_tuple[1] = 'Allen' ' cause cause a TypeError: 'tuple' object does not support item assignment"; then re-assign a new tuple to my_tuple, which consists of the strings 'Tom' and 'Allen' in turn. To output a newline, first use the print() statement to print the string 'The tuple was changed to:' on one line, and then use the for loop to print out the contents of the tuple my_tuple to make sure that the modification is correct.

Output description:
You can output according to the title description (note that the two output parts need to be separated by a blank line).

Case code:

my_tuple = ('Tom','Andy')
print('Here is the original tuple:')
for i in my_tuple:
    print(i)

try:
    my_tuple[1] = 'Allen'
except:
    print()
    print("my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment")

my_tuple = ('Tom','Allen')
print()
print('my_tuple was changed to:')
for i in my_tuple:
    print(i)

Q4: Dictionary - traverse the dictionary

Problem description:
Create a dictionary operators_dict containing key-value pairs '<': 'less than' and '==': 'equal' in turn, first use the print() statement to print the string 'Here is the original dict:' on one line ,
and then use a for loop to traverse the list containing all the keys of the dictionary operators_dict that has been temporarily sorted in ascending order using the sorted() function, and use the print() statement to output a statement similar to the string 'Operator < means less than.'; for the dictionary After operators_dict adds the key-value pair '>': 'greater than', output a newline, and then use the print() statement to print the string 'The dict was changed to:' in one line, and use the for loop again to traverse the sorted() function Temporarily sort a list of all keys of the dictionary operators_dict in ascending order. Use the print() statement to output a statement similar to the string 'Operator < means less than.' on one line, confirming that the dictionary operators_dict has indeed added a pair of key-value pairs.

Output description: You can output according to the title description (note that the two output parts need to be separated by a blank line).

Case code:

operators_dict={
    
    '<':'less than','==':'equal'}
print('Here is the original dict:')
for k,v in sorted(operators_dict.items()):
    print(f'Operators {
      
      k} means {
      
      v}')
operators_dict['>']='greater than'
print()
print('The dict was changed to:')

for k,v in sorted(operators_dict.items()):
    print(f'Operators {
      
      k} means {
      
      v}')

Q5: Dictionary - Graduate Employment Survey

Description of the problem:
It is the graduation season again. As the president of the student union of Niu Ke University, Niu Niu decided to conduct an employment survey on the school's fresh graduates. He created a list survey_list containing the strings 'Niumei', 'Niu Ke Le', 'GURR', and 'LOLO' in sequence, as a survey list, and created a key-value pair 'Niumei': 'Nowcoder' and 'GURR': a dictionary result_dict of 'HUAWEI', as the recorded findings. Please traverse the list survey_list. If the traversed name has appeared in the list containing all the keys of the dictionary result_dict,
use the print() statement to output a string similar to 'Hi, Niumei! Thank you for participating in our graduation survey!' statement to express thanks, otherwise use the print() statement to output a statement similar to the string 'Hi, Niu Ke Le! Could you take part in our graduation survey?' to issue a survey invitation.

Output description:

按题目描述进行输出即可。
Hi, Niumei! Thank you for participating in our graduation survey!
Hi, Niu Ke Le! Could you take part in our graduation survey?
Hi, GURR! Thank you for participating in our graduation survey!
Hi, LOLO! Could you take part in our graduation survey?

Case code:

survey_list=['Niumei','Niu Ke Le','GURR','LOLO']
result_dict={
    
    'Niumei':'Nowcoder','GURR':'HUAWEI'}
for i in survey_list:
    if i in result_dict.keys():
        print(f'Hi, {
      
      i}! Thank you for participating in our graduation survey!')
    else:
        print(f'Hi, {
      
      i}! Could you take part in our graduation survey?')

Q6: Name and student number

Problem description:
Create a dictionary my_dict_1 containing key-value pairs {'name': 'Niuniu' and 'Student ID': 1} in sequence, and create a dictionary containing key-value pairs {'name': 'Niumei' and 'Student ID' in sequence ID': 2} dictionary my_dict_2, create a dictionary my_dict_3 containing key-value pairs {'name': 'Niu Ke Le' and 'Student ID': 3} in turn, create an empty list dict_list, use the append() method Add the dictionaries my_dict_1, my_dict_2 and my_dict_3 to the dict_list in turn, and use the for loop to traverse the dict_list. For the traversed dictionary, use the print() statement to output a statement similar to the string "Niuniu's student id is 1." to print the corresponding dictionary. Content.

Output description:

按题目描述进行输出即可。
Niuniu's student id is 1.
Niumei's student id is 2.
Niu Ke Le's student id is 3.

Case code:

my_dict_1 = {
    
    'name': 'Niuniu', 'Student ID': 1}
my_dict_2 = {
    
    'name': 'Niumei', 'Student ID': 2}
my_dict_3 = {
    
    'name': 'Niu Ke Le', 'Student ID': 3}
dict_list = []
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)

for i in dict_list:
    # 字典获取元素的方法i['key值'],或者i.get('key值')
    print(f"{
      
      i['name']}'s student id is {
      
      i.get('Student ID')}.")

Summarize

Click on the link to jump to register and start your nanny-level problem-solving journey! The road of brushing questions and fighting monsters

In addition, there are not only questions here, but everything you want is here, which is very suitable for beginners and beginners to learn~
1. Algorithms (398 questions): 100 questions must be brushed in the interview, introduction to algorithms, high-frequency interview list
2 , Data Structures (300 questions): All are very classic linked lists, trees, heaps, stacks, queues, dynamic programming, etc.
3. Languages ​​(500 questions): C/C++, java, python introductory algorithm exercises
4, SQL (82 questions): Quick Start, SQL must know, SQL Advanced Challenge, Interview Question
5, Big Factory Written Exam Question: ByteDance, Meituan, Baidu, Tencent... Mastering experience is not afraid of interviews!

insert image description here

Guess you like

Origin blog.csdn.net/weixin_51484460/article/details/125839527