Programming Essentials in Python - Chapter 1 PDF

Summary

This textbook chapter introduces the Python programming language and fundamental computer concepts. It covers programming languages, hardware components, and getting started with Python programming. Concepts explained include machine language, high-level languages, and the Python interpreter.

Full Transcript

Chapter 1: Introduction to Computers, Programs, and Python This book provides an introduction to the Python programming language. Python is a popular open source programming language used for both standalone programs and scripting applications in a wide variety of domains. It is f...

Chapter 1: Introduction to Computers, Programs, and Python This book provides an introduction to the Python programming language. Python is a popular open source programming language used for both standalone programs and scripting applications in a wide variety of domains. It is free, portable, powerful, and remarkably easy and fun to use. Programmers from every corner of the software industry have found Python’s focus on developer productivity and software quality to be a strategic advantage in projects both large and small. Whether you are new to programming or are a professional developer, this book’s goal is to bring you quickly up to speed on the fundamentals of the core Python language. The benefits of using Python Some common terms you should know to get started How to download Python and the other programs you will need to get started Some of the basic functions and commands with Python Learning what comments are as 1 well as strings and more functions Learning what variables are and how they can help you do in Python Getting started in programming can be scary, but Python makes it easy. After reading this book, you will know enough about Python to apply it in whatever application domains you choose to explore. 1. What is a computer? A computer includes both hardware and software. In general, hardware comprises the visible, physical elements of the computer, and software provides the invisible instructions that control the hardware and make it perform specific tasks. Knowing computer hardware isn’t essential to learning a programming language, but it can help you better understand the effects that a program’s instructions have on the computer and its components. This section introduces com- puter hardware components and their functions. A computer consists of the following major hardware components (Figure 1): A central processing unit (CPU) Memory (main memory) Storage devices (such as disks and CDs) Input devices (such as the mouse and keyboard) Output devices (such as monitors and printers) Communication devices (such as modems and network interface cards) 2 2. Programming Languages Computers do not understand human languages, so programs must be written in a language a computer can use. There are hundreds of programming languages, and they were developed to make the programming process easier for people. However, all programs must be converted into a language the computer can understand. 2.1 Machine Language A computer’s native language, which differs among different types of computers, is its machine language—a set of built-in primitive instructions. These instructions are in the 3 form of binary code, so if you want to give a computer an instruction in its native language, you have to enter the instruction as binary code. For example, to add two numbers, you might have to write an instruction in binary code, like this: 1101101010011010 2.2 High-Level Language There are many high-level programming languages, and each was designed for a specific purpose. Table 1 lists some popular ones. A program written in a high-level language is called a source program or source code. Because a computer cannot understand a source program, a source program must be 4 translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler. An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away. A compiler translates the entire source code into a machine-code file, and the machine code file is then executed. Figure An interpreter translates and executes a program one statement at a time. (b) A compiler translates the entire source program into a machine-language file for execution. 5 2.3 Getting Started with Python Python is one of the world's most popular programming languages. Python is a high-level language that can be used to create high-performance applications. Simple, Easy to learn, and open source. Its syntax is clear and emphasizes readability. Python has a large and comprehensive standard library. The Problem Analysis–Coding–Execution Cycle 6 How to Download and Install Python? Steps to Download Python on Windows 10. Step 1: First and foremost step is to open a browser and type https://www.python.org/downloads/windows/ Step 2: Underneath the Python Releases for Windows find the Latest Python 3 Release – Python 3.11.2 (the latest stable release as of now is Python 3.11.2). Python installation 7 Step 3: On this page move to Files and click on Windows x86-64 executable installer for 64-bit or Windows x86 executable installer for 32-bit. Here we are providing the installation process of Python 3.11.2 on Windows Run the Python Installer for how to install Python on the Windows downloads folder Make sure to mark Add Python 3.11 to PATH otherwise you will have to do it explicitly. It will start installing Python on Windows. 8 After installation is complete click on Close. Now go to Windows and type IDLE. Python Shell 9 This is Python Interpreter also called Python Shell. I printed Hello geeks, python is working smoothly. The three greater than >>> sign is called the Python command prompt, where we write our program and with a single enter key, it will give results so instantly. Python Tools for development. Compiler. ✓ Python 3 Text Editor ✓ Atom, Sublime, Notepad++ Editor with the compiler: Fig. 1 Python Tools for development. 10 Creating Python Source Code Filest Entering Python statements at the statement prompt >>> is convenient, but the statements are not saved. To save statements for later use, you can create a text file to store the statements and use the following command to execute the statements in the file: python filename.py The text file can be created using a text editor such as Notepad. The text file, filename, is called a Python source file or script file, or module. By convention, Python files are named with the extension.py. Running a Python program from a script file is known as running Python in script mode. Typing a statement at the statement prompt >>> and executing it is called running Python in interactive mode. 1 # Display two messages 2 print("Welcome to Python") 3 print("Python is fun") When the Python interpreter sees #, it ignores all text after # on the same line. When it sees ''', it scans for the next ''' and ignores any text between the triple quotation marks. Here are examples of comments: # This program displays Welcome to Python ''' This program displays Welcome to Python andPython is fun 11 ''' # Display two messages print("Welcome to Python") print("Python is fun") Python programs are case sensitive. It would be wrong, for example, to replace print in the program with Print. You have seen several special characters (#, ", ()) in the program. They are used in almost every program. Table 2 summarizes their uses. 12 Using Python to Perform Mathematical Computations Python programs can perform all sorts of mathematical computations and display the result. To display the addition, subtraction, multiplication, and division of two numbers, x and y, use the following code: print(x + y) print(x – y) print(x * y) print(x / y) Programming Errors Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors. 1- Syntax Errors The most common error you will encounter are syntax errors. Like any programming language, Python has its own syntax, and you need to write code that obeys the syntax rules. If your program violates the rules—for example, if a quotation mark is missing or a word is misspelled—Python will report syntax errors. Syntax errors result from errors in code construction, such as mistyping a statement, incor- rect indentation, omitting some necessary punctuation, or using an opening parenthesis with- out a corresponding closing parenthesis. These errors are usually easy to detect, 13 because Python tells you where they are and what caused them. For example, the following print statement has a syntax error. 2- Runtime Errors Runtime errors are errors that cause a program to terminate abnormally. They occur while a program is running if the Python interpreter detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. An input error occurs when the user enters a value that the program cannot handle. For instance, if the program expects to read in a number, but instead the user enters a string of text, this causes data-type errors to occur in the program. Another common source of runtime errors is division by zero. This happens when the divisor is zero for integer divisions. For example, the expression 1 / 0 in the following statement would cause a runtime error. 14 3- Logic Errors Logic errors occur when a program does not perform the way it was intended to. Errors of this kind occur for many different reasons. For example, suppose you wrote the program to convert a temperature (35 degrees) from Fahrenheit to Celsius. 2.4 End of chapter Questions Q) Choose the best answers 15 16 17 18 19 20

Use Quizgecko on...
Browser
Browser