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 ways to improve the work efficiency of Python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly talks about "what are the ways to improve the efficiency of Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the ways to improve the efficiency of Python?"

Run external files

When you try to run some code snippets in JupyterNotebook, you want to run external code files located in a directory. % run allows any external python file to be run from Jupyter Notebook.

The above file myCode.py contains a simple script that outputs the above statement.

If you specify a file name that contains the% run command path, it will execute the file.

Note: the% run also allows the execution of external Jupyter Notebook.

Code execution time

Have you ever wondered how long it will take to run the cell? the Time magic command allows you to track the total execution of the cell. Since the entire cell will be processed here,%% is used as the prefix before the time keyword.

The above cell includes a for loop with random calculations. Time helps to obtain the time required to run the for loop.

Copy the content to an external file

Most of the time, it is necessary to add content directly from JupyterNotebook to a python script or text file. Instead of copying everything and creating a new file, you can export the unit content directly by adding the writefile command before the code.

Note that the double% in front of the command indicates that the entire contents of the unit will be exported.

Because this file has been created with some content, it displays "OverwritemyCode.py". Specifies that it will overwrite the original content with what is shown in the image above.

List all variables

This magic command shows all the variables used throughout notebook. Here are three variables-- two strings and one integer. If you run% who, it lists all three variables defined.

A = "hello" b = "Good Morning" c = 1

The above code shows all the variables, regardless of their data type.

In order to display specific data type variables, you need to pass the data type after the magic command. The above code displays all string data type variables as their output.

Sharing variables between notebook

This magic command allows any variable to be shared between different JupyterNotebook. You need to use magic commands to pass the original variables. To get this variable, you need to pass the same command with the "- r" argument.

This is what the first notebook looks like:

The code needed to get this data is written in another notebook.

This is probably the easiest way to share data of any data type between different notebook.

Display the contents of an external file

You usually need to copy a few lines of code from an external file into your code. % pycat allows the contents of any file to be displayed in any directory, rather than going through a lengthy process to get the file and open it for copying.

It displays all the contents of the external file as its output. In terms of its application, it can be seen as the reverse of% writefile.

Execute html script

Html allows html code to be written in a unit. Now, the unit will act as a html editor, outputting the html of the unit.

The following code contains a simple table created with html. Notice that the html output shows the expected table.

Html Name Country Age Sid India 22 Dave UK 28

Tip: you can use the%% jsmagic command similar to the HTML magic command to run Javascript code in the unit.

Show Matplotlib diagram

The% matplotlibinline magic command is the most popular command. This command allows Jupyternotebook to display matplotlib graphics in notebook. This command activates matplotlib's interactive support for Jupyter Notebook.

Import random import matplotlib.pyplot as plt matplotlib inline

Some libraries that need to explain command functionality have been imported.

You will now create two random lists to draw the graph:

A = [] b = [] for i in range (10): a.append (random.randint (0prime10)) b.append (random.randint (0mem10))

You will now draw a scatter plot of the data.

The% matplotlibinlin magic command allows visualization of graphics in Jupyter Notebook.

Set environment variabl

This magic command can do three things-list all environment variables, get the value of a specific environment variable, and set a value for a variable.

% env with no parameters lists all environment variables.

% env with a single parameter returns the value of the specified parameter.

% env variable value: the variable sets the value of the specified variable name.

Details of the object

% pinfo provides details of the objects passed with it. It is similar to the function of objects.

In the following code snippet, you pass a simple string a with% pinfo to get its details.

A = "The World Makes Sense!"% pinfo a

In the above output,% pinfo provides all the information about the string object.

You can use the% lsmagic command to find a list of all magic commands.

At this point, I believe you have a deeper understanding of "what are the ways to improve the efficiency of Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report