Get the App
SLTechnology News&Howtos  ›  Development  › 

Summarize the small examples of using Python

Shulou Source: shulou.com Published: 2022-06-03 03:12:27 10月01日 Update

This article focuses on "summing up small examples of the use of Python", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "summing up the small examples of using Python"!

Example 031: letter recognition

Title:

Please enter the first letter of the week to determine what day it is, and if the first letter is the same, continue to judge the second letter.

Program analysis: here the control relationship is saved directly in the form of a dictionary.

WeekT= {'t':weekT t':weekT,' s':weekS, 'etc. A=week [str (input ('Please enter the first letter:). Lower ()] if a==weekT or a==weekS: print (a [str (input (' Please enter the second 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]) instance 033: list to string

Title:

Separate the list by comma.

L = [1 print 2 3 pm 4 5] print (', '.join (str (n) for n in L)) instance 034: call function

Title:

Practice function calls.

Def hello (): print ('Hello wordstones') def helloAgain (): for i in range (2): hello () if _ _ name__=='__main__': helloAgain () instance 035: set 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 + "warning color font?" + bcolors.ENDC) example 036: arithmetic element

Title:

Find primes within 100.

Program analysis: use else to execute the reward code of the for loop (if the for is finished normally, not break).

Lo=int (input ('lower bound:') hi=int (input ('upper limit:') for i in range (lo,hi+1): if I > 1: for j in range (2PowerI): if (I% j) = 0: break else: print (I) instance 037: sort

Title:

Sort 10 numbers.

Program analysis: same as example 0.05.

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 (iLen (raw)): if raw [I] > raw [j]: raw [I], raw [j] = raw [j], Raw [I] print (raw) example 038: sum of diagonals of a matrix

Title:

Find the sum of the principal diagonal elements of a 3-3 matrix.

Mat= [[1Jing 2 print 3], [3 Jing 4 Jing 5], [4 Jing 5 Jo 6]] res=0for i in range (len (mat)): res+=mat [I] [I] print (res) instance 039: insert elements in an ordered list

Title:

There is an array that has been sorted. Now enter a number and ask it to be inserted into the array according to the original rule.

Program analysis: first determine whether this number is greater than the last number, and then consider the case of inserting the middle number, after the insertion of the number after this element, move back one position in turn.

Lis=] n=int (input ('insert a number:') lis.append (n) for i in range (len (lis)-1): if lis [I] > = n: for j in range (lis): lis [j], lis [- 1] = lis [- 1], lis [j] breakprint (lis) example 040: reverse list

Title:

Outputs an array in reverse order.

Program analysis: switch positions in turn, or call the reverse method directly.

Lis=] for i in range (int (len (lis) / 2): lis [I], lis [len (lis)-1kizi] = lislen (lis)-1Kui], lisi [I] print ('first implementation:') print (lis) lis= [1mie 10mei 100pm 100000] print ('second implementation:') lis.reverse () print (lis) example 041: class methods and variables

Title:

Imitate the use of static variables.

Program analysis: construct classes and understand the methods and variables of classes.

Def dummy (): iTun0 print (I) i+=1class cls: iTun0 def dummy (self): print (self.i) self.i+=1a=cls () for i in range (50): dummy () a.dummy () instance 042: variable scope

Title:

Learn how to use auto to define variables.

Program analysis: variable scope in python.

I=0n=0def dummy (): iExten0 print (I) i+=1def dummy2 (): global n print (n) n+=1print ('variable of the same name within the function') for j in range (20): print (I) dummy () i+=1print ('global declares variable of the same name') for k in range (20): print (n) dummy2 () nroom10 instance 043: scope, methods and variables of classes

Title:

Another case of imitating static variables (static).

Program analysis: comprehensive example 041 and example 042.

Class dummy: num=1 def Num (self): print ('class dummy num:',self.num) print (' global num:', num) self.num+=1n=dummy () num=1for i in range (5): num*=10 n.Num () example 044: Matrix addition

Title:

Calculate the addition of two matrices.

Program analysis: create a new matrix, iterate with for and take out the values of the corresponding positions in the X and Y matrices, add them to the corresponding positions of the new matrix.

X = [[12pence7, 6], [7, 8]] Y = [5, 8]], [6], [6], [4] res= [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) instance 045: summation

Title:

Count the sum of 1 to 100.

Res=0for i in range (1101): res+=iprint (res) instance 046: break the cycle

Title:

Find the square of the input number and exit if it is less than 50 after the square operation.

While True: try: n=float (input ('enter a number:') except: print ('input error') continue dn=n**2 print ('squared is:', dn) if dn=y) + y * (y > x) Min=lambda x

Tags: Example title variable program analysis matrix input function letter method output position number example number sum function element array color Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Linux Huawei Apple