Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common magic commands in Python

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces to you what commonly used magic commands in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1% lsmagic: find all commands

If you only remember one magic order, it has to be this one. Executing the% lsmagic command provides a list of all available magic commands:

2% debug: interactive debug

This is probably the most common magic command I use.

Most data scientists have encountered this situation: the block of code executed is break all the time, and you write 20 print () statements in despair, trying to output the contents of each variable. Then, when you finally fix the problem, you have to go back and delete all the print () statements again.

But you don't have to do that anymore. When you encounter a problem, simply execute the% debug command to execute any part of the code you want to run:

What happened in the picture above?

We have a function that takes a list as input and squares all even numbers.

We ran the function, but something went wrong. But we don't know what happened!

Use the% debug command for this function.

Let the debugger tell us the values of x and type (x).

The problem is obvious: we input'6' into the function as a string!

This is very useful for more complex functions.

3% store: passing variables between notebook

That's a cool order, too. Suppose you spend some time cleaning the data in notebook, and now you want to test some functionality in another notebook, do you implement this function in the same notebook, or do you save the data and load it in another notebook? None of these operations are required after using the% store command! This command will store the variable, which you can retrieve in any other notebook:

% store [variable] stores variables.

% store-r [variable] reads / retrieves storage variables.

4% who: lists all global variables.

Have you ever forgotten a variable name after assigning a value to it? Or accidentally delete the cell responsible for assigning values to variables? Using the% who command, you can get a list of all global variables:

5% time: timing magic command

Use this command to get all timing information. Simply apply the%% time command to any executable code and you will get the following output:

6% writefile: write cell contents to the file

This magic command is useful when you write a complex function or class in notebook and want to save it to an exclusive file. Simply add the%% writefile prefix to the cell of the function or class and the file name you want to save to:

As shown above, we can save the created function to a utils.py file, and then we can import it at will. You can do the same in other notebook, as long as you belong to the same directory as the utils.py file.

About which commonly used magic commands in Python are shared here, I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report