Table of Contents
1. We can assign values ​​to multiple variables through lists, as long as the number of list elements corresponds to the number of variables.
2. _ in interactive mode represents the output of the previous expression.
3. Link the elements in the list through the join operation.
4. Reversing a list should be a basic common operation. Here are two common methods to reverse the list through the reverse function or slicing.
5. Although computer hardware is now very cheap, it does not mean that we do not need to consider memory usage when writing programs. We can obtain the memory usage of variables through getsizeof of the sys module.
6. Printing a string multiple times is also a commonly used function of a table. It is often used during debugging. It is especially useful for making dividing lines.
7. Change the first letter of the string to uppercase.
8. Convert two lists with the same number of elements into dictionaries.
9. Set the return default value of the dictionary, somewhat similar to the map.getOrDefault() function in Java.
10. Finally, let me share with you the dictionary/set comprehension. This is one of the most useful functions of Python.
Summary
Home Backend Development Python Tutorial Python super practical tips to improve work efficiency Max

Python super practical tips to improve work efficiency Max

May 10, 2023 pm 03:19 PM
python Skill max

Python 超实用小技巧,提升工作效率 Max

As we all know, Python is famous for its simplicity and ease of use. In the face of complex and trivial repetitive work, sometimes it is necessary to refine some small scripts to handle the repetitive work. Today, Pison Jiang will give you We have compiled ten very practical tips that can be run directly. It is recommended to collect them.

1. We can assign values ​​to multiple variables through lists, as long as the number of list elements corresponds to the number of variables.

In [1]: list = [1, 2, 3]
In [2]: a, b, c = list
In [3]: print(a, b, c)
1 2 3
Copy after login

2. _ in interactive mode represents the output of the previous expression.

In [4]: 2 * 3
Out[4]: 6
In [5]: _
Out[5]: 6
Copy after login
In [6]: words = ['I', 'Like', 'Python']
In [7]: ' '.join(words)
Out[7]: 'I Like Python'
Copy after login

4. Reversing a list should be a basic common operation. Here are two common methods to reverse the list through the reverse function or slicing.

In [8]: words = ['I', 'Like', 'Python']
In [9]: words.reverse()
In [10]: words
Out[10]: ['Python', 'Like', 'I']
In [11]: words[::-1]
Out[11]: ['I', 'Like', 'Python']
Copy after login

5. Although computer hardware is now very cheap, it does not mean that we do not need to consider memory usage when writing programs. We can obtain the memory usage of variables through getsizeof of the sys module.

In [12]: import sys
In [13]: value = 100
In [14]: sys.getsizeof(value)
Out[14]: 28
Copy after login

6. Printing a string multiple times is also a commonly used function of a table. It is often used during debugging. It is especially useful for making dividing lines.

In [15]: '#' * 20
Out[15]: '####################'
0x06
Copy after login

7. Change the first letter of the string to uppercase.

In [16]: word = 'python'
In [17]: word.title()
Out[17]: 'Python'
Copy after login

8. Convert two lists with the same number of elements into dictionaries.

In [18]: keys = ["a", "b", "c"]
In [19]: values = [1, 2, 3]
In [20]: dict(zip(keys, values))
Out[20]: {'a': 1, 'b': 2, 'c': 3}
Copy after login

9. Set the return default value of the dictionary, somewhat similar to the map.getOrDefault() function in Java.

In [23]: d = {'a': 1, 'b': 2}
In [24]: d.get('a')
Out[24]: 1
In [25]: d.get('c')
In [26]: d.get('c', 300)
Out[26]: 300
Copy after login

10. Finally, let me share with you the dictionary/set comprehension. This is one of the most useful functions of Python.

In [27]: values = {i : i*i for i in range(5)}
In [28]: values
Out[28]: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
In [29]: nums = {i*i for i in range(5)}
In [30]: nums
Out[30]: {0, 1, 4, 9, 16}
In [31]: nums = [i for i in range(5)]
In [32]: nums
Out[32]: [0, 1, 2, 3, 4]
Copy after login

Summary

Today I will share with you some common practical tips in Python, use them quickly. What other tips do you know about Python? You can communicate with everyone in the comment area.

The above is the detailed content of Python super practical tips to improve work efficiency Max. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles