2.1.3.7 Lab – Basic Python Programming Answers
Objectives
- Part 1: Launch VirtualBox and Enter the I2IoT server VM
- Part 2: Python Basics
- Part 3: IDLE for Python
Background
Python, a programming language, allows for simpler statements. Python is very easy to use, powerful, and versatile. It has become the language of choice for many IoT developers. One of the main reasons for the popularity of Python is the developer community. Python developers have created and made available many specific modules that can be imported into any program to immediately lend added functionality.
Scenario
In this lab, you will learn and practice some basic Python programming. More specifically, we will use Python version 3 in the lab.
Required Resources
- A modern personal computer with internet access and sufficient RAM.
- VirtualBox with I2IoT server installed.
Instructions
Part 1: Launch VirtualBox and Enter the I2IoT server VM
In Part 1, you launch virtualization software VirtualBox and log in to the I2IoT server VM.
Step 1: Launch VirtualBox.
a. After VirtualBox is installed (see Lab 2.1.2.a), the VirtualBox icon should appear on the Desktop. Click the icon to launch the VirtualBox.
b. Click I2IoT – GUI on the left pane to launch the server VM.
c. The default username is IoT_user with no password. Click the blue bar IoT_user in the middle of the screen to login to the VM.
Step 2: Navigate to the user Document directory
a. To access the command line interface, click Application on the menu bar and choose Terminal.
Method 2: You can also right click on Desktop —> select “Open Terminal“
b. Use the pwd command to display the current directory
c. Use the ls command to show the list of contents in the current directory. Use the ls command with the -l option to show detailed information about the contents.
d. Use the cd Documents command to change the directory to the /home/IoT_user/Documents directory. Verify by using the command pwd.
e. To check the version of Python installed on the VM, issue the python3 –version command.
Part 2: Python Basics
In Part 2, you will learn and practice some basic Python programming.
Step 1: Python programming in the interactive interpreter.
As an interpreted language, Python commands can be issued in an interactive interpreter.
a. Use the python3 command to start Python interpreter.
b. Perform calculations.
>>> 1 + 2 3 >>> 2 * 4 8 >>> 6 / 2 3.0 >>>
c. Print a text string.
>>> “How are you?” ‘How are you?’ >>>
d. Use the type() command to determine the basic data type: int, float, string, Boolean.
>>> type(65) <class 'int'> >>> type(45.6) <class 'float'> >>> type("Hi!") <class 'str'> >>> type(True) <class 'bool'> >>> 1<2 True >>> 1<1 False >>> 1==1 True >>> 1>=1 True >>>
e. Create a variable.
>>> x=3 >>> x*5 15 >>> "Good!"*x 'Good!Good!Good!' >>>
f. Combine multiple strings together and print as one string.
>>> str1="Cisco" >>> str2="Networking" >>> str3="Academy" >>> space=" " >>> print(str1+space+str2+space+str3) Cisco Networking Academy >>>
g. Convert data type from a numeric number to a string.
>>> x=5 >>> str(x) >>> ‘5’ >>> y=4.2 >>> str(y) >>> y=‘4.2’ >>>
h. Note that integers are not rounded up when converting from float. The decimal is ignored.
>>> int(8.21) 8 >>> int(8.99) 8 >>> int(8.21) + int(8.99) 16 >>>
i. Convert an integer to a float.
>>> x=5 >>> x 5 >>> float(x) 5.0 >>> type(x) <class 'int'> >>> x=float(x) >>> type(x) <class 'float'> >>> x 5.0 >>>
j. Obtain user input.
>>> name=input("What is your name? ") What is your name? John >>> print("Hi " + name + ", it is nice to meet you!") Hi John, it is nice to meet you! >>>
k. Use quit() to exit interactive interpreter.
Part 3: IDLE for Python
IDLE stands for Integrated Development and Learning Environment. It is supported and included in the Python package. A few key features of IDLE for Python include these:
- A Python shell window (interactive interpreter) with colorizing of code input, output, and error messages
- A multiwindow text editor with multiple undo, Python colorizing, smart indent, call tips, auto completion, and other features
- The ability to search within any window, replace within editor windows, and search through multiple files (grep)
- A debugger with persistent breakpoints, stepping, and viewing of global and local namespaces
- Configuration, browsers, and other dialogs
In Part 3, you will launch IDLE and create a simple script.
Step 1: Launch IDLE.
a. Use the idle3 command to launch IDLE. By default, it starts in Python Shell, or interactive interpreter, window. You are already familiar with the interactive interpreter.
[[email protected] Documents]$ idle3
b. Click File -> New File to open a new (untitled) Python script.
c. Type the code in the script print(“Hello World!”), note the codes are colored, and open and close parentheses are matched.
d. Click File -> Save, save the current script as 1.py in the current directory. Click Save button.
e. Click Run -> Run Module (or press F5). The shell window will display the result.