|
| 1 | +# Coding 103: Coding with Python and Parsing JSON |
| 2 | + |
| 3 | +In this Learning Lab you will learn the basics of how to write Python code and parse JSON. |
| 4 | + |
| 5 | + |
| 6 | +## Objective ## |
| 7 | + |
| 8 | +Completion Time: 45 minutes |
| 9 | + |
| 10 | +* Understand and apply the fundamentals of coding in Python |
| 11 | +* Learn how to parse simple to slightly complex JSON using Python |
| 12 | + |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +**Background** |
| 17 | +* We recommend that you complete the [Coding 101 Rest Basics Learning Lab](/#/lab/coding-101-rest-basics-ga/step/1) before you start this lab. |
| 18 | + |
| 19 | +**Access to an APIC-EM Controller** |
| 20 | +* To run these code samples, you will need access to an APIC-EM controller. |
| 21 | +* **If you are not using your own APIC-EM Controller**, use the [DevNet Sandbox](https://developer.cisco.com/site/devnet/sandbox/) Always-On APIC-EM Lab: https[]()://devnetapi.cisco.com/sandbox/apic_em . |
| 22 | + |
| 23 | +**Python** |
| 24 | +* To run the code samples, you need to have Python 3 installed on your machine. |
| 25 | +* **If you are working on a DevNet Learning Lab PC at a DevNet event**, Python 3.x + is already installed. |
| 26 | +* See **How to Set up Your Own Computer** section above for how to install Python on your own machine. |
| 27 | + |
| 28 | +**Python Requests Library** |
| 29 | +* Some of the code samples use the Python Requests Library to simplify making REST API calls. |
| 30 | +* **If you are working on a DevNet Learning Lab PC at a DevNet event**, the Requests Library is already installed. |
| 31 | +* See **How to Set up Your Own Computer** section above for how to install the Requests Library on your own machine. |
| 32 | + |
| 33 | + |
| 34 | +**Clone Git Repo** |
| 35 | +* **If you are working on a DevNet Learning Lab PC at a DevNet event**, |
| 36 | + * Open the Git Command window by clicking on the *Git CMD* icon on the Task Bar or click on the Start button, then in the Run bar type: **git cmd** and press the **Enter** key. |
| 37 | +* **If you are working from your own workstation**, on your desktop open a command terminal. |
| 38 | +* Go to the root directory by typing: `cd \` |
| 39 | +* Create a directory called 'C:\DevNetCode\yourname' by typing: `mkdir DevNetCode\<your-name>` |
| 40 | + * For example: `mkdir DevNetCode\brTiller` |
| 41 | +* Go to the new directory by typing: `cd \DevNetCode\<your-name>` |
| 42 | + * For example: `cd \DevNetCode\brTiller` |
| 43 | +* Clone the coding skills sample code from GitHub. Enter the command below. |
| 44 | +``` |
| 45 | +git clone https://github.com/CiscoDevNet/coding-skills-sample-code |
| 46 | +``` |
| 47 | +<br/><br/> |
| 48 | +Inside the directory you created you should now see directory 'coding-skills-sample-code'.<br/><br/> |
| 49 | +<br/><br/> |
| 50 | + |
| 51 | +## Step 1. Python Version, Scripts and Interpretor |
| 52 | + |
| 53 | +### Checking Your Python Version |
| 54 | + |
| 55 | +It's quite easy to check the version of Python installed on your system. Simply access a terminal window, then at the command prompt type: **python -V** and press the return key. If you have multiple versions of python installed on your system or python 3 you may need to type either **py -3 -V** or **python3 -V** depending upon your operating system. See the graphic below for details. |
| 56 | +<br/><br/> |
| 57 | +Here's an example of how to get the Python version.<br/><br/> |
| 58 | +<br/><br/> |
| 59 | + |
| 60 | + |
| 61 | +### Running Your Python Script |
| 62 | +To run a Python script is quite simple. At the terminal window command prompt enter **python** then enter the full name of the python script and press the return key. If you are not at the directory where the python script resides you will have to specify the full directory location of the script as well. |
| 63 | +<br/><br/> |
| 64 | +Here's an example of running a Python Script.<br/><br/> |
| 65 | +<br/><br/> |
| 66 | + |
| 67 | +### Using the Python Interpretor |
| 68 | +At the command prompt you can enter the Python Interpretor by simply typing **python** and pressing the return key. Inside the Interpretor you can write and run very basic Python code. To exit type Cntrl-Z or quit(). |
| 69 | +<br/><br/> |
| 70 | +Here's an example of using the Python interpretor<br/><br/> |
| 71 | +<br/><br/> |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## Give it a try! |
| 76 | + |
| 77 | +### Get the Python Version. Use the interpretor |
| 78 | +1. Open a terminal |
| 79 | +2. Enter the appropriate python command for your operating system to get the version. For example, in Windows you would type **python -V** then press the return key. |
| 80 | +3. Enter the appropriate python command to start the interpretor. For example, in Windows you would type **python** then press the return key. |
| 81 | +4. Enter **print("Hello World! How are you?")** and press the return key. |
| 82 | +5. To exit press the **Ctrl** key then **z** or enter **quit()** |
| 83 | + |
| 84 | +### Run a Python Script |
| 85 | +1. Open a terminal and go to the directory you created earlier named: ** DevNetCode\<your-name>** |
| 86 | + * In the **Prerequisites section** you had cloned the source code files from the git repository into this directory. Inside the directory you created you should see subdirectory **coding-skills-sample-code**. If that subdirectory does not exist, go back to the prerequisites section and follow the steps to create the directory and clone the git repository. |
| 87 | +2. Go to directory **coding103-python-json**. In the terminal type: |
| 88 | +**cd \DevNetCode\<your-name>\coding-skills-sample-code\coding103-python-json** |
| 89 | +3. To run the script type the python command and then the filename at the command prompt, and press the return key. |
| 90 | + * *On Windows type*: **py -3 hello.py**. Or type: **python hello.py** |
| 91 | + * *On Mac OS or Linux type*: **python3 hello.py** |
| 92 | +4. The program should execute or display an error message. |
| 93 | + |
| 94 | +**Next Step:** Learn about Python Data Types |
0 commit comments