Starting Out With Python Second Edition

 0  0  632  2019-03-16 05:26:57 Laporkan dokumen yang dilanggar

ISBN 10: 0-13-257637-6

ISBN-13: 978-0-13-257637-6ISBN-10: 0-13-257637-6

1. Python (Computer program language) I. Title

  However, many improvements have been made, which are summarized here: Brief Overview of Each Chapter Chapter 1: Introduction to Computers and Programming This chapter begins by giving a very concrete and easy-to-understand explanation of how computers work, how data is stored and manipulated, and why we write programs in high- level languages. Chapter 4: Decision Structures and Boolean Logic In this chapter the student learns about relational operators and Boolean expressions and is shown how to control the flow of a program with decision structures.

Chapter 5: Repetition Structures This chapter shows the student how to create repetition structures using the loop

  As shown inFigure 1-2, a typical computer system consists of the following major components: 1.2 Hardware and SoftwareFigure 1-2 Typical components of a computer system Central Processing UnitOutput Devices Input DevicesMain Memory (RAM) Secondary Storage Devices The CPU When a computer is performing the tasks that a program tells it to do, we say that the com- puter is running or executing the program. For example, the pattern on the left in Figure 1-8 showshow the number 77 would be stored in a byte, and the pattern on the right shows how the letter A would be stored in a byte.

65 A

  Here is an example of how you type this statement at the interpreter’s prompt: >>> print('Python programming is fun!') e After typing the statement, you press the Enter key and the Python interpreter executes the statement, as shown here:⬎⬎⬎e print('Python programming is fun!') After the message is displayed, the >>> prompt appears again, indicating that the inter- preter is waiting for you to enter another statement. To make sure that you can interact with the Python interpreter, try the following steps on your computer: print('This is a test of the Python interpreter.') everything correctly, your session should look like this: print('This is a test of the Python interpreter.') e ⬎⬎⬎ This is a test of the Python interpreter.

2.3 Displaying Output with the print Function function to display output in a Python program

  When the Python interpreter reads a numeric literal in a program’s code, it deter- mines its data type according to the following rules: int 7 124 9 float are , , and .1.5 3.14159 5.0 So, the following statement causes the number 503 to be stored in memory as an : introom = 503 And the following statement causes the number 2.75 to be stored in memory as a : float dollars = 2.75 When you store an item in memory, it is important for you to be aware of the item’s data type. (You can think of the newline character as a special command that causes the computer to start a new line of output.)If you do not want the function to start a new line of output when it finishes dis- print playing its output, you can pass the special argument to the function, as shown in end=' ' the following code: print('One', end=' ') print('Two', end=' ')print('Three') Notice that in the first two statements, the argument is passed to the func- end=' ' print tion.

3.6 Global Variables and Global Constants CONCEPT: A global variable is accessible to all the functions in a program file

  When a variable is created by an assignment statement that is written outside all the functions in a program file, the variable is global. When the 10 The assignment statement in line 2 creates a variable named my_value Program Output 3.6 Global Variables and Global Constants stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from.

3 CONTRIBUTION_RATE = 0.05

4 5 def main():6 gross_pay = float(input('Enter the gross pay: ')) 7 bonus = float(input('Enter the amount of bonuses: '))8 show_pay_contrib(gross_pay) 9 show_bonus_contrib(bonus) 10 11 # The show_pay_contrib function accepts the gross12 # pay as an argument and displays the retirement 13 # contribution for that amount of pay.14 def show_pay_contrib(gross): 15 contrib = gross * CONTRIBUTION_RATE16 print('Contribution for gross pay: $', \ 17 format(contrib, ',.2f'), \18 sep='') 19 20 # The show_bonus_contrib function accepts the21 # bonus amount as an argument and displays the 22 # retirement contribution for that amount of pay.23 def show_bonus_contrib(bonus): 24 contrib = bonus * CONTRIBUTION_RATE25 print('Contribution for gross pay: $', \ 26 format(contrib, ',.2f'), \27 sep='') 28 29 # Call the main function.30 main()Program Output (with input shown in bold)Enter the gross pay: 80000.00 e Enter the amount of bonuses: 20000.00 e Contribution for gross pay: $4000.00 Contribution for bonuses: $1000.00 First, notice the global declaration in line 3: CONTRIBUTION_RATE = 0.05 will be used as a global constant to represent the percentage of an

CONTRIBUTION_RATE

  Then, she calculates the number of calories that result from the fat, using the following formula:calories from fat ⫽ fat grams ⫻ 9 Next, she calculates the number of calories that result from the carbohydrates, using the following formula:calories from carbs ⫽ carb grams ⫻ 4 The nutritionist asks you to write a program that will make these calculations. Here is the algorithm in pseudocode: Get the first test score Get the second test scoreGet the third test score Calculate the averageDisplay the average If the average is greater than 95: Congratulate the user Program 4-1 shows the code for the program.

6 HIGH_SCORE = 95

  If it is true, the block of indented statements following the if clause is executed, and then control of the program jumps to the statement that follows the if-else statement. If the condition is false, the block ofindented statements following the else clause is executed, and then control of the program jumps to the statement that follows the if-else statement.

BASE_HOURS

OT_MULTIPLIER

  The gross pay is displayed.39 def calc_regular_pay(hours, rate): 40 # Calculate the gross pay.41 gross_pay = hours * rate 42 43 # Display the gross pay.44 print('The gross pay is $', format(gross_pay, ',.2f'), sep='') 45 46 # Call the main function.47 main()Program Output (with input shown in bold)Enter the number of hours worked: 40 e Enter the hourly pay rate: 20 eThe gross pay is $800.00. 2 # Get a password from the user.3 password = input('Enter the password: ') 4 5 # Determine whether the correct password6 # was entered.7 if password == 'prospero': 8 print('Password accepted.')9 else: 10 print('Sorry, that is the wrong password.') Program Output (with input shown in bold)Enter the password: ferdinand e Sorry, that is the wrong password.

Dokumen baru
Aktifitas terbaru
Penulis
123dok avatar

Berpartisipasi : 2019-03-13

Tags

Starting Out With Python 4Th Global Edition Pdf Pdf

Learning Data Mining With Python Second Edition Pdf Pdf

Python For Secret Agents Second Edition Pdf Pdf

Starting With Maths

614 Programming Python 4Th Edition

Game Design Second Edition Pdf

A Student S Guide Second Edition

Ethics In Information Technology Second Edition

Civil Engineering Handbook Second Edition The

Starting Out With Python Second Edition

Gratis

Feedback