Mathematical Functions, Strings, and Objects. Introduction ■ To solve mathematics problems by using the functions in the math module (§3.2). ■ To represent.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Reading Input from the Console eval function. variable = input("Enter a value: ") The value entered is a string. You can use the function eval to evaluate.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
© 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.
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 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 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.
© 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,
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
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
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
Chapter 2 Variables.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 STRINGS String data type Basic operations on strings String functions String procedures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Introduction to Programming
28 Formatted Output.
C Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2, Part I Introduction to C Programming
TMF1414 Introduction to Programming
OUTPUT STATEMENTS GC 201.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Introduction to Programming
Suppressing print Newline
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
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
Topics Designing a Program Input, Processing, and Output
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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). ■ To use the ord function to obtain a numerical code for a character and the chr function 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 str function (§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).

Common Python Functions

For example

MathFunctions.py

Compute the angles by using the following formula

ComputeAngles.py import math 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)

Strings and Characters A string (described in Chapter 1) is a sequence of characters. Python treats characters and strings the same way In addition to processing numeric values, you can process strings in Python. A string is a sequence 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'

Unicode Code

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

The ord and chr Functions Python provides the ord(ch) function for returning the ASCII code for the character ch and the chr(code) function for returning the haracter represented by the code. For example: >>> 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'

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 the string 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 a letter 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 next page. The \r character is used to move the cursor to the first position on the same line.

Printing without the Newline 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") For example: radius = 3 print("The area is", radius * radius * math.pi, end = ' ') print("and the perimeter is", 2 * radius) displays The area is and the perimeter is 6

The str Function

The String Concatenation Operator

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)

Formatting Numbers and Strings 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 code computes interest, given the amount and the annual interest rate.

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

format function

Formatting Floating-Point Numbers

Formatting in Scientific Notation

Formatting as a Percentage

Formatting Integers

Formatting Strings

Drawing Various Shapes The Python turtle module contains methods for moving the pen, setting the pen’s size, lifting, and putting down the pen.

SimpleShapes.py

Drawing with Colors and Fonts

ColorShapes.py

Home-work All PROGRAMMING EXERCISES on page 85 to 89. (19 exam)