Python Basic data type exercises

Source: Internet
Author: User

1, write code, such as the following list, as required to achieve each function
Li = [' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ']

Calculates the length of the list and outputs
Print (Len (LI))
Answer: The result is 5

Append the element ' seven ' to the list and output the added list
Li.append (' seven ')
Print (LI)
The result: [' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ', ' seven ']

Please insert the element ' Tony ' in the 1th position of the list and output the added list
Li.insert (0, ' Tony ')
Print (LI)
The result is:
[' Tony ', ' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ']

Please modify the element in the 2nd position of the list to ' Kelly ' and output the modified list
Li[1] = ' Kelly '
Print (LI)
The result is:
[' Alex ', ' Kelly ', ' Eric ', ' Rain ', ' Alex ']

Add each element of the list l2=[1, ' A ', 3, 4, ' heart ' to the list Li, one line of code is implemented, and the loop is not allowed to add.
Li.extend ([1, ' A ', 3, 4, ' heart ')
Print (LI)
The result is:
[' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ', 1, ' A ', 3, 4, ' heart ']

Add each element of the string s = ' Qwert ' to the list Li, one line of code is implemented, and the loop is not allowed to add.
Li.extend (' Qwert ')
Print (LI)
The result is:
[' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ', ' Q ', ' W ', ' e ', ' r ', ' t ']

Please remove the element ' Eric ' from the list and output the added list
Li.remove (' Eric ')
Print (LI)
The result is:
[' Alex ', ' Wusir ', ' rain ', ' Alex ']

Delete the 2nd element in the list and output the deleted element and the list after the element is deleted
Print (Li.pop (1))
Print (LI)
The result is:
Wusir
[' Alex ', ' Eric ', ' Rain ', ' Alex ']

Delete the 2nd to 4th element in the list and output the list after the element is deleted
Del Li[1:4]
Print (LI)
The result is:
[' Alex ', ' Alex ']

Invert all elements of the list and output the inverted list
Li.reverse ()
Print (LI)
The result is:
[' Alex ', ' rain ', ' Eric ', ' Wusir ', ' Alex ']

Please calculate the number of times the ' Alex ' element appears in the list Li and output the number of times.
Print (Li.count (' Alex '))
Results:
2

2, write code, such as the following table, using slices to achieve each function
Li = [1, 3, 2, ' a ', 4, ' B ', 5, ' C ']

Create a new list by slicing the li list l1,l1 = [1,3,2]
L1 = Li[:3]
Print (L1)

Create a new list by slicing the li list l2,l2 = [' A ', 4, ' B ']
L2 = Li[3:6]
Print (L2)

Create a new list by slicing the li list l3,l3 = [' 1,2,4,5]
L3 = Li[::2]
Print (L3)

Create a new list by slicing the li list l4,l4 = [3, ' A ', ' B ']
L4 = Li[1:6:2]
Print (L4)

Create a new list by slicing the li list l5,l5 = [' C ']
L5 = li[-1:-2:-1] #注意这里很容易误打成li [-1], it is not a slice, but directly given the C string.
# add: L5 = Li[-1:] Direct is also output [' C '], more elegant.
Print (L5)

Create a new list by slicing the li list l6,l6 = [' B ', ' A ', 3]
L6 = Li[-3:0:-2]
Print (L6)

3, write code, such as the following table, as required to achieve each function.
Lis = [2, 3, ' K ', [' qwe ', +, [' K1 ', [' TT ', 3, ' 1 ']], [], ' ab ', ' adv ']

Capitalize the ' TT ' in the LIS list (in two ways).
The first type: Upper method
Lis[3][2][1][0] = Lis[3][2][1][0].upper ()

The second type: Direct index modification
Lis[3][2][1][0] = ' TT '
Print (LIS)

Change the number 3 in the list to the string ' 100 ' (in two ways).
The first method, the direct index position assignment modification.
LIS[1] = ' 100 '
LIS[3][2][1][1] = ' 100 '
Print (LIS)

The second method takes advantage of the deletion and insertion substitution of the list.

Lis.insert (1, ' 100 ')
Lis.pop (2)
Lis2 = [' qwe ', [' K1 ', [' TT ', ' 100 ', ' 1 '], 89]
Lis.pop (3)
Lis.insert (3, Lis2)
Print (LIS)

Change the string ' 1 ' in the list to the number 101 (in two ways).
The first method, direct index modification:
LIS[3][2][1][2] = 101
Print (LIS)

# The second method, with the deletion and insertion substitution of the list
Lis2 = [' qwe ', [' K1 ', [' TT ', 3, 101]], 89]
Lis.pop (3)
Lis.insert (3, Lis2)
Print (LIS)

4, use code to implement:
Li = [' Alex ', ' Eric ', ' Rain ']
Use underscores to stitch each element of a list into a string "Alex_eric_rain"

A: The code is implemented as follows:

= [‘alex‘,‘eric‘,‘rain‘]s = ‘_‘.join(li)print(s)

5, look for the elements in the list Li, remove the spaces for each element, and find all the elements that start with ' a ' or ' a ' and end With ' C ', add to a new list, and then loop through the new list.
Li = [' Taibai ', ' alexc ', ' AbC ', ' Egon ', ' Ritian ', ' wusir ', ' AQC ']

For:

Li= [' Taibai ',' Alexc ',' AbC ',' Egon ',' Ritian ',' Wusir ',' AQC ']li2= [ ]#print (LI2)for I in Li:  #print (I.strip ()) # Pay attention to the logical operation, first calculate or again calculate and, so we need to use high-priority parentheses to first enclose or operation. if (I.strip (). StartsWith (  ' A ') or I.strip (). StartsWith ( ' A ')) and I.strip (). EndsWith (  ' C '): Li2.append (I.strip ())  #print (LI2) span class= "CF" >for J in li2: print (j) The end result is: AQC              

6, develop the sensitive word filter program, prompting the user to enter the comment content, if the user input content contains special characters:
List of sensitive words li = ["Cang teacher", "Tokyo Fever", "Enrique", "Yui Hatano")
The sensitive words in the user input are replaced with a * * *, added to a list, or added to the list above if there are no sensitive words for the user input.

# li = ["Cang teacher", "Tokyo Hot", "Enrique", "Yui Hatano")
# L2 = []
# i = input (' Please enter: '). Strip ()
# If I in Li:
# i = ' * * '
# L2.append (i)
# Else:
# L2.append (i)
# Print (L2)

7, like the following table Li = [1,3,4 ', Alex ', [3,7,8, ' Taibai '],5, ' Ritian ']
Loop through each element in the list, and then loop through the elements in the list.
The result I want is (implemented in two ways, one of which is done in range):
1
3
4
' Alex '
3
7,
8
' Taibai '
5
Ritian

The first method: Li= [1,3,4,' Alex ', [3,7,8,' Taibai '],5,' Ritian ']For IIn Li:Iftype (i) == list:  For J in I: print (j) else:print (i) The second method: (range) for i in range (len (li)): < Span class= "Hljs-keyword" >if type (li[i]) == list: for J in Li[i]: print (j) else: print (li[i])   

Python basic data type exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.