Python 100 questions from "None" to "yes", daily supervision and clock in learning phase I: 31-40 questions, thinking sharing + mental journey

Keywords: Python

πŸ“’πŸ“’πŸ“’πŸ“£πŸ“£πŸ“£
🌻🌻🌻 Hello, everyone. My name is Dream. I'm an interesting Python blogger. I have a small white one. Please take care of it 😜😜😜
πŸ…πŸ…πŸ… CSDN is a new star creator in Python field. I'm a sophomore. Welcome to cooperate with me
πŸ’• Introduction note: this paradise is never short of genius, and hard work is your final admission ticket! πŸš€πŸš€πŸš€
πŸ’“ Finally, may we all shine where we can't see and make progress together 🍺🍺🍺
πŸ‰πŸ‰πŸ‰ "Ten thousand times sad, there will still be Dream, I have been waiting for you in the warmest place", singing is me! Ha ha ha~ 🌈🌈🌈
🌟🌟🌟✨✨✨

Here is a record of my mental process of brushing 100 questions. I send an article every 10 questions. I hope you can learn from it and stick to it! Welcome to supervise and study together!

Question 31

1. Title

31. Please enter the first letter of the day of the week to judge the day of the week. If the first letter is the same, continue to judge the second letter.

2. Code

letter = input("please input:")
# while letter  != 'Y':
if letter == 'S':
    print('please input second letter:')
    letter = input("please input:")
    if letter == 'a':
        print('Saturday')
    elif letter == 'u':
        print('Sunday')
    else:
        print('data error')

elif letter == 'F':
    print('Friday')

elif letter == 'M':
    print('Monday')

elif letter == 'T':
    print('please input second letter')
    letter = input("please input:")

    if letter == 'u':
        print('Tuesday')
    elif letter == 'h':
        print('Thursday')
    else:
        print('data error')

elif letter == 'W':
    print('Wednesday')
else:
    print('data error')

3. Ideas

Think normally and get the final answer by comparing the input many times!

Question 32

1. Title

32. Output the values of the list in reverse order.

2. Code

a = [2, 2, 3, 5, 3, 4]
for i in a[::-1]:
    print(i)

3. Ideas

Slice YYDS!!! To reverse the list, you can also use the: a.reverse() method!

a = [2, 2, 3, 5, 3, 4]
for i in a[::-1]:
    print(i)
a.reverse()
print(a)

Question 33

1. Title

33. Comma separated list.

2. Code

a=[1,2,3,4]
for i in range(0,len(a)):
    if i!=(len(a)-1):
        print(a[i],end=',')
    else:
        print(a[i])

3. Ideas

Traverse in turn, and use a[i],end = ',' to realize the sequential output of elements and single line output! When the last element, directly output the element, and then do not output the number!

Question 34

1. Title

34. Practice function calls.

Use the function to output three times RUNOOB character string.

2. Code

def hello_runoob():
    print('RUNOOB')


def hello_runoobs():
    for i in range(3):
        hello_runoob()


if __name__ == '__main__':
    hello_runoobs()

3. Ideas

Note the fixed code of the entrance:

if __name__ == '__main__':
	Function ()

Question 35

1. Title

35. Text color setting.

2. Code

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
print (bcolors.WARNING + "Warning color font?" + bcolors.ENDC)

3. Ideas

Question 36

1. Title

Find the prime within 100.

2. Code

# -*-coding:utf-8 -*-
# @Author: it's time. I love brother Xu
# Ollie, do it!!!
# 36. Find prime numbers within 100.
for num in range(1,101):
    # Prime greater than 1
    if num>1:
        for i in range(2,num):
            if (num % i) == 0:
                break
        else:
            print(num)

3. Ideas

Train of thought... None

Question 37

1. Title

37. Sort 10 numbers.

2. Code

l = []
for i in range(1,11):
    l.append(int(input('Please enter page{}Number:'.format(i))))
l.sort()
for i in l:
    print(i)

3. Ideas

sort() function sorts the list, the simplest method!

Question 38

1. Title

38. Find the sum of the main diagonal elements of a 3 * 3 matrix

2. Code

# 38. Find the sum of the main diagonal elements of a 3 * 3 matrix
import numpy as np
a=np.random.rand(3,3)
sum = 0
for i in range(0,3):
    for j in range(0,3):
        a[i][j]=int(input('Please enter a number:'))
print(a)
for i in range(0,3):
    sum+=a[i][i]
print(sum)

3. Ideas

Question 39

1. Title

39. There is an ordered array. Now enter a number and ask to insert it into the array according to the original law.

2. Code

# 39. There is an ordered array. Now enter a number and ask to insert it into the array according to the original law.

if __name__ == '__main__':
    # Method 1: 0 as a placeholder for adding numbers
    a = [1, 4, 6, 9, 13, 16, 19, 28, 40, 100, 0]
    print('Original list:')
    for i in range(len(a)):
        print(a[i])
    number = int(input("\n Insert a number:\n"))
    end = a[9]
    if number > end:
        a[10] = number
    else:
        for i in range(10):
            if a[i] > number:
                temp1 = a[i]
                a[i] = number
                for j in range(i + 1, 11):
                    temp2 = a[j]
                    a[j] = temp1
                    temp1 = temp2
                break
    print('Sorted list:')
    for i in range(11):
        print(a[i])

3. Ideas

First judge whether this number is greater than the last number, and then consider inserting the number in the middle. After inserting, the number after this element will move back one position in turn.

Question 40

1. Title

40. Output an array in reverse order

2. Code

# 40. Output an array in reverse order
a = [9, 6, 5, 4, 1]
print(a[::-1])

3. Ideas

[wonderful article] πŸ’• [recommended in previous periods]

Python 100 questions from "None" to "yes", daily supervision and clock in learning phase I: 1-10 questions, idea sharing + mental journey
Python 100 questions from "None" to "yes", daily supervision and clock in learning phase II: 11-20 questions, idea sharing + mental journey
Python 100 questions from "None" to "yes", daily supervision and clock in learning phase III: 21-30 questions, thinking sharing + mental journey

Conclusion: the fourth issue is completed successfully. See you in the fifth issue!!! Come on, stick to it!!!

🌲🌲🌲 Well, that's all I want to share with you today
❀️❀️❀️ If you like, don't save your one button three connections~

Posted by rhosk on Sun, 05 Dec 2021 03:50:36 -0800