Instructor s Manual. for. Harvey M. Deitel Paul J. Deitel Jonathan Liperi Ben Wiedermann

Size: px
Start display at page:

Download "Instructor s Manual. for. Harvey M. Deitel Paul J. Deitel Jonathan Liperi Ben Wiedermann"

Transcription

1 Instructor s Manual for Harvey M. Deitel Paul J. Deitel Jonathan Liperi Ben Wiedermann

2 II 1 Introduction to Computers, Internet and World Wide Web 1 2 Introduction to Python Programming 5 Control Structures 10 4 Functions 21 5 Lists, Tuples and Dictionaries 6 Introduction to the Common Gateway Interface (CGI) 44 7 Object-Based Programming 59 8 Customizing Classes 79 9 Object-Oriented Programming: Inheritance Graphical User Interface Components: Part Graphical User Interface Components: Part Exception Handling String Manipulation and Regular Expressions File Processing and Serialization Extensible Markup Language (XML) Python XML Processing Database Application Programming Interface (DB-API) Process Management Multithreading 2 20 Networking 24

3 III 21 Security Data Structures 26 2 Case Study: Online Bookstore Multimedia Python Server Pages (PSP) 24 C Number Systems 9 F Unicode 4 G Introduction to HyperText Markup Language 4: Part 1 48 H Introduction to HyperText Markup Language 4: Part 2 52 I Introduction to XHTML: Part 1 57 J Introduction to XHTML: Part 2 70 K Cascading Style Sheets (CSS) 87 L Accessibility 97 Multimedia Cyber Classroom: Solutions Provided on CD 40

4 1 Introduction to Computers, Internet and World Wide Web SELF-REVIEW EXERCISES 1.1 Fill in the blanks in each of the following statements: a) The company that popularized the phenomenon of personal computing was. Apple. b) The computer that made personal computing legitimate in business and industry was the. IBM Personal Computer. c) Computers process data under the control of sets of instructions called computer. programs. d) The six key logical units of the computer are the,,,, and the. input unit, output unit, memory unit, arithmetic and logic unit, central processing unit, secondary storage unit. e) Python can incorporate new (reusable pieces of software), which can be written by any Python developer. modules. f) The three classes of languages discussed in the chapter are, and. machine languages, assembly languages, high-level languages. g) The programs that translate high-level language programs into machine language are called. compilers. h) C is widely known as the development language of the operating system. UNIX. i) In 2001, the core Python development team moved to Digital Creations, the creators of a Web application server written in Python. Zope. j) The Department of Defense developed the Ada language with a capability called, which allows programmers to specify activities that can proceed in parallel.

5 2 Introduction to Computers, Internet and World Wide Web Appendix 1 multitasking. 1.2 State whether each of the following is true or false. If false, explain why. a) Hardware refers to the instructions that command computers to perform actions and make decisions. False. Software refers to the instructions that control computers, also referred to as hardware. Hardware refers to the computer s devices. b) The re regular-expression module provides pattern-based text manipulation in Python. c) The ALU provides temporary storage for data that has been entered through the input unit. False. The memory unit provides temporary storage for data that have been entered through the input unit. The arithmetic and logic unit (ALU) performs the calculations and contains the decision mechanisms of the computer. d) Software systems called batches manage the transition between jobs. False. Software systems called operating systems manage the transition between jobs; in single-user batch processing, the computer runs a single program at a time while processing data in batches. e) Assemblers convert high-level language programs to assembly language at computer speeds. False. Assemblers convert assembly-language programs to machine language at computer speeds. f) Interpreter programs compile high-level language programs into machine language faster than compilers. False. Interpreter programs can directly execute high-level language programs without compiling them into machine language. g) Structured programming is a disciplined approach to writing programs that are clear and easy to modify. h) Unlike other programming languages, Python is non-extensible. False. Unlike other programming languages, Python is extensible. i) Objects are reusable software components that model items in the real world. j) Several Canvas components include Label, Button, Entry, Checkbutton and Radiobutton. False. Several Tkinter components include Label, Button, Entry, Checkbutton and Radiobutton. EXERCISES 1. [CD] Categorize each of the following items as either hardware or software: a) CPU. Hardware. b) ALU. Hardware. c) Input unit. Hardware. d) A word-processor program. Software. e) Python modules. Software.

6 Appendix 1 Introduction to Computers, Internet and World Wide Web 1.4 Translator programs such as assemblers and compilers convert programs from one language (referred to as the source language) to another language (referred to as the object language). Determine which of the following statements are true and which are false: a) A compiler translates high-level language programs into object language. b) An assembler translates source-language programs into machine-language programs. c) A compiler converts source-language programs into object-language programs. d) High-level languages are generally machine-dependent. False. High-level languages are generally machine-independent. e) A machine-language program requires translation before it can be run on a computer. False. A computer can directly read its own machine language. 1.5 Fill in the blanks in each of the following statements: a) Objects are containers for values and are names that reference objects. variables. [Note: This question replaces the one that appears in the first printing of Python How to Program.] b) A computer program that converts assembly-language programs to machine language programs is called a(n). assembler. c) The logical unit of the computer that receives information from outside the computer for use by the computer is called. input unit. d) The process of instructing the computer to solve specific problems is called. programming. e) Three high-level Python data types are:, and. lists, tuples, dictionaries. f) is the logical unit of the computer that sends information that has already been processed by the computer to various devices so that the information may be used outside the computer. Output unit. g) The general name for a program that converts programs written in a certain computer language into machine language is. compiler. 1.6 [CD] Fill in the blanks in each of the following statements: a) is the logical unit of the computer that retains information. Memory unit. b) is the logical unit of the computer that makes logical decisions. Arithmetic logic unit. c) The commonly used abbreviation for the computer's control unit is. CPU. d) The level of computer language most convenient to the programmer for writing programs quickly and easily is. high level. e) are mappable types keys are stored with their associated values. Dictionaries. f) The only language that a computer can directly understand is called that computer's. machine language.

7 4 Introduction to Computers, Internet and World Wide Web Appendix 1 g) The is the logical unit of the computer that coordinates the activities of all the other logical units. central processing unit. 1.7 What do each of the following acronyms stand for: a) WC. World Wide Web Consortium. b) XML. Extensible Markup Language. c) DB-API. Database Application Programming Interface. d) CGI. Common Gateway Interface. e) XHTML. Extensible HyperText Markup Language. f) TCP/IP. Transmission Control Protocol/Internet Protocol. g) PSP. Python Server Pages. h) Tcl/Tk. Tool Command Language/Tool Kit. i) SSL. Secure Socket Layer. j) HMD. Harvey Michael Deitel. 1.8 [CD] State whether each of the following is true or false. If false, explain why. a) Inheritance is a form of software reusability in which new classes are developed quickly and easily by absorbing the capabilities of existing classes and adding appropriate new capabilities. b) Pmw is a module that provides an interface to the popular Tcl/Tk graphical-user-interface toolkit. False. Tkinter is a module that provides an interface to the popular Tcl/Tk graphical-user-interface toolkit. c) Like other high-level languages, Python is generally considered to be machine-independent.

8 2 Introduction to Python Programming SELF-REVIEW EXERCISES 2.1 Fill in the blanks in each of the following: a) The statement instructs the computer to display information on the screen. print. b) A is a Python data type that contains a sequence of characters. string. c) are simply names that reference objects. Identifiers. d) The is the modulus operator. percent sign (%). e) are used to document a program and improve its readability. Comments. f) Each if structure consists of the word, the to be tested, a, and a. if, condition, colon (:), body/suite. g) The function converts non-integer values to integer values. int. h) A Python statement can be spread over multiple lines using the. line-continuation character (\). i) Arithmetic expressions enclosed in are evaluated first. parentheses. j) An object s describes the information stored in the object. type. 2.2 State whether each of the following is true or false. If false, explain why. a) The Python function get_input receives input from the user. False. The Python function raw_input receives input from the user. b) A valid Python arithmetic expression with no parentheses is evaluated left to right. False. Python arithmetic expressions are evaluated according to the rules of operator precedence and associativity not left to right.

9 6 Introduction to Python Programming Appendix 2 EXERCISES c) The following are invalid variable names: g, 87 and 2h. d) The operator!= is an example of a relational operator. False. The operator!= is an example of an equality operator. e) A variable name identifies the kind of information stored in the object. False. An object type identifies the kind of information stored in the object. f) In Python, the programmer must declare the object type before using the object in the program. False. In Python, the object type is determined as the program executes. g) If parentheses are nested, the expression in the innermost pair is evaluated first. h) Python treats the variable names, a1 and A1, as the same variable. False. Python is case sensitive, so a1 and A1 are different variables. i) The backslash character is called an escape sequence. False. The backslash is called an escape character. j) The relational operators all have the same level of precedence and evaluate left to right. 2. [CD] State the order of evaluation of the operators in each of the following Python statements and show the value of x after each statement is performed. a) x = 7 + * 6 / 2-1 x = / 2-1 x = x = 16-1 x = 15 b) x = 2 % * 2-2 / 2 x = * 2-2 / 2 x = / 2 x = x = 4-1 x = c) x = ( * 9 * ( + ( 9 * / ( ) ) ) ) x = ( * 9 * ( + ( 9 * / ) ) ) x = ( * 9 * ( + ( 27 / ) ) ) x = ( * 9 * ( + ( 9 ) ) ) x = ( * 9 * ( + 9 ) ) x = ( * 9 * ( 12 ) ) x = ( * 9 * 12 ) x = (27 * 12) x = (24) x = Write a program that requests the user to enter two numbers and prints the sum, product, difference and quotient of the two numbers. 1 # Exercise 2.4: ex02_04.py 2 # Using the arithmetic operators.

10 Appendix 2 Introduction to Python Programming 7 4 print "Enter two integers, and I will output their sum," 5 print "product, difference and quotient." 6 7 # convert the input strings to integers 8 number1 = raw_input( "Please enter first integer: " ) 9 number1 = int( number1 ) number2 = raw_input( "Please enter second integer: " ) 12 number2 = int( number2 ) 1 14 print "%d is the sum of %d and %d" \ 15 % ( number1 + number2, number1, number2 ) print "%d is the product of %d and %d" \ 18 % ( number1 * number2, number1, number2 ) print "%d is the difference between %d and %d" \ 21 % ( number1 - number2, number1, number2 ) 22 2 print "%d is the quotient of %d and %d" \ 24 % ( number1 / number2, number1, number2 ) Please enter first integer: 18 Please enter second integer: 6 24 is the sum of 18 and is the product of 18 and 6 12 is the difference between 18 and 6 is the quotient of 18 and [CD] Write a program that reads in the radius of a circle and prints the circle s diameter, circumference and area. Use the constant value for π. Do these calculations in output statements. 1 # Exercise 2.5: ex02_05.py 2 # Given the radius of a circle, output its diameter, # circumference and area. 4 5 # obtain user-entered radius 6 radius = raw_input( "Enter the circle's radius: " ) 7 radius = float( radius ) 8 9 pi = # use for pi # calculate and print the circle's diameter 12 print "Diameter is", ( 2 * radius ) 1 14 # calculate and print the circle's circumference 15 print "Circumference is", ( 2 * pi * radius ) # calculate and print the circle's area 18 print "Area is", ( pi * radius ** 2 )

11 8 Introduction to Python Programming Appendix 2 Enter the circle's radius: 8 Diameter is 16.0 Circumference is Area is Write a program that prints a box, an oval, an arrow and a diamond, as shown: ********* *** * * * * * * *** * * * * * * ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ********* *** * * 1 # Exercise 2.6: ex02_06.py 2 # Prints a box, an oval, an arrow and a diamond. 4 print """ 5 ********* *** * * 6 * * * * *** * * 7 * * * * ***** * * 8 * * * * * * * 9 * * * * * * * 10 * * * * * * * 11 * * * * * * * 12 * * * * * * * 1 ********* *** * * 14 """ ********* *** * * * * * * *** * * * * * * ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ********* *** * * 2.7 Write a program that reads in two integers and determines and prints whether the first is a multiple of the second. (Hint: Use the modulus operator.)

12 Appendix 2 Introduction to Python Programming 9 1 # Exercise 2.7: ex02_07.py 2 # Determines if the first input is a multiple of the second input. 4 number1 = raw_input( "Enter possible multiple: " ) 5 number1 = int( number1 ) 6 7 number2 = raw_input( "Enter integer: " ) 8 number2 = int( number2 ) 9 10 if number1 % number2 == 0: 11 print "%d is a multiple of %d" % ( number1, number2 ) 12 1 if number1 % number2!= 0: 14 print "%d is not a multiple of %d" % ( number1, number2 ) Enter possible multiple: 24 Enter integer: 24 is a multiple of 2.8 [CD] Give a brief answer to each of the following object think questions: a) Why does this text choose to discuss structured programming in detail before proceeding with an in-depth treatment of object-oriented programming? Objects are composed in part of structured-program pieces. b) What needs to be determined before an object-oriented program can be built? Before an object-oriented program can be built, the attributes that the object will have and the behaviors that the object will exhibit should be determined. c) How is inheritance exhibited by human beings? Each individual person (object) has unique characteristics but also shares common characteristics with all human beings (the more general class). d) What kinds of messages do people send to one another? People send each other messages, such as commands, requests, questions and responses. e) Objects send messages to one another across well-defined interfaces. What interfaces does a car radio (object) present to its user (a person object)? A car radio presents often buttons, dials and perhaps other controls that allow the user to change the station, the volume and the bass/treble. With this interface, the user does not need to know the details of the radio's operations.

13 Control Structures SELF-REVIEW EXERCISES.1 Fill in the blanks in each of the following statements: a) The if/elif/else structure is a structure. multiple-selection. b) The words if and else are examples of reserved words called Python. keywords. c) Sentinel-controlled repetition is called because the number of repetitions is not known before the loop begins executing. indefinite repetition. d) The augmented assignment symbol *= performs. multiplication and assignments. e) Function creates a sequence of integers. range. f) A procedure for solving a problem is called a(n). algorithm. g) The keyword represents an empty statement. pass. h) A set of statements within an indented code block in Python is called a. suite. i) All programs can be written in terms of three control structures, namely,, and. the sequence structure, the selection structure, the repetition structure. j) A is a graphical representation of an algorithm. flowchart..2 State whether each of the following is true or false. If false, explain why. a) Pseudocode is a simple programming language. False. Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is not a programming language. b) The if selection structure performs an indicated action when the condition is true.

14 Appendix Control Structures 11 EXERCISES c) The if/else selection structure is a single-selection structure. False. The if/else selection structure is a double-selection structure it selects between two different actions. d) A fatal logic error causes a program to execute and produce incorrect results. False. A fatal logic error causes a program to terminate. e) A repetition structure performs the statements in its body while some condition remains true. f) Function float converts its argument to a floating-point value. g) The exponentiation operator ** associates left to right. False. The exponentiation operator associates from right to left. h) Function call range( 1, 10 ) returns the sequence 1 to 10, inclusive. False. Function call range( 1, 10) returns the sequence 1 9, inclusive. i) Sentinel-controlled repetition uses a counter variable to control the number of times a set of instructions executes. False. Counter-controlled repetition uses a counter variable to control the number of repetitions; sentinel-control repetition waits for a sentinel value (also called a flag, dummy or signal value) to stop repetition. j) The symbol = tests for equality. False. The operator == tests for equality; the symbol = is for assignment.. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a Python program that prompts the user to input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankful (= total miles driven divided by total gallons used). 1 # Exercise.: ex0_0.py 2 # Calculates miles per gallon. 4 # initialization phase 5 gallons = 0 # number of gallons per tank 6 totalgallons = 0 # total number of gallons used 7 miles = 0 # miles drive per tank 8 totalmiles = 0 # total miles driven 9 10 while -1: # assign gallons used for one tankful to gallons 1 gallons = raw_input( "Enter the gallons used (-1 to end): " ) 14 gallons = float( gallons ) # terminate on sentinel value 17 if gallons == -1: 18 break 19

15 12 Control Structures Appendix 20 # assign miles driven for one tankful to miles 21 miles = raw_input( "Enter the miles driven: " ) 22 miles = float( miles ) 2 24 # increment total number of miles, gallons and tanks 25 totalmiles += miles 26 totalgallons += gallons # prints the mileage for this tank 29 print "The miles / gallon for this tank was", ( miles / gallons ) 0 1 # prints the total mileage 2 if totalgallons!= 0: # if no user input, avoid division by zero print "The overall average miles / gallon was", \ 4 ( totalmiles / totalgallons ) 5 else: 6 print "Attempted to divide by zero gallons." Enter the gallons used (-1 to end): 12.8 Enter the miles driven: 287 The miles / gallon for this tank was Enter the gallons used (-1 to end): 10. Enter the miles driven: 200 The miles / gallon for this tank was Enter the gallons used (-1 to end): 5 Enter the miles driven: 120 The miles / gallon for this tank was 24.0 Enter the gallons used (-1 to end): -1 The overall average miles/gallon was [CD] A palindrome is a number or a text phrase that reads the same backwards or forwards. For example, each of the following five-digit integers is a palindrome: 1221, 55555, and Write a program that reads in a five-digit integer and determines whether it is a palindrome. (Hint: Use the division and modulus operators to separate the number into its individual digits.) 1 # Exercise.4: ex0_04.py 2 # Determines if a five-digit integer is a palindrome. 4 number = raw_input( "Please enter a five-digit integer: " ) 5 number = int( number ) 6 7 # isolate last digit 8 lastdigit = number % # isolate fourth digit 11 number = number / fourthdigit = number % # skip middle digit 15 number = number /

16 Appendix Control Structures 1 17 # isolate second digit 18 seconddigit = number % # isolate first digit 21 firstdigit = number / # number is palindrome if first and last digits match and 24 # if second and fourth digits match 25 if ( firstdigit == lastdigit ) and ( seconddigit == fourthdigit ): 26 print "The number is a palindrome." 27 else: 28 print "The number is not a palindrome." Please enter a five-digit integer: The number is a palindrome. Please enter a five-digit integer: 1245 The number is not a palindrome..5 [CD] Input an integer containing 0s and 1s (i.e., a binary integer) and print its decimal equivalent. Appendix C, Number Systems, discusses the binary number system. (Hint: Use the modulus and division operators to pick off the binary number s digits one at a time from right to left. Just as in the decimal number system, where the rightmost digit has the positional value 1 and the next digit leftward has the positional value 10, then 100, then 1000, etc., in the binary number system, the rightmost digit has a positional value 1, the next digit leftward has the positional value 2, then 4, then 8, etc. Thus, the decimal number 24 can be interpreted as 2 * * * 1. The decimal equivalent of binary 1101 is 1 * * * * 1.) 1 # Exercise.5: ex0_05.py 2 # Converts a binary number into its decimal equivalent. 4 binarynumber = raw_input( "Please enter a binary number: " ) 5 binarynumber = int( binarynumber ) 6 7 decimalvalue = 0 # holds the decimal value 8 digitposition = 0 # position in binary number 9 10 while binarynumber > 0: # isolate current last digit 1 lastdigit = binarynumber % binarynumber = binarynumber / # calculate value of position if 17 # the last digit is not equal to zero 18 if lastdigit!= 0: 19 counter = # value of position in binary number

17 14 Control Structures Appendix 22 positionvalue = 2 ** digitposition 2 24 # update value of binary digit 25 decimalvalue += lastdigit * positionvalue # move to next digit position 28 digitposition += # print decimal value of binary input 1 print "Decimal Equivalent is " + str( decimalvalue ) Please enter a binary number: 1000 Decimal Equivalent is 8 Please enter a binary number: 1101 Decimal Equivalent is 1 Please enter a binary number: Decimal Equivalent is 11.6 The factorial of a nonnegative integer n is written n! (pronounced n factorial ) and is defined as follows: n! = n (n - 1) (n - 2) 1 (for values of n greater than or equal to 1) and n! = 1 (for n = 0). For example, 5! = , which is 120. Factorials increase in size very rapidly. What is the largest factorial that your program can calculate before leading to an overflow error? a) Write a program that reads a nonnegative integer and computes and prints its factorial. 1 # Exercise.6: ex0_06a.py 2 # Calculates the factorial of a positive integer. 4 # input value 5 number = raw_input( "Enter a non-negative integer: " ) 6 number = int( number ) 7 8 factorial = 1 # holds value of factorial 9 10 # compute factorial by iterating backwards 11 for n in range( number, 1, -1 ): 12 factorial *= n 1 14 print str( number) + "! is " + str( factorial )

18 Appendix Control Structures 15 Enter a positive integer: 5 5! is 120 b) Write a program that estimates the value of the mathematical constant e by using the formula: e = ! 2!! 1 # Exercise.6: ex0_06b.py 2 # Calculates the mathematical constant e. 4 e = 1.0 # estimate of e 5 6 for counter in range ( 1, 10 ): 7 factorial = 1.0 # holds factorial 8 9 # calculate each factorial 10 for n in range ( counter, 1, -1 ): 11 factorial *= n 12 1 # summation of reciprocal of each factorial calculation 14 e += ( 1 / factorial ) print "An estimate of e is", e An estimate of e is c) Write a program that computes the value of e x by using the formula e x = x ! 2!! x 2 x 1 # Exercise.6: ex0_06c.py 2 # Estimates the value of e^x. 4 # input power of x 5 exponent = raw_input( "Please enter x (for e^x): " ) 6 exponent = int( exponent ) 7 8 e = 1.0 # estimate of e 9 10 # compute estimate of e^x 11 for counter in range ( 1, 10 ): 12 factorial = 1.0 # holds factorial

19 16 Control Structures Appendix 1 14 # calculates each factorial 15 for n in range ( counter, 1, -1 ): 16 factorial *= n # raise exponent to counter and assign value to x 19 x = exponent ** counter # summation of reciprocal of each factorial calculation 22 e += ( x / factorial ) 2 24 print "An estimate of e^x is", e Please enter x (for e^x): 0 An estimate of e^x is 1.0 Please enter x (for e^x): 1 An estimate of e^x is Please enter x (for e^x): An estimate of e^x is [CD] Write a program that prints the following patterns separately, one below the other each pattern separated from the next by one blank line. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form print '*', (which causes the asterisks to print side by side separated by a space). (Hint: The last two patterns require that each line begin with an appropriate number of blanks.) Extra credit: Combine your code from the four separate problems into a single program that prints all four patterns side by side by making clever use of nested for loops. For all parts of this program minimize the numbers of asterisks and spaces and the number of statements that print these characters. (A) (B) (C) (D) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1 # Exercise.7: ex0_07.py

20 Appendix Control Structures 17 2 # Prints patterns of asterisks. 4 for counter in range( 1, 12 ): 5 6 # prints counter asterisks on each line 7 for x in range( 1, counter ): 8 print "*", 9 print # prints a new line print # print a blank line 12 1 for counter in range( 1, 12 ): # prints 11 - counter asterisks on each line 16 for x in range ( 11, counter, -1 ): 17 print "*", 18 print # new line for counter in range ( 1, 11 ): # prints counter - 1 spaces on each line 2 for x in range ( 1, counter ): 24 print " ", # prints 11 - counter asterisks on each line 27 for x in range ( 11, counter, -1 ): 28 print "*", 29 print 0 1 for counter in range ( 1, 12 ): 2 # prints 11 - counter spaces on each line 4 for x in range ( 11, counter, -1 ): 5 print " ", 6 7 # prints counter asterisks on each line 8 for x in range ( 1, counter ): 9 print "*", 40 print

21 18 Control Structures Appendix * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *.8 (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2 and hypotenuse all no larger than 20. Use a triple-nested for loop that tries all possibilities. This is an example of brute force computing. You will learn in more advanced computer science courses that there are many interesting problems for which there is no known algorithmic approach other than using sheer brute force.

22 Appendix Control Structures 19 1 # Exercise.8: ex0_08.py 2 # Pythagorean triplets. 4 # test 20 possible hypotenuses 5 for hypotenuse in range ( 1, 21 ): 6 hypotenusesquared = hypotenuse * hypotenuse 7 8 # test 20 possibilities for side1 9 for side1 in range ( 1, 21 ): # test 20 possibilities for side2 12 for side2 in range ( 1, 21 ): 1 14 side1squared = side1 ** 2 15 side2squared = side2 ** # if the sum of the squares of the two sides is equal 18 # to the square of the hypotenuse, display Pythagorean 19 # triplet 20 if hypotenusesquared == side1squared + side2squared: 21 print "hypotenuse =", hypotenuse 22 print "side1 =", side1 2 print "side2 =", side2

23 20 Control Structures Appendix hypotenuse = 5 side1 = side2 = 4 hypotenuse = 5 side1 = 4 side2 = hypotenuse = 10 side1 = 6 side2 = 8 hypotenuse = 10 side1 = 8 side2 = 6 hypotenuse = 1 side1 = 5 side2 = 12 hypotenuse = 1 side1 = 12 side2 = 5 hypotenuse = 15 side1 = 9 side2 = 12 hypotenuse = 15 side1 = 12 side2 = 9 hypotenuse = 17 side1 = 8 side2 = 15 hypotenuse = 17 side1 = 15 side2 = 8 hypotenuse = 20 side1 = 12 side2 = 16 hypotenuse = 20 side1 = 16 side2 = 12

24 4 Functions SELF-REVIEW EXERCISES 4.1 Fill in the blanks in each of the following statements. a) Constructing a large program from smaller components is called. divide and conquer. b) Components in Python are called,, and. functions, classes, modules, packages. c) Pre-packaged functions or classes are available in Python. modules. d) The module functions allow programmers to perform common mathematical calculations. math. e) The indented statements that follow a statement form a function body. def. f) The in a function call is the operator that causes the function to be called. pair of parentheses. g) The module introduces the element of chance into Python programs. random. h) A program can obtain the name of its module through identifier. name. i) During code execution, three namespaces can be accessed:, and. the local namespace, the global namespace, the built-in namespace. j) A recursive function converges on the. base case. 4.2 State whether each of the following is true or false. If false, explain why. a) All variables declared in a function are global to the program containing the function. False. All variables declared in a function are local known only in the function in which they are defined.

25 22 Functions Appendix 4 EXERCISES b) An import statement must be included for every module function used in a program. False. Functions included in the builtin module do not need to be imported. c) Function fmod returns the floating-point remainder of its two arguments. d) The keyword return displays the result of a function. False. Keyword return passes control and optionally, the values of an expression, back to the point from which the function was called. e) A function s parameter list is a comma-separated list containing the names of the parameters received by the function when it is called. f) Function call random.randrange ( 1, 7 ) produces a random integer in the range 1 to 7, inclusive. False. Function call random.randrange ( 1, 7 ) produces a random integer in the range from 1 to 6, inclusive. g) An identifier s scope is the portion of the program in which the identifier has meaning. h) Every call to a recursive function is a recursive call. False. The initial call to the recursive function is not recursive. i) Omitting the base case in a recursive function can lead to infinite recursion. j) A recursive function may call itself indirectly. 4. [CD] Implement the following function fahrenheit to return the Fahrenheit equivalent of a Celsius temperature. F = 9 -- C+ 2 5 Use this function to write a program that prints a chart showing the Fahrenheit equivalents of all Celsius temperatures degrees. Use one position of precision to the right of the decimal point for the results. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable. 1 # Exercise 4.: ex04_0.py 2 # Print charts showing Fahrenheit and Celsius temperatures. 4 # convert a Celsius temperature to Fahrenheit 5 def fahrenheit( celsiustemperature ): 6 return ( 9.0 / 5 ) * celsiustemperature # print table headers 9 print "Celsius Fahrenheit Celsius Fahrenheit" # print Celsius to Fahrenheit from 0 to 100 degrees 12 # use four columns to minimize the number of lines of output 1 for ctemperature in range( 0, 50 ):

26 Appendix 4 Functions 2 14 print "%7.0f %11.1f" % \ 15 ( ctemperature, fahrenheit( ctemperature ) ), 16 ctemperature += print "%11.0f %11.1f" % \ 18 ( ctemperature, fahrenheit( ctemperature ) ) ctemperature += print "%1.0f %11.1f" % \ 2 ( ctemperature, fahrenheit( ctemperature ) )

27 24 Functions Appendix 4 Celsius Fahrenheit Celsius Fahrenheit

28 Appendix 4 Functions An integer greater than 1 is said to be prime if it is divisible by only 1 and itself. For example, 2,, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not. a) Write a function that determines whether a number is prime. 1 # Exercise 4.4a: ex04_04a.py 2 # Determines if a number is prime. 4 # returns 1 if number is prime 5 def isprime( number ): 6 7 for x in range( 2, number ): 8 9 # return 0 if x is a factor of number 10 if number % x == 0: 11 return return 1 b) Use this function in a program that determines and prints all the prime numbers between 2 and 1, # Exercise 4.4b: ex04_04b.py 2 # Determines and prints all prime numbers between 2 and 1, # returns 1 if number is prime 5 def isprime( number ): 6 7 for x in range( 2, number ): 8 9 # return 0 if x is a factor of number 10 if number % x == 0: 11 return return # test numbers between 2 and 1, for number in range( 2, 1001 ): # display the number if it is prime 19 if isprime( number ): 20 print number,

29 26 Functions Appendix c) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need go only as high as the square root of n. Rewrite the program and run it both ways to show that you get the same result. 1 # Exercise 4.4c: ex04_04c.py 2 # Determines and prints all prime numbers between 2 and 1, import math 5 6 # returns 1 if number is prime 7 def isprime( number ): 8 9 # test only up to the square root of the number 10 for x in range( 2, math.sqrt( number ) + 1 ): # return 0 if x is a factor of number 1 if number % x == 0: 14 return return # test numbers between 2 and 1, for number in range( 2, 1001 ): # display number if it is prime 22 if isprime( number ): 2 print number,

30 Appendix 4 Functions An integer number is said to be a perfect number if the sum of its factors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = Write a function perfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and Print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than # Exercise 4.5: ex04_05.py 2 # Displays the perfect numbers from 1 to 1000 and their factors. 4 # returns 1 if a number is perfect, 0 if not 5 def perfect( number ): 6 factorsum = 0 # holds the sum of factors 7 8 # test each possible factor from 1 to number - 1, inclusive 9 for possiblefactor in range( 1, number ): # sum factors 12 if number % possiblefactor == 0: 1 factorsum += possiblefactor if factorsum == number: 16 return 1 # perfect number 17 else: 18 return # prints a number with its factors in the form: 21 # number = factor1 + factor def displayfactors( number ): 2 24 print number, "=", for possiblefactor in range( 1, number ): if number % possiblefactor == 0: 29 print possiblefactor, 0 1 print # new line 2 # displays the perfect numbers from 1 to for integer in range( 1, 1000 ): 5 6 if perfect( integer ): 7 displayfactors( integer ) 6 = = =

Instructor s Manual. Perl How to Program

Instructor s Manual. Perl How to Program Instructor s Manual for Perl How to Program Harvey M. Deitel Paul J. Deitel Tem R. Nieto David C. McPhie 2001 Deitel & Associates, Inc. and Prentice Hall. All Rights Reserved. Contents Preface iv 1 Introduction

More information

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

[Page 177 (continued)] a. if ( age >= 65 ); cout << Age is greater than or equal to 65 << endl; else cout << Age is less than 65 << endl; Page 1 of 10 [Page 177 (continued)] Exercises 4.11 Identify and correct the error(s) in each of the following: a. if ( age >= 65 ); cout

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d. Gaddis: Starting Out with Python, 2e - Test Bank Chapter Two MULTIPLE CHOICE 1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

More information

download instant at Introduction to C++

download instant at  Introduction to C++ Introduction to C++ 2 Programming: Solutions What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would

More information

LEC03: Decisions and Iterations

LEC03: Decisions and Iterations LEC03: Decisions and Iterations Q1. Identify and correct the errors in each of the following statements: a) if ( c < 7 ); System.out.println( "c is less than 7" ); b) if ( c => 7 ) System.out.println(

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

Control Statements: Part 1

Control Statements: Part 1 4 Let s all move one place on. Lewis Carroll Control Statements: Part 1 The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert Frost All

More information

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python CS95003 - Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / 2014 Subjects 1) Beginning with Python 2) Variables 3) Strings 4) Basic arithmetic operators 5) Flow control 6) Comparison

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

Full file at

Full file at 2 Introduction to C Programming Solutions What s in a name? That which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

Fundamentals of Programming Session 13

Fundamentals of Programming Session 13 Fundamentals of Programming Session 13 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Introduction to Java Applications

Introduction to Java Applications 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the most fun? Peggy Walker Take some more

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with

More information

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1 Review Midterm Exam 1 Review Midterm Exam 1 Exam on Monday, October 7 Data Types and Variables = Data Types and Variables Basic Data Types Integers Floating Point Numbers Strings Data Types and Variables

More information

142

142 Scope Rules Thus, storage duration does not affect the scope of an identifier. The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned

More information

Object Oriented Programming with Java

Object Oriented Programming with Java Object Oriented Programming with Java What is Object Oriented Programming? Object Oriented Programming consists of creating outline structures that are easily reused over and over again. There are four

More information

Python Programming Exercises 1

Python Programming Exercises 1 Python Programming Exercises 1 Notes: throughout these exercises >>> preceeds code that should be typed directly into the Python interpreter. To get the most out of these exercises, don t just follow them

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

PLD Semester Exam Study Guide Dec. 2018

PLD Semester Exam Study Guide Dec. 2018 Covers material from Chapters 1-8. Semester Exam will be built from these questions and answers, though they will be re-ordered and re-numbered and possibly worded slightly differently than on this study

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e Before writing a program to solve a particular problem, it s essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. The

More information

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation. Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements

More information

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd 19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd Will you walk a little faster? said a whiting to a snail, There s a porpoise close behind us, and he s treading

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

More information

Name Period Date. REAL NUMBER SYSTEM Student Pages for Packet 3: Operations with Real Numbers

Name Period Date. REAL NUMBER SYSTEM Student Pages for Packet 3: Operations with Real Numbers Name Period Date REAL NUMBER SYSTEM Student Pages for Packet : Operations with Real Numbers RNS. Rational Numbers Review concepts of experimental and theoretical probability. a Understand why all quotients

More information

Chapter 5 : Repetition (pp )

Chapter 5 : Repetition (pp ) Page 1 of 41 Printer Friendly Version User Name: Stephen Castleberry email Id: scastleberry@rivercityscience.org Book: A First Book of C++ 2007 Cengage Learning Inc. All rights reserved. No part of this

More information

Full file at C How to Program, 6/e Multiple Choice Test Bank

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee 1 0 1 0 Foundation Topics 1 0 Chapter 1 - Introduction to Programming 1 1 Systems Development Life Cycle N/A N/A N/A N/A N/A N/A 1-8 12-13 1 2 Bloodshed Dev-C++ 5 Compiler/IDE N/A N/A N/A N/A N/A N/A N/A

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps Overview By the end of the lab, you will be able to: use fscanf() to accept inputs from the user and use fprint() for print statements to the

More information

Python lab session 1

Python lab session 1 Python lab session 1 Dr Ben Dudson, Department of Physics, University of York 28th January 2011 Python labs Before we can start using Python, first make sure: ˆ You can log into a computer using your username

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

Here n is a variable name. The value of that variable is 176.

Here n is a variable name. The value of that variable is 176. UNIT II DATA, EXPRESSIONS, STATEMENTS 9 Python interpreter and interactive mode; values and types: int, float, boolean, string, and list; variables, expressions, statements, tuple assignment, precedence

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

VBScript: Math Functions

VBScript: Math Functions C h a p t e r 3 VBScript: Math Functions In this chapter, you will learn how to use the following VBScript functions to World Class standards: 1. Writing Math Equations in VBScripts 2. Beginning a New

More information

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration STATS 507 Data Analysis in Python Lecture 2: Functions, Conditionals, Recursion and Iteration Functions in Python We ve already seen examples of functions: e.g., type()and print() Function calls take the

More information

9. Elementary Algebraic and Transcendental Scalar Functions

9. Elementary Algebraic and Transcendental Scalar Functions Scalar Functions Summary. Introduction 2. Constants 2a. Numeric Constants 2b. Character Constants 2c. Symbol Constants 2d. Nested Constants 3. Scalar Functions 4. Arithmetic Scalar Functions 5. Operators

More information

Fundamentals of Programming. Lecture 6: Structured Development (part one)

Fundamentals of Programming. Lecture 6: Structured Development (part one) Fundamentals of Programming Lecture 6: Structured Development (part one) Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu edu Sharif University of Technology Computer Engineering Department Outline Algorithms

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

In this chapter you ll learn:

In this chapter you ll learn: Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd Will you walk a little faster? said a whiting to a snail, There s a porpoise close behind us, and he s treading on

More information

BCA-105 C Language What is C? History of C

BCA-105 C Language What is C? History of C C Language What is C? C is a programming language developed at AT & T s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. C seems so popular is because it is

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming Lecture Numbers Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To understand the concept of data types To be familiar with the basic numeric data types in Python To be able

More information

Contents. 1 Introduction to Computers, Internet and World Wide Web 1

Contents. 1 Introduction to Computers, Internet and World Wide Web 1 pythonhtp1_01toc.fm Page viii Monday, January 14, 2002 12:20 PM Preface xxxvii 1 Introduction to Computers, Internet and World Wide Web 1 1.1 Introduction 2 1.2 What Is a Computer? 3 1.3 Computer Organization

More information

>>> * *(25**0.16) *10*(25**0.16)

>>> * *(25**0.16) *10*(25**0.16) #An Interactive Session in the Python Shell. #When you type a statement in the Python Shell, #the statement is executed immediately. If the #the statement is an expression, its value is #displayed. #Lines

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

Computers Programming Course 6. Iulian Năstac

Computers Programming Course 6. Iulian Năstac Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

And Parallelism. Parallelism in Prolog. OR Parallelism

And Parallelism. Parallelism in Prolog. OR Parallelism Parallelism in Prolog And Parallelism One reason that Prolog is of interest to computer scientists is that its search mechanism lends itself to parallel evaluation. In fact, it supports two different kinds

More information

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. Plan for the rest of the semester: Programming We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. We saw earlier that computers

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Fundamentals of Programming (Python) Getting Started with Programming

Fundamentals of Programming (Python) Getting Started with Programming Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

Chapter 2, Part III Arithmetic Operators and Decision Making

Chapter 2, Part III Arithmetic Operators and Decision Making Chapter 2, Part III Arithmetic Operators and Decision Making C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 2016 Pearson Education, Ltd. All rights reserved. 2 2016 Pearson

More information

CS 102 Lab 3 Fall 2012

CS 102 Lab 3 Fall 2012 Name: The symbol marks programming exercises. Upon completion, always capture a screenshot and include it in your lab report. Email lab report to instructor at the end of the lab. Review of built-in functions

More information

[CHAPTER] 1 INTRODUCTION 1

[CHAPTER] 1 INTRODUCTION 1 FM_TOC C7817 47493 1/28/11 9:29 AM Page iii Table of Contents [CHAPTER] 1 INTRODUCTION 1 1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information Processing...2 1.1.1 Algorithms...2 1.1.2

More information

Introductionto C++ Programming

Introductionto C++ Programming What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the most fun? Peggy Walker Take some more

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python + Condition Controlled Loops Introduction to Programming - Python + Repetition Structures n Programmers commonly find that they need to write code that performs the same task over and over again + Example:

More information

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 9/e Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Copyright 1992-2012 by Pearson Copyright 1992-2012 by Pearson Before writing a program to solve a problem, have

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

Microsoft Visual Basic 2005: Reloaded

Microsoft Visual Basic 2005: Reloaded Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations Objectives After studying this chapter, you should be able to: Declare variables and named

More information

Chapter 3 Problem Solving and the Computer

Chapter 3 Problem Solving and the Computer Chapter 3 Problem Solving and the Computer An algorithm is a step-by-step operations that the CPU must execute in order to solve a problem, or to perform that task. A program is the specification of an

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 1 Professional Program: Data Administration and Management JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) WHO

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

JAVASCRIPT FOR PROGRAMMERS

JAVASCRIPT FOR PROGRAMMERS JAVASCRIPT FOR PROGRAMMERS DEITEL DEVELOPER SERIES Paul J. Deitel Deitel & Associates, Inc. Harvey M. Deitel Deitel & Associates, Inc. PRENTICE HALL Upper Saddle River, NJ Boston Indianapolis San Francisco

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

Fundamentals: Expressions and Assignment

Fundamentals: Expressions and Assignment Fundamentals: Expressions and Assignment A typical Python program is made up of one or more statements, which are executed, or run, by a Python console (also known as a shell) for their side effects e.g,

More information

https://asd-pa.perfplusk12.com/admin/admin_curric_maps_display.aspx?m=5507&c=618&mo=18917&t=191&sy=2012&bl...

https://asd-pa.perfplusk12.com/admin/admin_curric_maps_display.aspx?m=5507&c=618&mo=18917&t=191&sy=2012&bl... Page 1 of 13 Units: - All - Teacher: ProgIIIJavaI, CORE Course: ProgIIIJavaI Year: 2012-13 Intro to Java How is data stored by a computer system? What does a compiler do? What are the advantages of using

More information

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar Examples of Software Programming and Data Structure Lecture 2 Sudeshna Sarkar Read an integer and determine if it is a prime number. A Palindrome recognizer Read in airline route information as a matrix

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved.

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved. 1 7 JavaScript: Control Statements I 2 Let s all move one place on. Lewis Carroll The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert

More information

CS112 Lecture: Repetition Statements

CS112 Lecture: Repetition Statements CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general

More information

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming Lecture 7 Control Structure I (Repetition) Part I C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer

More information

Repetition Structures Chapter 9

Repetition Structures Chapter 9 Sum of the terms Repetition Structures Chapter 9 1 Value of the Alternating Harmonic Series 0.9 0.8 0.7 0.6 0.5 10 0 10 1 10 2 10 3 Number of terms Objectives After studying this chapter you should be

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved. Internet & World Wide Web How to Program, 5/e Sequential execution Execute statements in the order they appear in the code Transfer of control Changing the order in which statements execute All scripts

More information

Chapter 2: Overview of C++

Chapter 2: Overview of C++ Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman C++ Background Introduced by Bjarne Stroustrup of AT&T s Bell Laboratories in

More information

Computer Programming C++ (wg) CCOs

Computer Programming C++ (wg) CCOs Computer Programming C++ (wg) CCOs I. The student will analyze the different systems, and languages of the computer. (SM 1.4, 3.1, 3.4, 3.6) II. The student will write, compile, link and run a simple C++

More information