Python made its first revolutionary appearance in 1991 and caused a lot of concern among programmers. People would also call it a risky programming language. However, that mindset changed throughout the years. Eventually, it became the main language for use in software development and machine learning. Here are 5 easy commands that will make you love Python even more.
Web developers consider Python popular because of its magic commands. These are the shortcuts that make this programming language easy to navigate. Moreover, they allow you to come up with viable solutions when it comes to standard data analysis.
Magic commands are in two forms: line magic and cell magic. Line magic operates on a single line of input and has only one prefix. Whereas, cell magic operates on several lines of input and has two prefixes.
Run an External Script
For example, you have been working on a new project and want to use the previous script. With an easy command in Python called run you can do magic. You don’t need to import any files or variables. By using the right line of code, you can provide the already-defined variables. As a result, all the variables from a previous file can be accessed on a new file.
Import or View an External Script
What if you need to copy the script into a notebook cell? No need to worry. The load magic function helps to import a script straight into the code cell. In addition, pycat is a similar function that allows viewing the file content without leaving.
Code Execution Time
You can calculate the time that you need to do a Python expression by using timeit. This magic command executes several runs of the expression, which makes the results more reliable.
Environment Variables
You can manipulate the system environment variables with the Python command called env. There are three scenarios. First, you can simply list the environment variables with env. Second, you can get value for a variable with env var. Thirdly, you can set values for variables with env var value. In case you do not add any parameter, the Python magic command will list all the environment variables.
List Variables in the Namespace
There are three commands that will help you list the variables in the namespace. First, if you want to print all the interactive variables with less formatting, then use who. Second, when you want to return all variables as a sorted list, use who_ls. The third command is whos that is similar to who but gives some extra information.
Conclusion
Python is full of magic commands that can make your work faster and more efficient. Learn more easy commands that will make you love Python and you will discover a great passion for coding.