day5 - loop exercises and lists

Python list

1. What is a list

① What does a list look like: a list is a container data type (sequence); Take [] as the flag of the container, in which multiple elements are separated by commas: [element 1, element 2, element 3,...]
② Characteristics of list: the list is variable (the number, value and order of elements are variable) - add, delete and change; The list is ordered - subscript operations are supported
③ List requirements for elements: no requirements (no matter what type of data can be used as list elements)

1) Empty list
Len (list) - gets the number of elements in the list

list1 = []
list2 = []
print(type(list1), type(list2))  # <class 'list'> <class 'list'>
print(bool(list1), bool(list2))  # False False
print(len(list1), len(list2))  # 0 0

2) The list can save multiple data at the same time

list3 = [89, 90, 76, 99, 58]
print(list3)
list4 = ['Li Si', 18, 'male', 100, True]
print(list4)
list5 = [10, 12.5, True, 'abc', [1, 0], {'a': 10}, (20, 9), {20, 9}, lambda x: x * 2]
print(list5)
list6 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(len(list6))

2. Query - get elements

1) Query is divided into three cases: obtaining a single element, slicing, and traversal (one by one)
① Get a single element

Syntax: list [subscript]
Function: get the element corresponding to the specified subscript in the list
explain:
List - any expression that results in a list, such as a variable that holds the list, a specific list value, and so on
[] - Fixed writing
Subscript - subscript, also known as index, is the position information of elements in an ordered sequence.
In Python, each element in an ordered sequence has two sets of subscript values: the subscript value increasing from 0 from the front to the back; Subscript value decreasing from - 1 from back to front
Note: the subscript cannot be out of bounds

names = ['Zhen Ji', 'army officer's hat ornaments', 'Han Xin', 'Lv Bu', 'Zhao Yun', 'offspring', 'Luban', 'Di Renjie']
print(names[1])
print(names[-1])
print(names[3])
# print(names[10])    # report errors

② Traversal

Method 1 - get each element in the list directly
for element in list:
Circulatory body

Method 2 - first obtain the subscript value of each element, and then obtain the element through the subscript
① for subscript in range (len):
Circulatory body

② for subscript in range (- 1, - len (list) - 1, - 1):
Circulatory body

Range (len) = = range (number of elements in the list)

Method 3 - get the subscript corresponding to each element and element in the list at the same time
for subscript, element in enumerate (list):
Circulatory body

names = ['Zhen Ji', 'army officer's hat ornaments', 'Han Xin', 'Lv Bu', 'Zhao Yun', 'offspring', 'Luban', 'Di Renjie']
for x in names:
    print(x)

print('-------------------------------Gorgeous dividing line--------------------------------')
for index in range(len(names)):
    print(index, names[index])

print('-------------------------------Gorgeous dividing line--------------------------------')
for index, item in enumerate(names):
    print(index, item)

Exercise 1: count the number of failed students

scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
count = 0
for x in scores:
    if x < 60:
        count += 1
print('Number of failed:', count)

Exercise 2: count the number of integers in the list

list7 = [89, 9.9, 'abc', True, 'abc', '10', 81, 90, 23]
count = 0
for x in list7:
    if type(x) == int:
        count += 1
print('Number of integers:', count)

Exercise 3: sum all even numbers in nums

nums = [89, 67, 56, 90, 98, 30, 78, 51, 99]
sum1 = 0
for x in nums:
    if x % 2 == 0:
        sum1 += x
print('Sum of all even numbers:', sum1)

1) Add - add element

① Add a single element

List Append - adds an element at the end of the list
List Insert (subscript, element) - inserts the specified element before the element corresponding to the specified subscript

movies = ['Fifty six degree grey', 'Godzilla vs. King Kong', 'Peach blossom Man vs chrysanthemum monster']
print(movies)     # ['fifty six degrees grey', 'Godzilla vs. King Kong', 'Peach Blossom man vs. chrysanthemum monster']

movies.append('the shawshank redemption ')
print(movies)     # ['fifty six degrees grey', 'Godzilla vs. King Kong', 'Peach Blossom man vs. chrysanthemum monster', 'Shawshank's redemption']

movies.insert(2, 'Silent lamb')
print(movies)     # ['fifty six degrees grey', 'Godzilla vs. King Kong', 'Silent Lamb', 'Peach Blossom man vs. chrysanthemum monster', 'Shaw's redemption']

② Batch add

List 1 Extend (list 2) - adds all the elements of list 2 to the end of list 1

movies.extend(['Let the bullet fly', 'Out of reach', 'V Word vendetta team'])
print(movies)   # ['fifty six degrees grey', 'Godzilla vs. King Kong', 'Silent Lamb', 'Peach Blossom man vs. chrysanthemum monster', 'Shaw's redemption', 'let bullets fly', 'out of reach']

Exercise 1: extract all passing scores from scores

scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
new_scores = []
for x in scores:
    if x >= 60:
        new_scores.append(x)

print(new_scores)

Keywords: Python Programmer AI

Added by softnmedia on Mon, 17 Jan 2022 19:36:14 +0200