XTU programming Python training three

Reference link: Vulnerability in input() function-Python2.x

Task 1 String splicing Input two strings in turn and assign them to s1 and s2, convert all English letters in s1 to uppercase and connect them after s2, and assign the splicing result to s3, and output the value of s3. Tip: Use the "+" operation and the upper() method of the string. Task 2 Determine the number of repetitions of the string output according to the length of the string. Input a string and assign it to s4. Determine how many times to repeat s3 according to the length of s4. Assign the result of the repeated operation to s5 and output the value of s5. For example: input "A1", if the value of s3 is 123XTU, output 123XTU123XTU. Tip: Use the built-in function len() to find the length of the string, and use "*" for repeated operations. Task 3 Access string and substring input a string of length 2 to assign s6, and determine that s6 is the second to eleventh of s5 The first occurrence position and number of occurrences between characters (including the 11th character). If the length of s5 is less than 10, it means the substring from the second character to the end of the string. For example: input "XT", if the value of s5 is 123XTU123XTU, output "2 2", the substring from the second to the 11th character is 23XTU123XT: means: "XT" starts from the first occurrence in the substring "23XTU123XT" The starting position is 2, and it appears 2 times in total. Tip: In this task, you need to use the string slice operation: [m:n], the occurrence position and the number of occurrences respectively use the common method of the sequence type: index() and count() Task 4 Replace the string input a string assignment For s8, replace all the number symbols "2" in s5 with the string stored in s8, and assign the replacement result to s9, and output the value of s9. For example: input a character string "TO", s5 is 123XTU123XTU, then the output after replacement is: 1TO3XTU1TO3XTU. Reminder: The method of character string is needed in this task: replace() Task 5 String inversion Invert the replacement result s9 in task 4, and output the value of s9. For example: the stored in s9 is: 1TO3XTU1TO3XTU, then the output inversion result is: UTX3OT1UTX3OT1 Tip: The string slice operation is needed in this task: [::-1]

Basic string operation

Task 1 code

s1=input()

s2=input()

Begin fill in the expression on the right side of "="

s3=s2+s1.upper()

End

print(s3)

Task 2 code

s4=input()

Begin fill in the expression on the right side of "="

s5=s3 * len(s4)

End

print(s5)

Task 3 code

s6=input()

Begin fill in the code between Begin and End

print(s5[1:11].index(s6),s5[1:11].count(s6))

End

Task 4 code

s8=input()

Begin fill in the expression on the right side of "="

s9=s5.replace("2",s8)

End

print(s9)

Task 5 code

Begin fill in the code between Begin and End

print(s9[::-1])

#End

Level 2: Tuple basic operation task 1 Tuple creation Input two strings from the keyboard and assign them to s1 and s2, and create a tuple t1 with the value (s1, s2). Output the value of t1. For example: input ABC,123, then output (ABC,123). Tip: To create a tuple, just use commas to separate multiple values into parentheses. When the tuple contains only one element, you need to add a comma after the element to eliminate Ambiguity, such as: x=(20,), means that a tuple with only one element 20 is created. Task 2 Tuple concatenation Input an integer and concatenate t1 in task 1 to form a new tuple t2, for example: input 20, which is the last element in t2, and output the value of t2. For example: if the tuple t1 is ("ABC", "123"), the value of the new tuple t2 generated after splicing is ("ABC", "123", 20). Tip: the splicing operator "+", When used for tuples, it means concatenating two tuples to generate a new tuple. Task 3 Repeat input of tuples and assign an integer to n, repeat output of t2 n times, and assign the repeated new tuple to t3. For example: input 2, the value of t2 is ("ABC","123",20), the output value of t3 is: ("ABC","123",20,"ABC","123",20) : The repeat operator "*", when used in a tuple, means that the tuple element is repeated n times to generate a new tuple. Task 4 Use tuples to exchange variable values. Exchange the tuples stored in t1, t2 and t3. t1 stores the value of the original t2, t2 stores the value of the original t3, and t3 stores the value of the original t1. For example: Suppose after task 1—task 3, the values of t1, t2, and t3 are: t1=("ABC","123"), t2=("ABC","123",20), t3=(" ABC","123",20,"ABC","123",20) then the result of the exchange t1=("ABC","123",20) t2=("ABC","123",20, "ABC","123",20) t3=("ABC","123") Task 5 Find the maximum value of the tuple so that the tuple t4 is equal to (12,32,33,24,35,26,47,568,69 ,1012), find the maximum value ma and minimum value mi of the tuple, and add the two maximum values and multiply the tuple length by t4 to get the result t, and add t to the last of t4 to generate a new tuple t5 , Output new tuple t5. Tip: Use max(tup), min(tup) to get the maximum value of tuple tup, len() returns the tuple length, this question t4 is initialized by yourself, and the system does not provide test examples.

Task 1 code

s1=input()

s2=input()

Begin fill in the expression on the right side of "="

t1=(s1,s2)

End

print(t1)

Task 2 code

Begin fill in the expression on the right side of "="

t2=t1+(eval(input()),)

End

print(t2)

Task 3 code

Begin

n=eval(input())

t3=t2 * n

End

print(t3)

Task 4 code

Begin fill in the expression on the right side of "="

t1,t2,t3=t2,t3,t1

End

print(t1,t2,t3)

Task 5 code

Begin fill in the expression on the right side of "="

t4=(12,32,33,24,35,26,47,568,69,1012)

ma=max(t4)

mi=min(t4)

t=(ma+mi)*len(t4)

t5=t4+(t,)

End

print(t5)

Level 3: List Basic Operation Task 1 Create a list. Input a string representing a name, create a list containing only this string lt1, and output the value of lt1. For example, if the input string is "Li Hai", then output [ "Lee Hai"] Task 2 Add elements and input 3 integers, representing the scores of mathematics, Chinese, and English respectively, to generate list lt2, add list lt2 to lt1, and output updated lt1. For example, the input integer is: 90 79 100, which means the score of the corresponding course, then the output: ["李海",90,79,100] Tip: You can use append(), extend(), insert() and "+" operations , But pay attention to their differences. Task 3 Insert an element. Input a numeric string from the keyboard, and insert this numeric string at the beginning of the list lt1, which represents the student number, and outputs the value of lt1. For example: input: "201705090345", the value of output lt1 is: ["201705090345","李海",90,79,100] Tip: You can use insert() to insert before the first element. Task 4 Deletion of the list Find the smallest element in the list lt1, delete this element in the list lt1, and then output the value of lt1. Tip: Use the built-in min() function to find the minimum value of the element in lt1, use The method remove() or pop() of the list can remove this element. You can also use the built-in function del() to delete the element at the specified position. Note the parameters: the parameter of remove() is the specific element to be deleted, and the parentheses of del() and pop() should be filled with integers, which indicate the index number of the element to be deleted. Task 5 Reverse and sort the list. Arrange and output the elements in the lt2 list in ascending order, then copy lt1 to lt3, and reverse lt3 to output. For example: the value of lt2 is [90,79,100], the output from small to large is [79,90,100], and the result of lt3 inversion is [100,90,"Li Hai","201705090345"] The methods of using the list in this task are: copy(), sort() and reverse()

Task 1 code

s1=input()

Begin

It1=[s1]

End

print(It1)

Task 2 Fill in the code in the blank space between begin and end

a=int(input())

b=int(input())

c=int(input())

begin

It2=[a,b,c]

It1=It1+It2

end

print(It1)

Task 3 Fill in the code in the blank space between begin and end

id=input()

begin

It1.insert(0,id)

end

print(It1)

Task 4 Fill in the code in the blank space between begin and end

begin

mi=min(It2)

It1.remove(mi)

print(It1)

end

Task 5 Fill in the code in the blank space between begin and end

begin

It3=It1.copy()

It2.sort()

print(It2)

It3.reverse()

print(It3)

Task 1 Convert a string into a list and a tuple. Input a string containing spaces and assign it to ss1, convert it into a list and a tuple, and output them in turn. For example: input: "My computer", output:

[ ‘M’, ‘y’, ’ ', ‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’](‘M’, ‘y’, ’ ', ‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)-Tip: Use the list() and tuple() functions. Task 2 String segmentation Use a space as a separator to divide the string ss1 into a list of multiple elements and assign it to ss2. For example, if "My computer" is divided into a list, the output is: ["My", "computer"]. Tip: Use the split() method of the string task 3 to merge the list into a string. First, pass the * operation to ss2, repeat 3 times, assign it to ss3, and then use "-" to combine the elements into a string. For example, after ss2 is repeated 3 times, the elements in ss3 are: ["My","computer","My","computer","My","computer"], after connecting with "-" as the connector The output is: "My-computer-My-computer-My-computer" Tip: Use the join() method of character strings to test instructions Note: When inputting data, enter only one number per line. The input and output samples of 1-3 task evaluation of this level are as follows: Input test data: My computer Output test result: [‘M’, ‘y’, ’ ', ‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’](‘M’, ‘y’, ’ ', ‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)["My","computer"]"My-computer-My-computer-My- computer"

Task 1 Fill in the code in the blank space between begin and end

begin

ss1=str(input())

It1=list(ss1)

print(It1)

t1=tuple(ss1)

print(t1)

end

Task 2 Fill in the code in the blank space between begin and end

begin

ss2=ss1.split(' ')

print(ss2)

end

Task 3 Fill in the code in the blank space between begin and end

begin

ss3=3*ss2

print('-'.join(ss3))

end

Recommended Posts

XTU programming Python training three
Python network programming
12. Network Programming in Python3
Detailed Python IO programming
Python GUI interface programming
Talking about Python functional programming
Python programming Pycharm fast learning
Google Python Programming Style Guide
Python3 script programming commonly used.md
Analysis of Python object-oriented programming
Python interview questions collection (three)
Black Hat Programming Application Python2
Python string three formatted output
Black hat programming application of Python1
How to understand python object-oriented programming
Python classic programming questions: string replacement