Lecture #1 Introduction to Python Introduction Python Is a High-Level

Lecture #1 Introduction to Python Introduction Python Is a High-Level

Lecture #1 Introduction to Python Introduction Python is a high-level general-purpose programming language, which means it is a language designed to be used for writing software in a wide variety of application domains. It is an object-oriented language and is described by the 2019 IEEE Spectrum as an easy-to-use programming language. In the field of cybersecurity, Python has earned a top position and shows a tendency to quickly outperform other languages. Microsoft has included Python in its Visual Studio, known as the Microsoft Python extension. Python, in a nutshell, is a highly modularized language, which means it supports a large number of standard libraries. Each of the modules is specially developed to support a different programming task such as connecting to web servers, searching text with regular expressions, reading and modifying files, system administration, and cybersecurity. Python is a free, open source language maintained by the Python Software Foundation. Its packages and source codes are available for download the at http://www.python.org website. It is “free” in following two senses: cost and freedom. • Cost: It does not cost anything to download or use Python, or to include it in your application. • Freedom: Python can also be freely modified and re-distributed, because it is available under an open source license although the language is copyrighted. The latest generation of Python is Python 3. There has been some discussions about the future Python 4. However, nothing is released by Python.org as of the day this lecture is written. This lecture and all its sample codes are designed based on Python 3. Getting Python Most Windows operating system users starts the learning journey with downloading and installing the Python interpreter which is a program that reads Python codes and converts the Python codes to machine codes at the runtime. However, the Python codes will remain being a text-based contents stored in a text file. Python source codes Python intepreter Operating system Linux users probably do not need to go through the installation process, because most Linux distributions come with Python pre-installed. Some Linux distributions provide Python 2, but are readily for upgrading to Python 3. Mac OS X 10.8 comes with Python 2.7 pre-installed, while Microsoft Windows operating systems do not have Python pre-installed. With that being said, some Linux and Mac OS users might have to upgrade from Python 2.7 to Python 3. A later section will provide some guidelines for Linux users. Although Mac OS is not a supported platform for this course, students who choose to use Mac OS can visit the https://docs.python.org/3/using/mac.html site for details about installation and upgrading of Python. Microsoft Windows users can download the Python package from many sources including: • Python.org (http://python.org/download/). As of July 2020, the latest stable release is Python 3.8.3, which was released on 13 May 2020. The file name is python- Python Programming – Penniel P. Wu, PhD. 1 3.8.3.exe for Microsoft Windows users. By the way, this is the primary source, and the entire Python platform can be installed in a USB drive. • ActivePython (http://www.activestate.com/activepython/downloads). • Portable Python (http://www.portablepython.com/). This is a Python package preconfigured to run directly from any USB storage device, enabling users to have, at any time, a portable programming environment. For Windows users, the instructor recommends students to install the Python package in a USB drive for the sake of portability. For Linux users, the instructor suggests using a “Live USB” version of Linux (e.g. such as Ubuntu). This course, however, does not suggest students to use Mac OS. Launch the An “interpreter” of a programming language is a program that reads and converts a sequence python of instructions in a “script” (a text file containing the source codes) into machine codes for the interpreter computer to execute the instructions on the fly. One major difference between Python and C, C++, Java, and C# are that Python does not use “compiler”. A compiler is a program designed to read and convert the source code into machine language to produce an individually executable program (such as those of “.ext”). An “interpreter” only reads the script file and convert the source codes to machine codes at the runtime, but it does not produce any individually executable program. Python relies on the interpreter to effectively read and convert the source code while a Python program is called for execution. In other words, a Python script is the program. Only when the Python script is present, the program is ready to be called for execution. Python C, C++, Java, and C# Source Interpreter Source Compiler Executable Code Code Output Executable Output On the C, C++, Java, and C# side, once compiled, the source code is no longer needed. The compiled code is the program. On the Python side, the source code (script) is the only file needed to be called for execution. Another benefit of using interpreter (instead of compiler) is that individual statement (typically is only one single line) can be interpreted to produce immediate result, without the need of compilation as shown below. During the first few lectures, the lecture notes will guide students through how to use the Python interpreter to execute individual Python statements before organizing them as a Python script. >>> print("Hello, World!") Hello, World! The Python interpreter is a console application, which means it only runs in a command-line interface (CLI) environment such as the “Command Prompt” of a Windows operating system or a terminal emulator of the Linux operating system, as shown below. Windows Command Prompt Terminal emulator Python Programming – Penniel P. Wu, PhD. 2 The prompt (or shell prompt) is defaulted to display the home drive (e.g. “C:” of Microsoft Windows or “/” of Linux) and the home directory (the directory the user is defaulted to use by the system settings). The following is a sample prompt, in which “C” is the drive name, and “C:\Users\D0213657” is the home directory, while “~” home directory of Linux. The greater than sign (>) is the symbol of prompt for Windows operating system, while the dollar sign ($) or pound sign (#) is the shell prompt for Linux. In a nutshell, the dollar sign ($) is for regular Linux user, while the pound (#) sign is for the superuser (such as “root”). Microsoft Windows Linux C:\Users\D02132657> [liveuser@localhost ~]$ By typing cd \ (or cd / in Linux) and press [Enter] in the prompt, the “presently working directory” changes to the root (\) directory which is typically represented by a letter (such as “C”) in the Windows OS and a “/” in Linux, as illustrated below. Microsoft Windows Linux C:\Users\D02132657>cd \ [liveuser@localhost ~]$ cd / [liveuser@localhost /]$ C:\> As stated previously, most Linux distributions come with Python preinstalled and pre- configured. Ubuntu, for example, is one of the most popular distributions of Linux. Python 3.6 is installed on all stable release of Ubuntu 14.04 or later. In a Linux machine, the Python interpreter is usually installed in the “/usr/local/bin/” directory of the file system. In a Python 3 pre-installed Linux environment, Linux users can simply type python3 and press [Enter] in the shell prompt to launch Python. Once launched, the Python shell (“>>>”), as shown below, will appear. [Details about using Python 3 in Linux platform are available at the https://docs.python.org/3/tutorial/interpreter.html site.] [liveuser@localhost ~]$ python3 Python 3.8 (default, Sep 16 2019, 09:25:04) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> Interestingly, users of Microsoft Windows operating systems need to add the path of the Python directory in the “PATH” environment variable in order for the operating systems to be able to locate the Python interpreter. The “PATH” variable is an operating systems variable that specifies a set of directories where executable programs are located. By default, the path to Python interpreter such as “C:\Python\” is not added to the “PATH” environment variable. The following demonstrates how to manually configure the path settings (assuming the path is “C:\python\”). By the way, environment variables in Windows are denoted with moduli surrounding the name; therefore, the value of the “PATH” variable is denoted by “%PATH%”. C:\>PATH = %PATH%;C:\python; Python Programming – Penniel P. Wu, PhD. 3 The following is another way to set the path. It uses the DOS “set” command. C:\>set path=%path%;C:\python; After the path setting, Microsoft Windows user can open the Command Prompt, and then type python.exe (or simply python) and press [Enter] to launch the Python interpreter. If the path setting went successfully, the following (or similar) message will appear. C:\>Users\D00182312>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> It is necessary to note that “>>>” is the Python prompt. It is the intermediary between the Python programmer and the Python interpreter. The prompt takes a Python statement (which consists of Python commands, arguments, expressions, or a combination of them), interprets the Python statement, and then produce the output. The following is a sample Python statement. >>> print("Hello World!") The given Python statement will be temporarily store in the memory (such as DRAMs). The programmer must press the [Enter] key to notify the shell prompt to pass the given Python statement to the Python interpreter for execution.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    48 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us