The first Python script

Now that you have a basic idea of what Python is, let's create a script. Instead of the famous Hello World! introduction, we are going to use a cult film example. The scripts will define a function, which will print a famous quote from the 1983 cult classic WarGames. There are two ways of doing this, as mentioned previously; the first is through the interactive interpreter, and the second is through a script. Open an interactive interpreter and execute the following line:

print("Shall we play a game?
")

The preceding print statement will show that the code execution worked. To exit the interactive interpreter, either type exit() or use Ctrl + Z in Windows or Ctrl + D in Linux. Now, create a script in your preferred editing tool, such as vi, vim, emacs, or gedit. Then save the file in /root/Desktop as wargames_print.py:

#!/usr/bin/env python
print("Shall we play a game?
")

After saving the file, run it with the following command:

python /root/Desktop/wargames_print.py

You will again see the script execute with the same results. Be aware of a few items in this example. The python script is run by referencing the fully qualified path so as to ensure that the correct script is called, no matter what the location is. If the script resided in the current location, it could, instead, be executed in the following manner:

python ./wargames_print.py

Tip

Kali does not natively require ./ to execute these scripts, but it is a good habit to be in, as most other Linux and Unix operating systems do. If you are out of the habit and slightly sleep deprived on an assessment, you may not realize why your script is not executing initially. This technique can save you a little embarrassment on multimember team engagements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.176.80