Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

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

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers

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

Lecture 3. Input, Output and Data Types

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

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

ENGR 101 Engineering Design Workshop

Getting Started with Python

Fundamentals of Programming (Python) Getting Started with Programming

Chapter 2 Getting Started with Python

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Python Input, output and variables

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Variables, expressions and statements

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Variable and Data Type I

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

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

بسم اهلل الرمحن الرحيم

Variables, Expressions, and Statements

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CS 115 Lecture 4. More Python; testing software. Neil Moore

Notes on Chapter 1 Variables and String

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Python for ArcGIS. Lab 1.

CMSC 201 Computer Science I for Majors

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Should you know scanf and printf?

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

IS 0020 Program Design and Software Tools

Unit 4. Input/Output Functions

Reviewing all Topics this term

Expressions and Variables

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Variable and Data Type I

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Table of Contents. Student Practice Pages. Number Lines and Operations Numbers. Inverse Operations and Checking Answers... 40

Introduction to Computer Programming for Non-Majors

Programming with Python

Programming Training. Main Points: - Python Statements - Problems with selections.

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

Text Input and Conditionals

Computer Programming : C++

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

C: How to Program. Week /Mar/05

PROGRAMMING FUNDAMENTALS

CMSC 201 Fall 2018 Lab 04 While Loops

Com S 127x - Lab 6 1. READING FLOWCHARTS WITH CONDITIONAL ACTIONS!

Lab 1: Input, Processing, and Output This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design.

Fundamentals of Programming CS-110. Lecture 2

Input/Output Week 5:Lesson 16.1

COMP 202 Java in one week

Expressions, Input, Output and Data Type Conversions

Chapter 2 Writing Simple Programs

CSCE 110 Programming I

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Introduction to Python (All the Basic Stuff)

Fundamentals of Programming Session 8

PYTHON. Values and Variables

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Introduction to Computer Programming for Non-Majors

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Fundamentals: Expressions and Assignment

2 nd Week Lecture Notes

Python Programming: An Introduction to Computer Science

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Introduction to Python, Cplex and Gurobi

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

VLC : Language Reference Manual

Language Reference Manual

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

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

BASIC ELEMENTS OF A COMPUTER PROGRAM

Bank Probationery Officer M I S C E L L A N I O U S - 1

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

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

Chapter 2 - Introduction to C Programming

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Jython. secondary. memory

UNIT- 3 Introduction to C++

Program Fundamentals

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Microsoft Excel 2010 Handout

Introduction to Python

Transcription:

Chapter 2 Input, Processing and Output Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

Designing a Program Designing a Program o Programs must be carefully designed before they are written. Before beginning a program, you must have a firm idea of what the program should produce and what data is needed to produce that output. So programmers need to define the output and data flows, develop the logic to get to that output. o Programmers can use tools such as pseudocode and flowchart to create models of programs

Designing a Program Designing a Program -- Tools o Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. o The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch o The pseudocode can be translated directly to actual code. o

Designing a Program Designing a Program -- Tools o an example of pseudocode to create a program to add 2 numbers together and then display the result. Start Program Enter two numbers, A, B Add the numbers together Print Sum End Program

Designing a Program Designing a Program Tools o another example of pseudocode to compute the perimeter of a rectangle: Start Program Enter length, l Enter width, w Compute Perimeter = 2*l + 2*w Display Perimeter of a rectangle End Program

Designing a Program Designing a Program Use flowchart

Designing a Program Designing a Program Use flowchart Oval called terminal (appear at top and bottom) Parallelogram called input and output symbol Rectangle called processing symbol (performs some steps on data, such as calculation)

Designing a Program Designing a Program Summary The process of designing a program can be summarized in the following two steps Understand the task that the program is to perform Determine the steps that must be taken to perform the task.

Input, Processing, and Output Input o the information entered into a computer system, examples include: typed text, mouse clicks, etc. Processing o the process of transforming input information into and output. Output o the visual, auditory, or tactile perceptions provided by the computer after processing the provided information. Examples include: text, images, sound, or video displayed on a monitor or through speaker as well as text or Braille from printers or embossers. o

Print function, Comments, Variables print function print("hong Sun") Output: Hong Sun Where Hong Sun is string. String can be enclosed in single or double quote. Where print is function. Perhaps the most fundamental build-in function is print function, which displays output on the screen. A function is a piece of code in a program. The function performs a specific task. The advantages of using functions are: Reducing duplication of code Decomposing complex problems into simpler pieces Improving clarity of the code Reuse of code Information hiding

Print function, Comments, Variables Some of python s escape characters \ \ Backslash (\) \' Single quote (') \" Double quote (") \n ASCII Linefeed (LF) \t ASCII Horizontal Tab (TAB)

Print function, Comments, Variables More about print string output print ("HongSun") print ("Hong\\Sun") print ("Hong\'Sun") print ("Hong\"Sun") output: HongSun output: Hong\Sun output: Hong Sun output: Hong Sun print ("Hong\tSun") output: Hong Sun print ("Hong\nSun") output: Hong Sun

Print function, Comments, Variables More about print string output print("""i'm reading "Hamlet" tonight""") Output : I'm reading "Hamlet" tonight

Print function, Comments, Variables print function s ending The print function normally display a line of output. Two print sentences will produce two lines of output print( One ) print( Two ) Output is: One Two If you want to suppress the newline print( One, end= ) print( Two ) Output is: One Two

Print function, Comments, Variables print function specifying an item separator print ( One, Two, Three ) when multiple arguments are passed to the print function, they are automatically separated by space. So the output is One Two Three print ( One, Two, Three, sep= ) print ( One, Two, Three, sep= ## ) print ( One, Two, Three, sep= --- ) print ( One, Two, Three, sep= *** )

Print function, Comments, Variables print function Displaying Multiple Items with + Operator print( This is + a cat ) Output: This is a cat print( The print function will prints as strings everything +\ in a comma-separated sequence of expressions, +\ and it will separate the results with single blanks by default. +\ Note that you can mix types: anything that is not already a string is +\ automatically converted to its string representation. )

Print function, Comments, Variables Variables o A variable is a name that represents a value stored in the computer. Naming Rules: o Can not use one of Python s key words as a variable name. o A variable name can not contains space. o The first character must be one of the letters a through z,a through Z or under-score character(_) o After the first character you may use one of the letters a through z, A through Z, the digit 0 through 9 or under-score character(_) o Variable name is case sensitive. This means the variable name Lastname is not the same as lastname.

Print function, Comments, Variables List of Python keywords (chapter1 p17) o The following is a list of keywords for the Python programming language. o and, del, from, not, while, as, elif, global, or, with, assert, else, if, pass, yield, break, except, import, print,class, exec, in, raise, continue, finally, is, return, def, for, try o Python is a dynamic language. It changes during time. The list of keywords may change in the future.

Print function, Comments, Variables Variable Assignment use an assignment statement to create a variable and make it reference a piece of data. example: price = 12.25 # this is float (float) age = 25 # this is integer (int) name = Hong Sun # this is string (str) An assignment statement is written in the following general format: variable = expression print(price, age, name)

Print function, Comments, Variables Variable reading input form keyboard use input function to read user input. Example: name = input( Enter your name: ) #Note: the input function always returns the user s input as a string, even if the user enters a numeric data. string_hours = input( how many hours did you work for week? ) hours = int(string_hours), hours = float(string_hours) or hours = int(input( how many hours did you work for week? )) hours = float(input( how many hours did you work for week? )) int, float called data conversion function.

Print function, Comments, Variables Performing Calculations Python math Operators (code see chapter2_mathoperators.py) Symbol Name + Addition - Subtraction * Multiplication / Division // Integer Division % Reminder ** Power

Print function, Comments, Variables Performing Calculations # Python Source code a = 10.89234 b = 11 c = 12 add = a + b + c sub = c - a mult = a * b div = c / 3 power = a ** 2 print(add, sub, mult, div) print(power)

# percentage print(0.5, % ) print(0.5,.1% ) Print function, Comments, Variables Formatting Numbers. x = 1234.56789 # this is float data # Two decimal places of accuracy print(format(x, '0.2f')) # Inclusion of thousands separator print(format(x, ',.1f')) print(format(x, '0,.1f'))

Print function, Comments, Variables Functions we learn today print -- output function int -- data conversion function float -- data conversion functions str -- data conversion functions format data formatting function Input read data from the keyboard

Programming Exercise 12.Stock Transaction program page 79 Last month Joe purchased some stock in Acme Software, Inc. here are the details of the purchase: The number of shares that Joe purchased was 2,000 When joe purchased the stock, he paid $40.00 per share Joe paid his stockbroker a commission that amounted to 3% of the amount he paid for the stock. Two weeks later joe sold the stock. Here are the details of the sale: The number if shares that Joe sold was 2,000 He sold the stock for $42.75 per share He paid his stockbroker another commission that amount to 3% of the amount he received for the stock.

Programming Exercise 12.Stock Transaction program page 79 Write a program that display the following information: The amount of money joe paid for the stock The amount of Commission Joe paid for his broker when he bought the stock The amount of money Joe sold for the stock The amount of Commission Joe paid for his broker when he sold the stock Display the amount of money that Joe had left when he sold the stock and paid his broker(both times). If this amount is positive, then Joe made a profit. If the amount is negative, then Joe lost money.

Programming Exercise 12.Stock Transaction program page 79 buy_share=2000 # this variable is integer buy_per_share=40 # this variable is integer buy_commission_rate=0.03 # this variable is float sold_share=2000 # this variable is integer sold_per_share=42.75 # this variable is float sold_commission_rate=0.03 # this variable is float buy_amount=buy_share*buy_per_share # this variable is integer buy_commission = buy_amount*buy_commission_rate # this variable is float sold_amount = sold_share*sold_per_share # this variable is float sold_commission = sold_amount*sold_commission_rate # this variable is float # this variable final_amount is float final_amount= sold_amount-buy_amount-buy_commission-sold_commission print ('The amount of money Joe paid for the stock is $',format(buy_amount,',d'),sep='') print("the amount of Commission Joe paid for his broker when he bought the stock is $",format(buy_commission,',.0f'),sep='') print("the amount of money Joe the stock is $",format(sold_amount,',.0f'),sep='') print("the amount of Commission Joe paid for his broker when he sold the stock is $",format(sold_commission,',.0f'),sep='') print("the amount of money Joe got is $",format(final_amount,',.0f'),sep='')

Lab exercise and Assignment Exercises Review Questions p73-p74 Programming Exercises P77-78 #3, #7, #11 Assignments: Programming Exercises 1,4,5,9 you can send source code and output to hong.sun@hccs.edu or show me in the class Due: Next Monday 2/6/2017