1.

Python Commands

Answer»

Magic Commands are one of the new features added to the python shell. Basically, they are enhancements over the normal python code, provided by the IPython Kernel. The main syntax for these commands is that they are prefixed by as “%” character. They prove useful in solving many common problems we encounter while coding and also provide us some versatile shortcuts.
There are main kinds of commands:



  • %prefix: The command will operate only on the given single line of code.


  • %%prefix: The command will operate on the entire code block.

Some examples of these commands in Python are:

  • %run: It is used for running an external file in Python.
def runner():
print("Hello World")

runner()
%run runner.py
Output:
Hello World

  • %%time: This allows us to track the amount of time taken by our code for execution.
%%time
for i in range(10000):
a = a + i**2
Output:
CPU Times: user: 3.72 ms, sys: 9us, , total: 3.73ms, Wall time: 3.75ms

  •  %%writefile: This command will copy content from our current code cell to another external file.
%%writefile code.py
def func():
print("Hello")
func()
Output:
Overwriting code.py

  •  $pycat: This command is used to display the contents of an external file.
%pycat code.py
def func():
print("Hello")
func()

  • %who: This command lists all the variables in the Python notebook.
a = "hello"
b = 5
c = 1
%who
Output:
a b c

  •  %%html: This command will let us write and execute html code in the current cell.
%%html
<html>
<body>
<table>
<tr> 
<th>Name</th> 
<th>Country</th> 
<th>Age</th> 
</tr> 
<tr> 
<td>Sid</td> 
<td>India</td> 
<td>22</td> 
</tr>
<tr> 
<td>Dave</td> 
<td>UK</td> 
<td>28</td> 
</tr>
</table>
</body>
</html>

Output:


  • %env: This command allows us to list all the environment variables, set a value for such a variable, and get the value of such a variable.

  • %pinfo: This command provides detailed information regarding the object passed along with it. It works similar to that of the object? function.

Note: All the magic commands can be listed by using the %lsmagic command.

Some other useful tools for Python



  • pipenv: It is a packaging tool for python aimed to solve common problems which are associated with the typical program workflow.


  • poetry: It is a dependency management and packaging tool in Python.

Conclusion

We can conclude that Python is a robust, high-level, interpreted programming language. It is also an Object Oriented Programming Language that strongly follows all OOPs principles. It has various inbuilt powerful modules that follow simple syntax which makes it appealing to both beginners and experienced folks alike. A vast collection of libraries and functions makes the development of any sort much easier in Python. In this cheat sheet, we have covered the most common fundamentals of python language that would help you kickstart your career in python.

Useful Resources:


  • Python Interview Questions

  • Python Projects

  • Python Developer: Career Guide

  • Python Developer Skills

  • Fast Track Python




Discussion

No Comment Found