🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Ch1 - Introduction to Python (1).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

ReceptiveDanburite

Uploaded by ReceptiveDanburite

Irbid National University

Tags

python programming programming concepts computer science

Full Transcript

Chapter 1 Introduction to Python 1 Python o High-level o Human readable o Dynamically typed o Is that a good thing? o Interpreted o Object-oriented (if you want it to be) o So, easy to use o Used a lot for ‘scripting’ 2 P...

Chapter 1 Introduction to Python 1 Python o High-level o Human readable o Dynamically typed o Is that a good thing? o Interpreted o Object-oriented (if you want it to be) o So, easy to use o Used a lot for ‘scripting’ 2 Program Translation  A central processing unit (CPU) is designed to interpret and execute a specific set of instructions represented in binary form (i.e., 1s and 0s) called machine code. Only programs in machine code can be executed by a CPU.  3 Program Translation  Writing programs at this “low level” is tedious and error-prone. Therefore, most programs are written in a “high-level” programming language such as Python. Since the instructions of such programs are not in machine code that a CPU can execute, a translator program must be used.  There are two fundamental types of translators: ⚫ Compiler translates programs into machine code to be executed by the CPU ⚫ Interpreter which executes instructions in place 4 of the CPU Compiler Compiled programs generally execute faster than interpreted programs 5 Interpreter An interpreter can immediately execute instructions as they are entered. This is referred to as interactive mode. This is a very useful feature for program development. Python, as we shall see, is executed by an interpreter. 6 Interpreter 7 The Python programming language The Python Programming Language was created by Guido van Rossum. It was first released in the early 1990s. Its name comes from a 1970s British comedy sketch show called Monty Python’s Flying Circus. Companies and organizations that use Python include YouTube. Google, Yahoo and NASA. 8 Python Features o Simple Syntax o Python programs are clear and easy to read o Great for those starting out in software programming o Interpreted Language o Python instructions can be executed interactively o Powerful Programming Features o Can accomplish significant computation with only a few instructions o Modules Provide Additional Capabilities o Capabilities that can be incorporated into a Python program o Promotes re-use. 9 The IDLE Python development environment  IDLE is an integrated development environment (IDE). An IDE is a bundled set of software tools for program development. This typically includes, an editor ⚫ for creating and modifying programs a translator ⚫ for executing programs, and a program debugger ⚫ for taking control of the execution of a program to aid in finding program errors 10 The Python shell  Python can be executed interactively in the Python shell. In this mode, executing Python is similar to using a calculator. o The >>> symbol is the shell prompt. Here, typing 2 + 3 at prompt outputs the result 5, again displaying the prompt in wait of another instruction. 11 The Python standard library  The Python Standard Library is a collection of built- in modules, each providing specific functionality beyond what is included in the “core” part of Python.  For example, the math module provides additional mathematical functions. The random module provides the ability to generate random numbers, useful in programming, as we shall see. 12 Importing a library module  In order to utilise the capabilities of modules in a specific program, an import statement is used as shown. 13 Importing a library module Because the factorial function is from the math module, the function is called with the name of the module prepended: e.g., math.factorial(20) 14 A bit of Python  We introduce a bit of Python, just enough to begin writing some simple programs.  Since all computer programs, input data process the data output results  we look at the notion of a variable, how to perform some simple arithmetic calculations, and how to do simple input and output. 15 Variables  One of the most fundamental concepts in programming is that of a variable.  A variable is “a name that is assigned to a value,” as shown below,  n = 5 variable n is assigned the value 5  Thus, whenever variable n appears in a calculation, it is the current value of n is that is used, as in the following,  n + 20 (5 + 20)  If variable n is assigned a new value, then the same expression will produce a different result,  n = 10  n + 20 (10 + 20) 16 Some basic arithmetic operators  The common arithmetic operators in Python are, + (addition) * (multiplication) ** (exponentiation) - (subtraction) / (division)  Addition, subtraction, and division use standard mathematical notation, 10 + 20 25 - 15 20 / 10 (Also, // for truncated division, discussed later)  For multiplication and exponentiation, the asterisk (*) is used, 5 * 10 (5 times 10) 2 ** 4 (2 to the 4th power)  Multiplication is never denoted by the use of parentheses, 10 * (20 + 5) CORRECT 10(20 + 5) INCORRECT 17  Note that parentheses may be used to denote subexpressions. Basic input  The programs that we will write request and get information from the user. In Python, the input function is used for this purpose,  name = input('What is your name?: ')  Characters within quotes are called strings. This particular use of a string, for requesting input from the user, is called a prompt.  The input function displays the string on the screen to prompt the user for input,  What is your name?: Charles  The underline is used here to indicate the user’s input. 18 Basic output  The print function is used to display information on the screen in Python. This may be used to display a message (string), >>> print('Welcome to My First Program!') Welcome to My First Program!  or used to output the value of a variable, >>> n = 10 >>> print(n) 19 10 Basic output  Can also display a combination of strings and variables, >>> name = input('What is your name?: ') What is your name?: Charles >>> print('Hello', name) Hello Charles  Note that a comma is used to separate the individual items being printed, which causes a space to appear between each when displayed. Thus, the output of the print function in this case is Hello Charles  and not HelloCharles  We will soon learn more about variables, operators, and input/output in Python. 20 Basic output  To begin the next output on the same line as the previous one, you can place the expression end = "", which says “end the line with an empty string instead of a newline,” as follows: print(, end = "") 21 Basic Input/output 22 Using IDLE  In order to become familiar with writing your own Python programs using IDLE, we will create a simple program that asks the user for their name and responds with a greeting. This program utilizes the following concepts: ➤ Creating and executing Python programs ➤ Input and print functions 23 Creating a new Python program  To create a Python program file, select New Window from the File menu in the Python shell, 24 Creating a new Python program  A new, untitled window will appear, 25 Creating a new Python program  Now you can begin entering lines of a program without them being immediately executed, as in the Python shell. Note that parts of the program lines are displayed in a certain color. Since print and input are predefined function names in Python, they are colored purple. The strings in the program are colored green. The statement in red is a comment statement. Comment statements are for those reading the program, and are ignored when the program is 26 executed. Creating a new Python program  When finished, save the program file by selecting Save As under the File menu, and save in the appropriate folder with the name MyFirstProgram.py. 27 Executing a Python program  To run a Python program, select Run Module from the Run menu (or simply hit function key F5). 28 Executing a Python program 29 Executing a Python program 30 Detecting and Correcting Syntax Errors 31 Detecting and Correcting Syntax Errors In this error message, Python explains that this line of code is unexpectedly indented. In fact, there is an extra space before the word print. Indentation is significant in Python code. Each line of code entered at a shell prompt or in a script must begin in the leftmost column, with no leading spaces. The only exception to this rule occurs in control statements and definitions, where nested statements must be indented one or more spaces. 32

Use Quizgecko on...
Browser
Browser