Basic Commands in Python

Here are some basic commands in Python:

  1. print(): This command is used to print the specified text or value to the console.
  2. input(): This command is used to take input from the user.
  3. len(): This command is used to find the length of a string, list, or tuple.
  4. range(): This command is used to generate a sequence of numbers.
  5. type(): This command is used to find the data type of a variable or value.
  6. int(): This command is used to convert a value to an integer.
  7. float(): This command is used to convert a value to a float.
  8. str(): This command is used to convert a value to a string.
  9. list(): This command is used to convert a value to a list.
  10. tuple(): This command is used to convert a value to a tuple.
  11. dict(): This command is used to create a dictionary.
  12. set(): This command is used to create a set.
  13. max(): This command is used to find the maximum value in a list.
  14. min(): This command is used to find the minimum value in a list.
  15. sum(): This command is used to find the sum of values in a list.
  16. sorted(): This command is used to sort a list.
  17. reversed(): This command is used to reverse the order of a list.
  18. zip(): This command is used to combine two or more lists.
  19. for loop: This command is used to execute a block of code repeatedly for a specified number of times.
  20. if statement: This command is used to execute a block of code if a specified condition is true.

Magic Commands:

Magic commands, also known as “IPython magic commands,” are special commands available in the Jupyter Notebook and IPython shell that allow you to perform a variety of tasks. Here are some examples:

  1. %run: This command is used to run a Python script.
  2. %time: This command is used to time the execution of a statement or expression.
  3. %matplotlib: This command is used to enable interactive matplotlib graphs in the Jupyter Notebook.
  4. %pwd: This command is used to print the current working directory.
  5. %cd: This command is used to change the current working directory.
  6. %ls: This command is used to list the contents of the current directory.
  7. %load: This command is used to load the contents of a file into a cell in the Jupyter Notebook.
  8. %reset: This command is used to reset the namespace by removing all names defined by the user.
  9. %history: This command is used to show the history of commands that have been entered in the shell.
  10. %debug: This command is used to activate the debugger.
  11. %%timeit: This command is used to time the execution of a statement or expression multiple times and calculate the average time.
  12. %%writefile: This command is used to write the contents of a cell to a file.
  13. %%html: This command is used to display HTML code.
  14. %%bash: This command is used to run a bash command in the Jupyter Notebook.
  15. %%python2 or %%python3: These commands are used to run Python 2 or Python 3 code in the Jupyter Notebook, respectively.

Note that magic commands are specific to IPython and Jupyter Notebook and may not work in other Python environments.

Execute the Html Script in IPython:

You can execute HTML code in IPython using the %%html magic command. Here’s an example:

  1. Open a new cell in your Jupyter Notebook.
  2. Type the following HTML code into the cell:
%%html
<h1>Hello, World!</h1>
<p>This is an example of executing HTML code in IPython.</p>
  1. Run the cell by pressing “Shift+Enter” or clicking on the “Run” button in the toolbar.

This will display the HTML output in the Jupyter Notebook, which should show a heading that says “Hello, World!” and a paragraph of text.

Note that the %%html magic command can be used to execute any valid HTML code in IPython. However, it is important to note that the HTML code will only be executed and displayed in the Jupyter Notebook, and will not have any effect on any other Python environments or webpages.

Working with Environment Variable:

Environment variables are a way to store configuration information or other data that can be accessed by different programs running on the same computer. In Python, you can access and modify environment variables using the os module. Here are some basic examples:

  1. Accessing an environment variable:
import os
my_variable = os.environ.get('MY_VARIABLE')
print(my_variable)

This code retrieves the value of an environment variable called MY_VARIABLE and stores it in a variable called my_variable. If the environment variable does not exist, my_variable will be None.

  1. Setting an environment variable:
import os
os.environ['MY_VARIABLE'] = 'my_value'

This code sets the value of an environment variable called MY_VARIABLE to my_value.

  1. Checking if an environment variable exists:
import os
if 'MY_VARIABLE' in os.environ:
    print('MY_VARIABLE exists')
else:
    print('MY_VARIABLE does not exist')

This code checks if an environment variable called MY_VARIABLE exists and prints a message accordingly.

  1. Removing an environment variable:
import os
del os.environ['MY_VARIABLE']

This code removes the environment variable called MY_VARIABLE.

Note that changes made to environment variables in a Python script only affect the environment for that script and any child processes it spawns. They will not affect the environment of the parent process or any other running programs.

Conclusion:

In conclusion, Python is a versatile programming language that can be used for a variety of tasks, from web development and data analysis to machine learning and artificial intelligence. In this conversation, we discussed some basic commands in Python, as well as some magic commands that can be used in the Jupyter Notebook and IPython shell to perform various tasks. We also briefly touched upon how to work with environment variables in Python using the os module. Python has a large and active community of developers and users, making it a great language to learn and use for a wide range of projects.