Python basic 100 questions punch in Day11

Posted by michalchojno on Thu, 03 Feb 2022 00:16:21 +0100

Title 38

For a given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), write a program to print the first half value in one line and the last half value in one line.

code implementation

Method 1: use the loop to output the tuple data

tpl = (1,2,3,4,5,6,7,8,9,10)

for i in range(0,5):
    print(tpl[i],end= ' ')
print()
for i in range(5,10):
    print(tpl[i],end= ' ')

Method 2: use the in the list The append() function adds tuple data to the list and then outputs it

tpl = (1,2,3,4,5,6,7,8,9,10)
lst1, lst2 = [], []
for i in range(0,5):
    lst1.append(tpl[i])
print(lst1,end= '\n')
for i in range(5,10):
    lst2.append(tpl[i])
print(lst2)

Method: triple slice copy

tpl = (1,2,3,4,5,6,7,8,9,10)
lt = int(len(tpl)/2)
print(tpl[:lt],end='\n')
print(tpl[lt:])

Method 4: The format() function specifies the output style, and the range (initial point, end point, step size) determines the output position

print("Initial tuple:", tpl)
[
    print("Split tuple :{List}".format(List=tpl[x : x + 5]))
    for x in range(0, len(tpl), 5)
]

Operation results

Output result:

(1, 2, 3, 4, 5)
(6, 7, 8, 9, 10)

Title 39

Write a program to generate and print another tuple whose value is an even number in a given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10).

code implementation

Method 1: use the loop to output the tuple data

tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tpl1 = tuple(i for i in tpl if i%2 == 0 )
print(tpl1)

Method 2: lambda one sentence function

tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tpl1 = tuple(
    filter(lambda x: x%2 == 0,tpl) #The return value of lambda function is True
)#filter removes the content whose return value is False
print(tpl1)

Operation results

Output result:

(2, 4, 6, 8, 10)

Title 40

Write a program that accepts a string as input. If the string is "yes" or "yes" or "yes", print "yes", otherwise print "No".

code implementation

Method 1: use if conditional statement to detect the input

text = input("Please enter:")
if text == 'YES' or text == 'Yes' or text == 'yes':
    print('Yes')
else:
    print('No')

Method 2: The join() function sets the condition

text = input("Please enter")
print_text = " ".join(
    ['Yes' if text == 'YES' or text == 'Yes' or text == 'yes' else 'No']
)
print(str(print_text))

Operation results

Output result:

Please enter y
No

Title 41

Write a program that can map() and list the square list of elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

code implementation

method:

lst = [1,2,3,4,5,6,7,8,9,10]
doublelst = map(lambda x: x**2 , lst)
print(list(doublelst))

Note: the output of the map() function is a collection, and its output needs to be converted

Operation results

Output result:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Title 42

Write a program that can map() and filter() to list the elements with even squares in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

code implementation

Methods: use map() to calculate the list and filter() to remove the function of False value

def even(x):
    return x%2 == 0

def square(x):
    return x**2

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lst1 = list(map(square, filter(even,lst)))
print(lst1)

Note: the output of the map() function is a collection, and its output needs to be converted

Operation results

Output result:

[4, 16, 36, 64, 100]

Title 43

Write a program that can filter () to generate a list of elements with an even number between 1 and 20 (both included).

code implementation

def even(x):
    return x%2 == 0

Num = filter(even , range(1,21))
print(list(Num))

Note: the output of the map() function is a collection, and its output needs to be converted

Operation results

Output result:

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Topics: Python list Functional Programming