1 / 37

Mathematical Functions, Strings, and Objects

Mathematical Functions, Strings, and Objects. Introduction. ■ To solve mathematics problems by using the functions in the math module (§3.2). ■ To represent and process strings and characters (§§3.3–3.4). ■ To encode characters using ASCII and Unicode (§§3.3.1–3.3.2).

gwidon
Download Presentation

Mathematical Functions, Strings, and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mathematical Functions, Strings, and Objects

  2. Introduction ■ To solve mathematics problems by using the functions in the math module (§3.2). ■ To represent and process strings and characters (§§3.3–3.4). ■ To encode characters using ASCII and Unicode (§§3.3.1–3.3.2). ■ To use the ordfunction to obtain a numerical code for a character and the chrfunction to convert a numerical code to a character (§3.3.3). ■ To represent special characters using the escape sequence (§3.3.4). ■ To invoke the print function with the end argument (§3.3.5). ■ To convert numbers to a string using the strfunction (§3.3.6). ■ To use the + operator to concatenate strings (§3.3.7). ■ To read strings from the keyboard (§3.3.8). ■ To introduce objects and methods (§3.5). ■ To format numbers and strings using the format function (§3.6). ■ To draw various shapes (§3.7). ■ To draw graphics with colors and fonts (§3.8).

  3. CommonPythonFunctions

  4. Forexample

  5. MathFunctions.py

  6. Computethe angles by using the following formula

  7. ComputeAngles.py importmath x1, y1, x2, y2, x3, y3 = eval(input("Enter three points: ")) a = math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)) b = math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)) c = math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c))) B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c))) C = math.degrees(math.acos((c * c - b * b - a * a) / (-2 * a * b))) print("The three angles are ", round(A * 100) / 100.0, round(B * 100) / 100.0, round(C * 100) / 100.0)

  8. StringsandCharacters A string (described in Chapter 1) is a sequence of characters. Python treats charactersand strings the same way In addition to processing numeric values, you can process strings in Python. A string is asequence of characters and can include text and numbers. String values must 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. For example: letter = 'A' # Same as letter = "A" numChar = '4' # Same as numChar = "4" message = "Good morning" # Same as message = 'Good morning'

  9. UnicodeCode

  10. DisplayUnicode.py importturtle turtle.write("\u6B22\u8FCE \u03b1 \u03b2 \u03b3") turtle.done()

  11. The ordand chrFunctions Python provides the ord(ch) function for returning the ASCII code for the character chand the chr(code) function for returning the haracterrepresented by the code. Forexample: >>> ch = 'a' >>> ord(ch) 97 >>> chr(98) 'b' >>> ord('A') 65 >>> ord('a') – ord('A') 32 >>> ord('d') – ord('D') 32 >>> offset = ord('a') – ord('A') >>> lowercaseLetter = 'h' >>> uppercaseLetter = chr(ord(lowercaseLetter) – offset) >>> uppercaseLetter 'H'

  12. Escape Sequences for Special Characters Suppose you want to print a message with quotation marks in the output. Can you write a statement like this? print("He said, "John's program is easy to read"") No, this statement has an error. Python thinks the second quotation mark is the end of thestring and does not know what to do with the rest of the characters. To overcome this problem, Python uses a special notation to represent special characters,as shown in Table 3.3. This special notation, which consists of a backslash (\) followed by aletter or a combination of digits, is called an escape sequence. The \n character is also known as a newline, line break or end-of-line (EOL) character,which signifies the end of a line. The \f character forces the printer to print from the nextpage. The \r character is used to move the cursor to the first position on the same line.

  13. PrintingwithouttheNewline print(item, end = "anyendingstring") For example, the following code print("AAA", end = ' ') print("BBB", end = '') print("CCC", end = '***') print("DDD", end = '***') displays AAA BBBCCC***DDD*** You can also use the end argument for printing multiple items using the following syntax: print(item1, item2, ..., end = "anyendingstring") Forexample: radius = 3 print("The area is", radius * radius * math.pi, end = ' ') print("and the perimeter is", 2 * radius) displays The area is 28.26 and the perimeter is 6

  14. Thestr Function

  15. TheStringConcatenation Operator

  16. Reading Strings from the Console To read a string from the console, use the input function. For example, the following codereads 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)

  17. FormattingNumbersandStrings • You can use the format function to return a formatted string. • Often it is desirable to display numbers in a certain format. For example, the following codecomputes interest, given the amount and the annual interest rate.

  18. Onlytwo digits after thedecimalpoint However, the format is still not correct. There should be two digits after the decimal point like 16.40 rather than 16.4. You can fix it by using the format function, like this:

  19. format function

  20. FormattingFloating-PointNumbers

  21. Formatting in ScientificNotation

  22. Formatting as a Percentage

  23. FormattingIntegers

  24. FormattingStrings

  25. DrawingVariousShapes • The Python turtle module contains methods for moving the pen, setting the pen’ssize, lifting, and putting down the pen.

  26. SimpleShapes.py

  27. Drawing with Colors and Fonts

  28. ColorShapes.py

  29. ColorShapes.py

  30. Home-work • AllPROGRAMMING EXERCISES on page85 to 89. (19 exam)

More Related