A simple python code, some simple python codes

Hello everyone, let me share with you a simple python code. Many people don't know this yet. Let’s explain it in detail below. Now let’s take a look!

Big guys who often write code must have their own very useful code snippets, right?

Although I am not a big boss, I am good at collecting information. Through my efforts, I collected and compiled 10 commonly used Python code snippets from the Internet and shared them with those who need them.

Do you want to ask why Python?

In fact, there is no reason to choose Python because of all the operation symbols in Python .

1. The prospect of Python is very good: Uncle Guido said: He plans to double the speed of CPython when version 3.11 is released in October 2022. Over the next four years, his goal is to make CPython five times faster.

2. The usage of Python is very simple and flexible: its extension library is also very rich, which can meet the needs of many complex scenarios and can replace a lot of manual operations.

3. Python is very cross-platform: no matter how you switch between macOS and Windows, you can have already written programs run directly on the new platform without modifying any line of code.

Okay, no more nonsense.

In short, there is always someone in the workplace who can complete the tasks assigned by the boss without working overtime, so why can't that person be you?

Therefore, please save the 10 commonly used Python code snippets compiled today. If you think they are useful enough, remember to share them with your friends and colleagues~

1Exchange the values ​​of two variables
num_1, num_2 = 666, 999

# 一行代码搞定交换两个变量的值

num_1, num_2 = num_2, num_1

print(num_1, num_2)

Output:

999 666

Process finished with exit code 0
2Find the memory used by an object
import sys

slogan = "今天你学python了么?"

size = sys.getsizeof(slogan)

print(size)

Output:

100

Process finished with exit code 0
3Reverse a string
slogan = "今天你学习python了么?"

# 一行代码搞定字符串的反转

new_slogan = slogan[::-1]

print(new_slogan)

Output:

?么了nohtyp习学你天今

Process finished with exit code 0
4 Check if a string is a palindrome
# 定义一个判断字符串是否是回文的函数

def is_palindrome(string):

    return string == string[::-1]

Example: Call the judgment function to judge whether the slogan is a palindrome string

slogan = "今天你学python了么?"

_ = is_palindrome(slogan)

print(_)

Output:

False

Process finished with exit code 0
5 Combine lists of strings into a single string
slogan = ["今", "天", "你", "学", "python", "了", "么", "?"]

# 一行代码搞定将字符串列表合并为单个字符串

real_slogan = "".join(slogan)

print(real_slogan)

Output:

今天你学python了么?

Process finished with exit code 0
6Find elements that exist in either list
# 定义一个函数用来查找存在于两个列表中任一列表存在的元素

def union(list1, list2):

    return list(set(list1 + list2))

Example: Call this function to find elements that exist in either list

list1, list2 = [5, 2, 0], [5, 2, 1]

new_list = union(list1, list2)

print(new_list)

输出:

[0, 1, 2, 5]

Process finished with exit code 0
7 Print a string N times
slogan = "今天你学python了么?"

new_slogan = 11*slogan

print(new_slogan)

Output:

今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?

Process finished with exit code 0
8 chain comparison
number = 100

print(98<number<102)

Output:

True

Process finished with exit code 0

print(100==number<102)

Output:

True

Process finished with exit code 0
9 word case
slogan = "python happy"

# 一行代码搞定单词大小写转换

print(slogan.upper())

# 一行代码搞定单词首字母大写

print(slogan.capitalize())

# 一行代码搞定将每个单词的首字母转为大写,其余小写

print(slogan.title())

Output:

PYTHON HAPPY

Python happy

Python Happy

Process finished with exit code 0
10 Count the frequency of elements in a list
from collections import Counter

numbers = [1, 1, 3, 2, 4, 4, 3, 6]

# 一行代码搞定求列表中每个元素出现的频率

count = Counter(numbers)

print(count)

Output:

Counter({1: 2, 3: 2, 4: 2, 2: 1, 6: 1})

Process finished with exit code 0

The above are 10 simple and practical python codes that I shared. I hope it can be helpful to everyone!

PS: The space is limited. You can use WeChat to scan the official CSDN QR code below. There are carefully prepared free python materials~           

                              

In addition, it is not new for programmers to learn Python. Many people even learn Python as their first language. No wonder, Python has so many advantages. It has a simple language, high development efficiency, strong portability, and can be easily and seamlessly connected with other programming languages ​​​​(such as C++).

Moreover, if you learn Python well and then become a Python programmer crawler, you can smoothly transform into data analysis, data mining, artificial intelligence, deep learning and other directions.

It can be said that all roads lead to Rome.

But while Python is easy to learn, it is difficult to master. It seems very simple, but it is not easy to learn. Do friends who are new to python always have such questions?

Don’t know where to start if you have no basic knowledge?

Self-study from scratch, what materials can you recommend?

I am already working and am getting a bit older. Is it still too late to learn python by myself?

          ......

Friends who have these questions, as well as friends who want to learn python or are interested in python, can read here. Here I will share a free learning gift package with you and let you learn together. I hope this information can help everyone!

1. Summary of Python learning routes

The technical points in all directions of Python are organized to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.

2. Python essential development tools

3. Excellent Python learning books

4. Python video collection of 600

5. Practical cases

6.100 Python exercises

7. Interview questions

This complete version of the complete set of Python learning materials has been uploaded to CSDN. If you need it, friends can scan the CSDN official certification QR code below on WeChat [free access]

                                 

Guess you like

Origin blog.csdn.net/mynote/article/details/133548846
Recommended