Basic Commands in Python | Magic CommandsWhen Python was introduced in 1991, it was general assumption that Python is "use at own risk" language. But situation has changed; Python is a dominant language at present time, it is used for data science, machine learning, and software development. As we know that Python is a flexible language. So we can add new features and functions such as magic command. This tutorial will discuss about the magic commands. These magic commands surely help to reduce the lot of headache. IntroductionMagic commands are easy designed syntax that facilitates us to perform routine task. These are basically created to accomplish some common task in data analysis using Python. In fact, they control the nature of IPython itself. It is used to running an external script or calculating the execution time of a piece of code. We can use the two different forms of magic commands in IPython.
Let's learn about the most popular and interesting commands. Built -in Magic commands1.%autocall [mode]The %autocall[mode] magic function is used to make a function automatically callable without having to use parentheses. 2. %automagicThe magic function can be also callable without having to initial % if we set to 1. We need to set to 0 to deactivate it. Output: 3. %runSuppose we have a file named program_test.py with the following code. We use the following statement to run. The script will run without importing defined variables. The above command will behave same as the python program_script.py file. Note - We can offer access to already defined variables using %run -i.Now we can access all the variables of executed file in IPython shell. 4. %cdThis magic command changes the current directory. It automatically manages the internal list of directories we visit during IPython session. Output: Usage -
5. %dhistIt is a very useful magic command; it prints all the directories we have visited in current session. Every time %cd command is executed, this is updated in _dh variable. Output: 6. %editThe edit magic command unlocks the default text editor of current operating system (Notepad for Window) for editing a Python script. We can edit the current Python script in editor. Output: 7. %env[GUINAME]This magic command is used to enable and disables IPython GUI event loop integration. When we use the GUINAME argument, this command replaces the default GUI toolkits by the specified one. 8. %%timeitThe above command calculates the time taken by the IPython environment to execute a Python program. Let's understand the following program. Output: 659 µs ± 19.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 9. %lsmagicThis command will return the list of all magic command. Let's see the following example. Output: 10. %whoThe above command will return a list of all variables that are defined in the current notebook. Output: 11. %pinfo <variable_name>It returns the detailed information about the variable. We can inspect the object that is stored in the particular variable name. Let's understand the following example. Command - Output: Type: int String form: 10 Docstring: int([x]) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int('0b100', base=0) 4 13. %matplotlib inlineIt is used to display the matplotlib graph in the Jupyter notebook. However, this command is available in the older version of Jupyter notebook. In the newer versions, this is no longer in use. 14. %histThis command returns the history of the current notebook. Means, what we have done so far in the current notebook. Output - Execute the Html Script in IPythonWe can execute the HTML and JavaScript code using the magic command. It helps us to provide some simple UI elements to our code. Syntax - %%html allows us to execute the html script. %%js allows us to execute the JS script. Let's see the following example. Command Working with Environment Variable
This command offers us to access and handle system environment variables. Output: {'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\DEVANSH SHARMA\\AppData\\Roaming', 'C:\\PROGRAMDATA\\ANACONDA3\\SCRIPTS\\': 'C:\\ProgramData\\Anaconda3', 'C:\\USERS\\DEVANSH SHARMA\\APPDATA\\ROAMING\\NPM': 'C:\\Users\\DEVANSH SHARMA\\AppData\\Roaming\\npm', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'DESKTOP-2VAN176', 'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe', 'DART_SDK': 'C:\\Program Files\\Dart\\dart-sdk', 'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\DEVANSH SHARMA', 'LOCALAPPDATA': 'C:\\Users\\DEVANSH SHARMA\\AppData\\Local', 'LOGONSERVER': '\\\\DESKTOP-2VAN176', 'NUMBER_OF_PROCESSORS': '4', 'ONEDRIVE': 'C:\\Users\\DEVANSH SHARMA\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'C:\\Users\\DEVANSH SHARMA\\Anaconda3;C:\\Users\\DEVANSH SHARMA\\Anaconda3\\Library\\mingw-w64\\bin;C:\\Users\\DEVANSH SHARMA\\Anaconda3\\Library\\usr\\bin;C:\\Users\\DEVANSH SHARMA\\Anaconda3\\Library\\bin;C:\\Users\\DEVANSH SHARMA..............
Output:
ConclusionIn this tutorial, we have covered the important magic commands of the Jupyter notebook. These commands make work easier and also provide the flexibility to use notebook efficiently. Next TopicF String in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India