30 simple Python codes that are commonly used, just use them and collect them!

How to learn Python is the fastest, of course, is to fight various small projects. Only when you think and write yourself can you remember the rules. This article is 30 minimal tasks, beginners can try to achieve it by themselves; this article is also 30 pieces of code, Python developers can also see if there are unexpected usages.

Duplicate element determination

The following method can check whether there are duplicate elements in a given list. It will use the set() function to remove all duplicate elements.

Character element composition judgment

Check if the elements of the two strings are the same.

Memory footprint

Byte occupied

The following code block can check the number of bytes occupied by a string.

Print string N times

This code block can print the string N times without loop statements.

Capitalize the first letter

The following code block uses the title() method to capitalize the first letter of each word in the string.

Block

Given a specific size, define a function to cut the list according to this size.

compression

This method can remove boolean values, such as (False, None, 0, ""), and it uses the filter() function.

Unpack

The following code snippet can unpack the packed paired list into two different tuples.

Chain comparison

We can use different operators in a line of code to compare multiple different elements.

Comma connection

The following code can concatenate the list into a single string, and set the separation between each element to a comma.

Vowel Statistics

The following method will count the number of vowels ('a','e','i','o','u') in a string, and it is done through regular expressions.

Lowercase first letter

The following method will unify the first character of a given string to lowercase.

Expand list

This method will recursively expand the nesting of the list into a single list.

List difference

This method will return the elements of the first list, which are not in the second list. If you want to feed back the unique elements of the second list at the same time, you also need to add a sentence of set_b.difference(set_a).

Difference by function

The following method first applies a given function, and then returns the list elements with different results after applying the function.

Chain function call

You can call multiple functions in one line of code.

Check for duplicates

The following code will check if there are duplicates in the two lists.

Merge two dictionaries

The following method will be used to merge two dictionaries.

In Python 3.5 or higher, we can also merge dictionaries in the following ways:

Convert two lists into dictionaries

The following method will convert two lists into a single dictionary.

Use enum

We often use For loop to traverse a list, and we can also enumerate the index and value of the list.

execution time

The following code block can be used to calculate the time it takes to execute a specific code.

Try else

We can also add an else clause when using the try/except statement. If no error is triggered, this clause will be executed.

Element frequency

The following method will take the most common elements in the list based on the element frequency.

Palindrome sequence

The following method will check whether the given string is a palindrome sequence. It will first convert all letters to lowercase and remove non-English letter symbols. Finally, it compares whether the string is equal to the reversed string, which is represented as a palindrome sequence. If you are interested in Python, you can add the teacher's WeChat: abb436574, get a set of learning materials and video courses for free~

Is not used if-else calculation sub

This piece of code can implement addition, subtraction, multiplication, division, and exponentiation operations without using conditional statements. It is implemented through the dictionary data structure:

Shuffle

This algorithm will disrupt the order of the list elements, it will mainly sort the new list through the Fisher-Yates algorithm:

Expand all elements in the list, including sub-lists, into a list.

Exchange value

No additional operations are required to exchange the values ​​of two variables.

Dictionary default

Take the corresponding Value value through Key, and set the default value in the following ways. If the get() method does not set a default value, it will return None if it encounters a non-existent Key.

Guess you like

Origin blog.csdn.net/weixin_45820912/article/details/108405449