100 small examples of Python (Exercise 2)
- 2021-12-09 09:36:21
- OfStack
Example 031: Letter recognition
Title:
Please enter the first letter of the day of the week to judge the day of the week. If the first letter is like 1, continue to judge the second letter.
Program analysis: Here, the contrast relationship is stored directly in the form of a dictionary.
weekT={'h':'thursday',
'u':'tuesday'}
weekS={'a':'saturday',
'u':'sunday'}
week={'t':weekT,
's':weekS,
'm':'monday',
'w':'wensday',
'f':'friday'}
a=week[str(input(' Please enter the 1 Bit letter :')).lower()]
if a==weekT or a==weekS:
print(a[str(input(' Please enter the 2 Bit letter :')).lower()])
else:
print(a)
Example 032: Reverse Output II
Title:
Outputs the values of the list in reverse order.
a = ['one', 'two', 'three']
print(a[::-1])
Example 033: List to String
Title:
Separate the list by commas.
L = [1,2,3,4,5]
print(','.join(str(n) for n in L))
Example 034: Calling a function
Title:
Practice function calls.
def hello():
print('Hello World!')
def helloAgain():
for i in range(2):
hello()
if __name__=='__main__':
helloAgain()
Example 035: Setting Output Color
Title:
Text color settings.
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 + " Color font of warning ?" + bcolors.ENDC)
Example 036: Calculating Prime Numbers
Title:
Find prime numbers within 100.
Program analysis: using
else
Execute
for
The reward code for the loop (if for is a normal end, not break).
lo=int(input(' Lower limit: '))
hi=int(input(' Upper limit: '))
for i in range(lo,hi+1):
if i > 1:
for j in range(2,i):
if (i % j) == 0:
break
else:
print(i)
Example 037: Sorting
Title:
Sort 10 numbers.
Program analysis: Same as example 005.
raw=[]
for i in range(10):
x=int(input('int%d: '%(i)))
raw.append(x)
for i in range(len(raw)):
for j in range(i,len(raw)):
if raw[i]>raw[j]:
raw[i],raw[j]=raw[j],raw[i]
print(raw)
Example 038: Sum of diagonal lines of matrix
Title:
Find the sum of the main diagonal elements of a 3*3 matrix.
mat=[[1,2,3],
[3,4,5],
[4,5,6]
]
res=0
for i in range(len(mat)):
res+=mat[i][i]
print(res)
Example 039: Ordered List Insert Elements
Title:
There is 1 ordered array. Now enter 1 number, and ask to insert it into the array according to the original rule.
Program analysis: First of all, judge whether this number is greater than the last number, and then consider the insertion of the number in the middle. After inserting the number after this element, it moves back one position in turn.
lis=[1,10,100,1000,10000,100000]
n=int(input('insert a number: '))
lis.append(n)
for i in range(len(lis)-1):
if lis[i]>=n:
for j in range(i,len(lis)):
lis[j],lis[-1]=lis[-1],lis[j]
break
print(lis)
Example 040: Reverse list
Title:
Output 1 array in reverse order.
Program analysis: Exchange positions in turn, or call reverse method directly.
lis=[1,10,100,1000,10000,100000]
for i in range(int(len(lis)/2)):
lis[i],lis[len(lis)-1-i]=lis[len(lis)-1-i],lis[i]
print(' No. 1 1 Implementation of: ')
print(lis)
lis=[1,10,100,1000,10000,100000]
print(' No. 1 2 Implementation of: ')
lis.reverse()
print(lis)
Example 041: Methods and variables of a class
Title:
Imitate the usage of static variables.
Program analysis: Construct classes and understand the methods and variables of classes.
a = ['one', 'two', 'three']
print(a[::-1])
0
Example 042: Variable scope
Title:
Learn how to use auto to define variables.
Program analysis:
python
Gets or sets the scope of variables in the.
a = ['one', 'two', 'three']
print(a[::-1])
1
Example 043: Scope, Methods and Variables of a Class
Title:
Imitate another case of static variable (static).
Program analysis: Comprehensive example 041 and example 042.
a = ['one', 'two', 'three']
print(a[::-1])
2
Example 044: Matrix Addition
Title:
Calculate the sum of two matrices.
Program analysis: Create a new matrix, use for iteration and take out the X and Y matrix in the corresponding position of the value, after adding into the new matrix in the corresponding position.
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
res=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(res)):
for j in range(len(res[0])):
res[i][j]=X[i][j]+Y[i][j]
print(res)
Example 045: Summation
Title:
Count the sum of 1 to 100.
a = ['one', 'two', 'three']
print(a[::-1])
4
Example 046: Breaking Loop
Title:
Squared the input number. If the squared number is less than 50, exit.
a = ['one', 'two', 'three']
print(a[::-1])
5
Example 047: Function Exchange Variables
Title:
The values of two variables are interchanged by functions.
a = ['one', 'two', 'three']
print(a[::-1])
6
Example 048: Number ratio size
Title:
Digital comparison.
a = ['one', 'two', 'three']
print(a[::-1])
7
Example 049: lambda
Title:
Use lambda to create anonymous functions.
Max=lambda x,y:x*(x>=y)+y*(y>x)
Min=lambda x,y:x*(x<=y)+y*(y<x)
a=int(input('1:'))
b=int(input('2:'))
print(Max(a,b))
print(Min(a,b))
Example 050: Random Numbers
Title:
Output 1 random number.
Program analysis: using
random
Module.
a = ['one', 'two', 'three']
print(a[::-1])
9