[Python brush questions] - Python introduction 07 loop statement (on)

insert image description here

🤵‍♂️ Personal homepage: @North Pole's Sanha personal homepage

👨‍💻 About the author:PythonDomain Rising Star Creator.

📒 Series of columns: " Niuke Question Bank - Python" 🌐Recommend "

Niuke.com "——job search tool|Written Exam Question Bank|interview experience|Internship experience,One-stop solution for job hunting

👉Click the link to register and study

insert image description here


Niu Ke Question Bank " Online Programming - Python "

  pythonThe learning still needs the basic knowledge+Do-it-yourself synchronization. in the case ofpythonIf you are a novice, you need to find a website where you can practice online. I suggest you go to Niuke.com to practice more.


Getting Started with Python: 07 Loop Statements NP49 - NP61

  Doing the same thing over and over is called a loop,PythonThere are two types of loop structures in the language: traversal loops and infinite loops. traversal loop using reserved wordsfor inExtract each element of the traversal structure in turn for processing; use reserved words in an infinite loopwhileThe program is executed according to the judgment condition.

insert image description here


Length of NP49 character list

Description
Creates a string that in turn contains'P'、'y'、't'、'h'、'o'and'n'list ofmy_list,
usingprint()statement print string'Here is the original list:', and then directly use the print() statement to print the list just createdmy_listPrint the whole, output a newline, and then useprint()statement print string'The number that my_list has is:',reuseonly()function get listmy_listhow many strings are there, and useprint()The function prints the integer on one line.

Input description:
none

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

Here is the original list:
['P', 'y', 't', 'h', 'o', 'n']

The number that my_list has is:
6
  • 1
  • 2
  • 3
  • 4
  • 5

Code:

my_list=['P','y','t','h','o','n']
print('Here is the original list:')
print(my_list)
print()
print('The number that my_list has is:')
print(len(my_list))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Self-test run save commit:
insert image description here


NP50 Programmer's Day

Description
Niu Niu, Niu Mei and Niu Keke are all loyal users of Nowcoder, and it is the annual Programmer's Day (October 24th). There is no doubt that they are all logged in to Nowcoder because they have not finished brushing Niu Ke Tiba. …

The admin of Nowcoder wanted to send them some simple login greetings and wished them the holidays.

Please create a sequence containing the string'Niuniu', 'Niumei'and'Niu Ke Le'list ofusers_list,please useforloop through the listuser_list, in turn for the listusers_listThe name in the output is a line like'Hi, Niuniu! Welcome to Nowcoder!'String,forAfter the loop ends, the last line of string is output"Happy Programmers' Day to everyone!"

Input description:
none

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

Hi, Niuniu! Welcome to Nowcoder!
Hi, Niumei! Welcome to Nowcoder!
Hi , I'm Not Afraid! Welcome to Nowcoder!
Happy Programmers' Day to everyone!
  • 1
  • 2
  • 3
  • 4

Code:

users_list = ['Niuniu', 'Niumei', 'Niu Ke Le']
for i in users_list:
    print(f'Hi, {i}! Welcome to Nowcoder!')
print("Happy Programmers' Day to everyone!")
  • 1
  • 2
  • 3
  • 4

Self-test run save commit:
insert image description here


Max and Min of NP51 List

Description
Niu Niu just learned the loop statement , you can teach him to useforstatement to create a10arrive50list of numbers? Please output the complete list, and output the first and last elements of the list to check whether it is from10arrive50.

Input description:
none

Output description:
The first line outputs the complete list.
The second line outputs the first and last elements of the list, separated by spaces.

Code:

ls = [i for i in range(10, 51)]
print(ls)
print(ls[0], ls[-1])
  • 1
  • 2
  • 3

Self-test run save commit:
insert image description here


NP52 cumulative number and average

Description
Niuniu has a list of his and his colleagues' ages, you can useforLoop through each element of the linked list and add them up to get the sum and average of their ages?

Input description:
Enter multiple integers on one line, separated by spaces.

Output description:
Output the sum and average of ages. The average is kept to 1 decimal place, and the two numbers are separated by spaces.

Example 1
input:22 23 24
output:69 23.0

Code:

ls = list(map(int, input().split(" ")))
s = 0
for i in ls:
    s += i
print("{} {:.1f}".format(s, s/len(ls)))
  • 1
  • 2
  • 3
  • 4
  • 5

Self-test run:
insert image description here


NP53 top 10 even numbers

Description
By giving the functionrange()Specify the third parameter to create a listmy_list, which contains[0, 19]all even numbers in ; use one moreforThe loop prints all these numbers (each number on its own line).

Input description:
none

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

Code:

my_list = [i for i in range(0, 20) if i%2==0] 
for i in my_list:
    print(i)
  • 1
  • 2
  • 3

Self-test run save commit:
insert image description here


Recommended: Niu Ke Question Ba - classic high frequency interview question bank

🌐Job Search Artifact-|Written Exam Question Bank|Interview Experience|Dachang Interview Questions👉Click the link to register and study
insert image description here

Tags: [Python brush questions] - Python introduction 07 loop statement (on)

Niu Ke brush questions - Python articles python interview Development language

Related: [Python brush questions] - Python introduction 07 loop statement (on)