python small examples
1.0.0
Say goodbye to boring, say goodbye to boring, and devote ourselves to creating classic small examples and cases of Python.
Reprinting is allowed upon request, but any commercial use is prohibited. If you reprint small examples and cases from this library, please note the link below:
Summary of all Python small examples
AI news
AI news coverage
AI large model
AI toolset
Six ways to use ChatGPT4o for free
Python tutorial
Data Analysis Tutorial
Algorithm Tutorial
AI Tutorial
Git tutorial
programmer
Data download
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | Common arithmetic operations | Operation | v1 | |
2 | Implement relu | max | V4.0 | ️️ |
3 | Base conversion | bin,oct,hex | V4.0 | ️️ |
4 | Convert integers to ASCII | chr,ord | V1.0 | ️️ |
5 | Check if elements are all true | all | V2.0 | ️️️ |
6 | at least one is true check | any | V2.0 | ️️️ |
7 | Determine whether it is true or false | bool | V2.0 | ️️️ |
8 | Create plurals | complex | V1.0 | ️️️ |
9 | Get quotient and remainder | divmod | V1.0 | ️️ |
10 | Convert to floating point type | float | V1.0 | ️️ |
11 | Convert to integer | int | V1.0 | ️ |
12 | Power | pow | V1.0 | ️ |
13 | rounding | round | V1.0 | ️ |
14 | chained comparison | compare | V1.0 | ️️ |
15 | String to byte | bytes,utf-8 | V1.0 | ️️ |
16 | Convert any object to string | str | V1.0 | ️️ |
17 | Execute the code represented by the string | compile | V1.0 | ️️️ |
18 | Calculation expression | eval | V1.0 | ️️️️ |
19 | String formatting | format | V1.0 | ️️️️ |
20 | Swap two elements | pack,unpack | V1.0 | ️️ |
twenty one | Convert to dictionary | dict | V1.0 | ️️ |
twenty two | freeze collection | frozenset | V1.0 | ️️ |
twenty three | Convert to collection type | set | V1.0 | ️️ |
twenty four | Convert tuple | tuple | V1.0 | ️️ |
25 | Check the number of bytes occupied by the variable | getsizeof | V1.0 | ️️️ |
26 | Tuple containing a single element | tuple | V1.0 | ️ |
27 | Pitfalls of list deletion | list | V1.0 | ️ |
28 | The Pitfalls of Quick Copying of Lists | list | V1.0 | ️ |
29 | Find the first 3 largest or smallest numbers in a list | list heapq | v1.0 | ️ |
30 | string resident | str | V1.0 | ️ |
31 | Create empty collection error | set | V1.0 | ️ |
32 | Fully understand for | for | V1.0 | ️ |
33 | Know when to execute | generator | V1.0 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | Operation function object | operator | V2.0 | ️️️️ |
2 | Create a range sequence | range | V1.0 | ️️ |
3 | Generate reverse sequence | range | V1.0 | ️️ |
4 | Ready-to-use sorting function | sorted | V1.0 | ️️️ |
5 | summation function | sum | V1.0 | ️️ |
6 | Examples of using five types of parameters of functions | variable parameter | V2.0 | ️️️️ |
7 | Use the slice object | slice | V2.0 | ️️️️️ |
8 | lambda function | lambda | V3.0 | ️️️️ |
9 | enumeration object | enumerate | V1.0 | ️️️ |
10 | filter filter | filter | V1.5 | ️️️ |
11 | Return object hash value | hash | V1.0 | ️️ |
12 | tuple with name | namedtuple | V1.0 | ️️️ |
13 | One line of code to reverse a list | reverse | V1.0 | ️️ |
14 | Two ways to reverse a string | reversed | V1.0 | ️️ |
15 | join concatenates strings | join | V1.0 | ️️ |
16 | String length in bytes | encode | V1.0 | ️️ |
17 | groupby single field grouping | itertools, groupby, lambda | V1.0 | ️️️ |
18 | groupby multi-field grouping | itemgetter,itertools,groupby | V1.0 | ️️️️ |
19 | itemgetter and key functions | operator,itemgetter,itertools | V1.0 | ️️️️️ |
20 | The sum function calculates and aggregates at the same time | sum,generator | V1.0 | ️️️️️ |
twenty one | Default parameters are set to empty | function | V1.0 | ️ |
twenty two | Pitfalls in using various parameters | function parameter | V1.0 | ️ |
twenty three | The pitfall of lambda free parameters | lambda | V1.0 | ️ |
twenty four | Use heap ascending list | sort heapq | v1.0 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | root of all objects | object | V1.0 | ️ |
2 | Is the object callable? | callable | V2.5 | ️️️️ |
3 | ascii display object | __repr__ | V2.5 | ️️️ |
4 | class method | classmethod | V1.5 | ️️️ |
5 | Dynamically delete attributes | delattr,hasattr | V1.5 | ️️ |
6 | View all methods of an object with one click | dir | V1.5 | ️️ |
7 | Dynamically obtain object properties | getattr | V1.5 | ️️ |
8 | Whether the object has a certain attribute | hasattr | V1.5 | ️️️ |
9 | Object house number | ID | V1.0 | ️ |
10 | Instance and object relationship judgment | isinstance | V1.5 | ️️️ |
11 | issubclass parent-child relationship identification | issubclass | V1.5 | ️️️ |
12 | Two ways to create properties | property | V2.5 | ️️️️️ |
13 | View object types | type | V1.0 | ️ |
14 | Introduction to using metaclasses | type, __class__ | V2.0 | ️️️️️ |
15 | Immutable objects of the same value | mutable | V1.0 | ️ |
16 | Object destruction order | OOP del | V1.0 | ️ |
17 | Do subclasses inherit static methods from parent classes? | staticmethod | V1.0 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | The role of the character r in regular expressions | re, r | V3.0 | ️️️ |
2 | Regular atomic operations | re | V3.0 | ️️️ |
3 | Escapes in regular expressions | re, | V3.0 | ️️️ |
4 | The most common regular search | re,findall | V3.0 | ️️️ |
5 | Find using universal characters | re,s,w,d | V3.0 | ️️️ |
6 | Search using metacharacters | re,+,* | V3.0 | ️️️ |
7 | capture substring | () | V3.0 | ️️️️ |
8 | Greedy capture and non-greedy capture | re | V1.0 | ️️️️ |
9 | Use regular rules to do password security checks | re | V1.0 | ️️️️️ |
10 | Crawl Baidu homepage title | re | V1.0 | ️️️️ |
11 | Batch conversion to camel case format (Camel) | re | V1.0 | ️️️️️ |
12 | Use regular expression to determine whether it is a positive floating point number | str,re,float | V1.0 | ️️️️️ |
13 | Use regular expressions to extract positive integers and floating point numbers greater than 0 | re findall | v2 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | A simple understanding of decorators | decorator | V1.0 | ️️️ |
2 | Decorator for testing function runtime | decorator | V1.0 | ️️️️ |
3 | Count exception decorator | decorator,nonlocal | V1.5 | ️️️️ |
4 | Custom descending iterator | Iterator | V3.0 | ️️️️ |
5 | Create iterator | iter, __iter__ | V1.5 | ️️️ |
6 | Reverse iterator reversed | reversed | V1.0 | ️️ |
7 | zip iterator | zip | V1.5 | ️️️ |
8 | list grouping (generator version) | yield,generator | V1.0 | ️️️ |
9 | Full list expansion (generator version) | list,yield,generator | V1.0 | ️️️ |
10 | chain small containers into large containers | itertools,chain | V1.0 | ️️️️️ |
11 | product use case | product | V1.0 | ️️️️️ |
12 | The first n terms of the Fibonacci sequence | yield, range | V1.0 | ️️️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | Turtle draws the Olympic rings | turtle | V1.0 | ️️️ |
2 | turtle draws snowflakes in the sky | turtle | V1.0 | ️️️ |
3 | Python word cloud chart | WordCloud | V1.0 | ️️ |
4 | Plotly bar charts and line charts | plotly | V1.0 | ️ |
5 | seaborn heat map | seaborn | V1.0 | ️ |
6 | Pyecharts dashboard | pyecharts | V1.0 | ️ |
7 | Pyecharts Funnel Chart | pyecharts | V1.0 | ️ |
8 | Pyecharts Water Polo Chart | pyecharts | V1.0 | ️ |
9 | Pyecharts Pie Chart | pyecharts | V1.0 | ️ |
10 | Pyecharts polar plot | pyecharts | V1.0 | ️ |
11 | Pyecharts word cloud chart | pyecharts | V1.0 | ️ |
12 | Pyecharts heatmap | pyecharts | V1.0 | ️ |
13 | matplotlib draws animations | matplotlib | V1.0 | ️ |
14 | seaborn pairplot diagram | seaborn | V1.0 | ️ |
15 | pyecharts fails to pass in Numpy data drawing | numpy pyecharts | V1.0 | ️ |
16 | Image processing package pillow | pillow | V1.0 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | Data analysis artifact: deepnote | deepnote | v1.0 | ️ |
2 | NumPy pad filling method | NumPy pad | V1.0 | ️ |
3 | Create a diagonal matrix with lower diagonals 1, 2, 3, and 4 | NumPy diag | V1.0 | ️ |
4 | cut data binning | Pandas cut | v1.0 | ️ |
5 | Discard null values and fill null values | Pandas dropna fillna | v1.0 | ️ |
6 | The apply method removes special characters | pandas apply | v1.0 | ️ |
7 | Use map to perform feature engineering on columns | pandas map | v1.0 | ️ |
8 | Convert category column to numerical value | pandas category | v1.0 | ️ |
9 | rank ranking | pandasrank | v1.0 | ️ |
10 | Complete data downsampling and adjust the step size from hours to days | pandas resample | v1.0 | ️ |
11 | How to quickly generate time series data with Pandas | pandasutil | v1.0 | ️ |
12 | How to quickly find the number of null values in all columns of a DataFrame | pandas isnull sum | v1.0 | ️ |
13 | Reorder columns of DataFrame | pandas dataframe | v1.0 | ️ |
14 | Use count to count the number of occurrences of a term | pandas count | v1.0 | ️ |
15 | split finds the minute difference between hours and minutes (HH:mm) | pandas split | v1.0 | ️ |
16 | melt pivot data tips | pandasmelt | v1.0 | ️ |
17 | pivot perspective tips | pandasmelt | v1.0 | ️ |
18 | p randomly reads K lines of the file and generates N | pandas sample | v1.0 | ️ |
19 | Formatting time column in Pandas | pandas apply | v1.0 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | help one-click help | help | V1.0 | ️ |
2 | Get user input | input | V1.0 | ️ |
3 | File reading and writing and mode value table | open,read,write,with,mode | V2.0 | ️️️ |
4 | Examples of using operator | operator | V1.0 | ️️️️ |
5 | Transfer json object | json | V2.0 | ️️️️️ |
6 | Get file extension | os,splitext | V1.0 | ️️ |
7 | Get the file name in the path | os,split | V1.0 | ️️ |
8 | Modify file suffixes in batches | argparse,listdir | V1.0 | ️️️️ |
9 | Convert xls to xlsx in batches | os,listdir,splitext | V1.0 | ️️️️ |
10 | Get the file with the specified suffix name | os,listdir,splitext | V1.0 | ️️️️ |
11 | Batch compress files | zipfile | V1.0 | ️️️️ |
12 | 32-bit encryption | hashlib | V1.0 | ️️️️ |
13 | year calendar chart | calendar | V1.0 | ️️ |
14 | Determine whether it is a leap year | calendar | V1.0 | ️️️ |
15 | Determine how many days there are in the month | calendar,datetime | V1.0 | ️️️ |
16 | first day of month | datetime | V1.0 | ️️ |
17 | last day of month | calendar,datetime | V1.0 | ️️ |
18 | Get current time | time,datetime | V1.0 | ️️ |
19 | Convert character time to time | time,datetime | V1.0 | ️️ |
20 | time to character time | time,datetime | V1.0 | ️️ |
twenty one | 1~n days after getting a certain day | Calendar,monthrange | V4.0 | ️️️ |
twenty two | Start main thread by default | threading | V1.0 | ️️ |
twenty three | Create thread | threading | V1.0 | ️️ |
twenty four | Alternately obtain CPU time slices | threading | V1.0 | ️️️ |
25 | Multiple threads grab the same variable | threading | V1.0 | ️️️ |
26 | Problems caused by multi-thread variable competition | threading | V1.0 | ️️️ |
27 | multithread lock | threading,lock | V1.0 | ️️️ |
28 | Time to array and common formats | time,datetime,format | V1.0 | ️️️ |
29 | nonlocal is used in inline functions | nonlocal | V2.0 | ️️️️️ |
30 | global declares global variables | global | V2.0 | ️️️️️ |
31 | The pitfall of unbound shared variables | global | V1.0 | ️ |
32 | Optimize code exception output package | debugger | V1.0 | ️ |
33 | One line of code to find the encoding | chardet | V1.0 | ️ |
34 | Create SQLite connection | SQLite | v1.0 | ️ |
35 | json object to python object | python json | v1.0 | ️ |
36 | Convert python object to json object | python json | v1.0 | ️ |
37 | One line of code speeds up pip installation 100 times | pip install | v1.0 | ️ |
Small example | Link | Label | Version | difficulty |
---|---|---|---|---|
1 | Implement a calculator without else and if | operator | V1.0 | ️️️ |
2 | Go for the average | list,sort,round | V1.0 | ️️️️ |
3 | Print 99 multiplication table | for,range,format | V1.0 | ️️️ |
4 | Recursive version of flatten function | recursion,list,isinstance | V1.0 | ️️️️ |
5 | The list is divided into n equal parts | list,ceil | V1.0 | ️️️ |
6 | compressed list | list,filter | V1.0 | ️️️️ |
7 | Ask for a longer list | max, lambda | V1.0 | ️️️️️ |
8 | Find the mode of a list | max, lambda, count | V1.0 | ️️️️ |
9 | Maximum value of all multiple lists | max, lambda | V1.0 | ️️️️ |
10 | List check for duplicates | set | V1.0 | ️️️ |
11 | Arithmetic sequence of floating point numbers | range, float | V1.0 | ️️️️ |
12 | Group by condition | lambda | V1.0 | ️️️️ |
13 | map implements vector operations | map,lambda | V1.0 | ️️️ |
14 | Dictionary with the largest value | max, lambda | V1.0 | ️️️️ |
15 | Merge two dictionaries | ** | V1.0 | ️️️ |
16 | Topn dictionary | heapq,nlargest | V1.0 | ️️️ |
17 | Determine whether it is an anagram | collections, Counter | V1.0 | ️️️ |
18 | Logically merge dictionaries | ChainMap | V1.0 | ️️️️️ |
19 | sample sample sampling | random,sample | V1.0 | ️️️ |
20 | Rewash the dataset | shuffle | V1.0 | ️️️ |
twenty one | 10 uniformly distributed coordinate points | random, uniform | V1.0 | ️️️ |
twenty two | 10 coordinate points of Gaussian distribution | random,gauss | V1.0 | ️️️️ |
twenty three | Whether they are sorting words for each other | collections,defaultdict | V1.0 | ️️️️ |
twenty four | Whether str1 is rotated from str2 | str | V1.0 | ️️️ |
25 | Find the nth occurrence of position | enumerator | V1.0 | ️️️ |
26 | Find all duplicate elements | calendar,datetime | V1.0 | ️️️️ |
27 | joint statistics | Counter | V1.0 | ️️️️️ |
28 | Find the spherical distance between two points | math asin | V1.0 | ️️️️️ |
29 | Get file encoding | chardet | V1.0 | ️️️️️ |
30 | Format json string | json | V1.0 | ️️️️️ |