© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Mathematical Functions, Strings, and Objects. Introduction ■ To solve mathematics problems by using the functions in the math module (§3.2). ■ To represent.
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Introduction to Python
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
1 Chapter 2: Elementary Programming Shahriar Hossain.
Introduction to Java Programming, 4E Y. Daniel Liang.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Devon M. Simmonds University of North Carolina, Wilmington
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Chapter 4: Mathematical Functions, Characters, and Strings
Topics Designing a Program Input, Processing, and Output
OUTPUT STATEMENTS GC 201.
INPUT & OUTPUT scanf & printf.
Chapter 4: Mathematical Functions, Characters, and Strings
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Topics Designing a Program Input, Processing, and Output
Chapter 3 Mathematical Functions, Strings, and Objects
functions: argument, return value
Topics Designing a Program Input, Processing, and Output
Java Programming Language
Topics Designing a Program Input, Processing, and Output
COMPUTER PROGRAMMING SKILLS
Chapter 2 Primitive Data Types and Operations
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 2 Motivations Suppose you need to estimate the area enclosed by four cities, given the GPS locations (latitude and longitude) of these cities, as shown in the following diagram. How would you write a program to solve this problem? You will be able to write such a program after completing this chapter.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 3 Objectives F To solve mathematics problems by using the functions in the math module (§3.2). F To represent and process strings and characters (§§ ). F To encode characters using ASCII and Unicode (§§ ). F To use the ord to obtain a numerical code for a character and chr to convert a numerical code to a character (§3.3.3). F To represent special characters using the escape sequence (§3.3.4). F To invoke the print function with the end argument (§3.3.5). F To convert numbers to a string using the str function (§3.3.6). F To use the + operator to concatenate strings (§3.3.7). F To read strings from the console (§3.3.8). F To introduce objects and methods (§3.5). F To format numbers and strings using the format function (§3.6). F To draw various shapes (§3.7). F To draw graphics with colors and fonts (§3.8).

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 4 Built-in Functions and math Module >>> max(2, 3, 4) # Returns a maximum number 4 >>> min(2, 3, 4) # Returns a minimu number 2 >>> round(3.51) # Rounds to its nearest integer 4 >>> round(3.4) # Rounds to its nearest integer 3 >>> abs(-3) # Returns the absolute value 3 >>> pow(2, 3) # Same as 2 ** 3 8

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 5 The math Functions MathFunctions Run

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 6 Problem: Compute Angles Given three points of a triangle, you can compute the angles using the following formula: ComputeAngles Run

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 7 Strings and Characters letter = 'A' # Same as letter = "A" numChar = '4' # Same as numChar = "4" message = "Good morning" # Same as message = 'Good morning' A string is a sequence of characters. String literals can be enclosed in matching single quotes (') or double quotes ("). Python does not have a data type for characters. A single-character string represents a character.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 8 NOTE For consistency, this book uses double quotes for a string with more than one character and single quotes for a string with a single character or an empty string. This convention is consistent with other programming languages. So, it will be easy to convert a Python program to a program written in other languages.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 9 Unicode and ASCII Code Python characters use Unicode, a 16-bit encoding scheme Python supports Unicode. Unicode is an encoding scheme for representing international characters. ASCII is a small subset of Unicode. DisplayUnicode Run

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 10 Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 11 ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 12 Functions ord and chr >>> ch = 'a' >>> ord(ch) >>> 97 >>> chr(98) >>> 'b'

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 13 Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 Linefeed \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \ ' \u0027 Double Quote \ " \u0022

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 14 Printing without the Newline print(item, end = 'anyendingstring') print("AAA", end = ' ') print("BBB", end = '') print("CCC", end = '***') print("DDD", end = '***')

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 15 The str Function The str function can be used to convert a number into a string. For example, >>> s = str(3.4) # Convert a float to string >>> s '3.4' >>> s = str(3) # Convert an integer to string >>> s '3' >>>

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 16 The String Concatenation Operator You can use the + operator add two numbers. The + operator can also be used to concatenate (combine) two strings. Here are some examples: >>> message = "Welcome " + "to " + "Python" >>> message 'Weclome to Python' >>> chapterNo = 2 >>> s = "Chapter " + str(chapterNo) >>> s 'Chapter 2' >>>

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 17 Reading Strings from the Console To read a string from the console, use the input function. For example, the following code reads three strings from the keyboard: s1 = input("Enter a string: ") s2 = input("Enter a string: ") s3 = input("Enter a string: ") print("s1 is " + s1) print("s2 is " + s2) print("s3 is " + s3)

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 18 Case Study: Minimum Number of Coins This program lets the user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters, dimes, nickels, and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. ComputeChangeRun

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 19 Introduction to Objects and Methods In Python, all data—including numbers and strings— are actually objects. An object is an entity. Each object has an id and a type. Objects of the same kind have the same type. You can use the id function and type function to get these information for an object.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 20 Object Types and Ids The id and type functions are rarely used in programming, but they are good pedagogical tools for understanding objects. >>> n = 3 # n is an integer >>> id(n) >>> type(n) >>> f = 3.0 # f is a float >>> id(f) >>> type(f) >>> s = "Welcome" # s is a string >>> id(s) >>> type(s)

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 21 OOP and str Objects The id and type functions are rarely used in programming, but they are good pedagogical tools for understanding objects.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 22 Object vs. Object reference Variable For n = 3, we say n is an integer variable that holds value 3. Strictly speaking, n is a variable that references an int object for value 3. For simplicity, it is fine to say n is an int variable with value 3.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 23 Methods You can perform operations on an object. The operations are defined using functions. The functions for the objects are called methods in Python. Methods can only be invoked from a specific object. For example, the string type has the methods such as lower() and upper(), which returns a new string in lowercase and uppercase. Here are the examples to invoke these methods:

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 24 str Object Methods >>> s = "Welcome" >>> s1 = s.lower() # Invoke the lower method >>> s1 'welcome' >>> s2 = s.upper() # Invoke the upper method >>> s2 'WELCOME'

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 25 Striping beginning and ending Whitespace Characters Another useful string method is strip(), which can be used to strip the whitespace characters from the both ends of a string. >>> s = "\t Welcome \n" >>> s1 = s.strip() # Invoke the strip method >>> s1 'Welcome'

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 26 Formatting Numbers and Strings Often it is desirable to display numbers in certain format. For example, the following code computes the interest, given the amount and the annual interest rate. The format function formats a number or a string and returns a string. format(item, format-specifier)

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 27 Formatting Floating-Point Numbers

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 28 Formatting in Scientific Notation If you change the conversion code from f to e, the number will be formatted in scientific notation. For example, print(format( , '10.2e')) print(format( , '10.2e')) print(format(57.4, '10.2e')) print(format(57, '10.2e'))

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 29 Formatting as a Percentage You can use the conversion code % to format numbers as a percentage. For example, print(format( , '10.2%')) print(format( , '10.2%')) print(format(7.4, '10.2%')) print(format(57, '10.2%'))

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 30 Justifying Format By default, the format is right justified. You can put the symbol < in the format specifier to specify that the item is a left justified in the resulting format within the specified width. For example, print(format( , '10.2f')) print(format( , '<10.2f'))

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 31 Formatting Integers You can use the conversion code d, x, o, and b to format an integer in decimal, hexadecimal, octal, or binary. You can specify a width for the conversion. For example, print(format(59832, '10d')) print(format(59832, '<10d')) print(format(59832, '10x')) print(format(59832, '<10x'))

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 32 Formatting Strings You can use the conversion code s to format a string with a specified width. For example, print(format("Welcome to Python", '20s')) print(format("Welcome to Python", '<20s')) print(format("Welcome to Python", '>20s'))

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 33 Drawing Various Shapes A turtle contains methods for moving the pen and setting the pen’s size and speed.

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 34 Turtle Pen Drawing State Methods

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 35 Turtle Motion Methods

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 36 Problem: Draw Simple Shapes SimpleShapesRun

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 37 Turtle Drawing with Colors and Fonts

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 38 Drawing with Colors and Fonts ColorShapes A turtle object contains the methods for setting colors and fonts.