This article is 【Python Language foundation 】 Column articles , Mainly the notes and exercises in class
Python special column Portal
The experimental source code has been in Github Arrangement
Write a function func(str), Evaluate and return string str Number in 、 The number of letters and other types of characters
stay func Use tuples to define numbers in functions 、 Letter 、 Space 、 Other , And initialize to 0. Then use for Loop through characters , successively if Determine which character type it is
Built in functions :
isdigit () Judge the numbers isalpha () Judging letters isspace () Judge the space """ @Author: Zhang Shier @Date:2022 year 05 month 28 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
content = input ( ' Input string ' )
def func(s):
num = 0
alpha = 0
space = 0
others = 0
dic = {
'num': 0,
'alpha': 0,
'space': 0,
'other': 0
}
for i in s:
if i.isdigit ():
dic[ 'num' ] += 1
elif i.isalpha ():
dic[ 'alpha' ] += 1
elif i.isspace ():
dic[ 'space' ] += 1
else:
dic[ 'other' ] += 1
return dic
print ( func ( content ) )

Test Goldbach's conjecture : Any one greater than 2 Even numbers of can be expressed as 2 Sum of prime numbers . Write a function isGDBH(n) Will the incoming 6~100 Even numbers between are expressed as 2 Sum of prime numbers , The results are saved in the list and returned . for example , Function passed in parameters 10, Then return to [“10=3+7”, “10=5+5”]
Enter a number , Verify that the number entered is within 6~100 Between , call isGDBH(n) Function to validate the data and save the results to a list , Which uses isPrime(n) First determine whether it is a prime number
""" @Author: Zhang Shier @Date:2022 year 05 month 28 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
# Judge whether it is a prime
def isPrime(n):
i=2
while i<=n:
if n%i==0:
break
i+=1
if n==i:
return True
num=int(input(" Enter even number ,6~100 Between \n"))
if num<6:
print(" Please enter greater than 6 An even number of !")
exit(0)
elif num>100:
print(" Please input less than 100 An even number of !")
exit(0)
elif (num%2)>0:
print(" Please enter an even number !")
exit(0)
i=1
result = []
def isGDBH(n):
global i
while(i<=num):
i = i + 1
if(isPrime(i)):
j=1
while(j<num):
j = j + 1
if(isPrime(j) and i<=j): #j<i Prevent duplication
if(j+i==num):
elem = str(num)+'='+str(i)+'+'+str(j)
result.append(elem)
isGDBH(num)
print(result)

Design three functions to receive three values of input , Call the respective judgment conditions through the decorator with parameters , By judgment return. The structure of the decorator is messy , You can go and check it out , There is no participation and there is participation , What I use here is a decorator with parameters , With parameters, you need to use internal decorators and internal functions
""" @Author: Zhang Shier @Date:2022 year 05 month 28 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
import math
# Define decorator with parameters
def DECO(Shape):
# Define interior decorators
def deco(func):
# Define inner function
def call_func(a,b=0,c=0):
if(Shape==' triangle '):
# print(' test ')
if ((a + b > c) & (a + c > b) & (b + c > a)):
return func ( a, b, c )
else:
return ' The input is invalid '
if(Shape==' rectangular '):
if((a>0)&(b>0)):
return func(a,b)
else:
return ' The input is invalid '
if(Shape==' circular '):
if(a>0):
return func(a)
else:
return ' The input is invalid '
return call_func
return deco
# Pass decorator parameters
@DECO(' triangle ')
def Triangle(a,b,c):
return a+b+c
@DECO(' rectangular ')
def Cube(a,b):
return (a+b)*2
@DECO(' circular ')
def Circle(a):
return 2*math.pi*a
if __name__ == "__main__":
a,b,c=map(int,input(' Enter three sides of the triangle ').split(" "))
print(Triangle(a,b,c))
a,b=map(int,input(' Enter both sides of the rectangle ').split(" "))
print(Cube(a,b))
a=int(input(' Enter the radius of the circle '))
print(Circle(a))

Mastering the multiplexing IO model in the learning notes of Python introductory development
The point of this section Mas
Statistics and analysis using Python Chapter 7, building dummies for film classification
##############################