I've always wanted to learn python by myself, and I'm just staying in my collection almost every time. Occasionally, they study for a few days at a time and give up. I hope I can stick to it this summer without any requirement, I just want to learn a little bit every day.
Learn basic grammar directly from simple examples
The Gallery comes from:
Python Basic Training 100 Questions (with Answers) - Know (zhihu.com)
001: Combination of numbers
1, 2, 3, 4, how many different three digits can they make up without repeating numbers?
total=0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if ((i!=j)and(j!=k)and(k!=i)): print(i,j,k) total+=1 print(total)
Learned c, r, matlab, this for loop is really annoying
In python, for loop syntax highlights:
for i in range(a,b): #a b is a comma, not a colon #Add one after: #Do not add end #Must be tab #i=a,a+1,a+2,,,b-1
if
#and not &&
print(a) #not printf ,without ''
A simple method, permutations in itertools
import itertools sum2=0 a=[1,2,3,4]#list #a=(1,2,3,4) #iterable is a tuple #List is the same as tuple output for i in itertools.permutations(a,3): print(i) sum2+=1 print(sum2)
Prototype itertools.permutations(iterable, r=None), returns all mathematical permutations (each permutation exists as a tuple) of an Iterable object whose length is r.
More information: (9 messages) Permutations in itertools implement full and arbitrary permutations_ Zhuyu Blog-CSDN Blog with Mist_ Itertools Full Permutation
002 tax calculations
The bonus awarded by the enterprise shall be based on profit. When profit (I) is less than or equal to 100,000 yuan, the bonus may be increased by 10%; When the profit is higher than 100,000 yuan and less than 200,000 yuan, the part less than 100,000 yuan is deducted by 10%, and the part higher than 100,000 yuan is deducted by 7.5%; Between 200,000 and 400,000 yuan, the portion above 200,000 yuan can be deducted by 5%; If the amount between 400,000 and 600,000 yuan is higher than 400,000 yuan, 3% may be deducted. Between 600,000 and 1,000,000 yuan, the part over 600,000 yuan can be deducted by 1.5%, while the part over 1,000,000 yuan is deducted by 1%. Should the total bonus be paid by entering profit I for the month from the keyboard?
profit = int(input("show me the money")) bonus = 0 thresholds = [0, 100000, 200000, 400000, 600000, 1000000] rates = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01] for i in range(0,6): if (thresholds[i]<=profit)and (profit<=thresholds[i+1]): if i == 0: bonus = profit*rates[i] else: for j in range(0,i): bonus+=(thresholds[j+1]-thresholds[j])*rates[j] bonus+=(profit-thresholds[i])* rates[i] print(bonus)
003 Complete Square Number
An integer, which is a complete square number after adding 100, and 168 is a complete square number. What is the number, please?
for i in range(1,85): if 168 % i ==0: j=168/i if i%2==0 and j%2==0 and i>=j: m=(i+j)/2 n=(i-j)/2 x=n*n-100 print(x)
Analysis:
Assume that the integer is x, x + 100 = nn, x + 100 + 168 = m * m
Calculation equation: m m - n*n = (m+n) (m-n) =168
Settings: m + n = i, M-N = j, I * J = 168, that is, I and j have at least one even number
m = (i+j)/2, n = (i-j)/2, because m and N are integers, I and j are either even or odd.
Derived from 3 and 4, both i and j are even numbers greater than or equal to 2
Because I * J =168, J >= 2, then 1 < I < 168/2 +1
Then all i's are counted in a numerical loop.
--------
https://blog.csdn.net/liuzhuang2017/article/details/85179327
004 What day is it
Enter the day of a month and a year to determine if it is the day of the year?
def isLeapYear(y): return (y%400==0 or (y%4==0 and y%100!=0)) DofM=[0,31,28,31,30,31,30,31,31,30,31,30] res=0 year=int(input('Year:')) month=int(input('Month:')) day=int(input('day:')) if isLeapYear(year): DofM[2]+=1#If it is a leap year plus one day for i in range(month): res+=DofM[i] print(res+day)
def defines functions, basic usage
def<Function name>(parameter): Function Body return Return value
More: (9 messages) [Python] def function [Beginner's Direction]_QAQjiaru233's Blog-CSDN Blog_def function
005 Triple Sort
Enter three integers x,y,z, and output them from small to large.
raw=[] for i in range(3): 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) #The sensation of bubble sorting
python input array method get
append adds an x element at the end of the raw
More: Use of append in Python - Python Tutorials - PHP Chinese Web
Exchange elements: raw[i],raw[j]=raw[j],raw[i]
Direct call to sorted function
raw2=[] for i in range(3): x=int(input('int%d: '%(i))) raw2.append(x) print(sorted(raw2))
Exchange elements: raw[i],raw[j]=raw[j],raw[i]
Or you can call the sorted function directly
raw2=[] for i in range(3): x=int(input('int%d: '%(i))) raw2.append(x) print(sorted(raw2))