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

Similar documents
ENGR 101 Engineering Design Workshop

Getting Started with Python

Fundamentals of Programming (Python) Getting Started with Programming

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

Basic Syntax - First Program 1

Variables, expressions and statements

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Chapter 2: Input, Processing, and Output

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

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

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 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

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

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

PROGRAMMING FUNDAMENTALS

Chapter 1. Introduction to Programming and Visual Basic Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Jython. secondary. memory

CMSC 201 Computer Science I for Majors

CSCE 110 Programming I

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

Variable and Data Type I

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Chapter 2 Writing Simple Programs

Python Unit

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

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

Variable and Data Type I

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

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

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

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

1/11/2010 Topic 2: Introduction to Programming 1 1

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

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

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

Introduction to Problem Solving and Programming in Python.

Part III Appendices 165

Lesson 1. Introduction to Programming OBJECTIVES

Question 1. Part (a) Simple Syntax [1 mark] Circle add_ints(), because it is missing arguments to the function call. Part (b) Simple Syntax [1 mark]

Variables, Expressions, and Statements

(I m not printing out these notes! Take your own.)

Short, Unique and Mysterious

Spring 2017 CMSC 140 Programming Project 7: Payroll

SOFT 161. Class Meeting 1.6

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Python

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

Programming with Python

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Act like a code monkey Coding Basics

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

DaMPL. Language Reference Manual. Henrique Grando

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

Topic 2: Introduction to Programming

PYTHON- AN INNOVATION

Key Differences Between Python and Java

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

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

About Variables in Python F E B 1 1 T H

Variables, Constants, and Data Types

The float type and more on variables FEB 6 TH 2012

Chapter 2 Working with Data Types and Operators

Introduction to Python, Cplex and Gurobi

Introduction to computers and Python. Matthieu Choplin

Chapter 4: Making Decisions

Chapter 2 Getting Started with Python

Introduction to Programming

Table of Contents EVALUATION COPY

CSCI 121: Anatomy of a Python Script

Notes on Chapter 1 Variables and String

Overview of List Syntax

Lecture 1. Types, Expressions, & Variables

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

SNS COLLEGE OF ENGINEERING,

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Problem Solving for Intro to Computer Science

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

SOEE1160: Computers and Programming in Geosciences Semester /08. Dr. Sebastian Rost

STEAM Clown Productions. Python lab. Binary Register. STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Variables and Values

Some material adapted from Upenn cmpe391 slides and other sources

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Name Feb. 14, Closed Book/Closed Notes No electronic devices of any kind! 3. Do not look at anyone else s exam or let anyone else look at yours!

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CSCI 2132 Software Development. Lecture 8: Introduction to C

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

And Parallelism. Parallelism in Prolog. OR Parallelism

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS1 Lecture 3 Jan. 22, 2018

CS Name : Grading TA:

Python Input, output and variables

Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

If Statements, For Loops, Functions

Exercise: Using Numbers

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

Transcription:

Input, Processing and Output Bonita Sharif 1 Review A program is a set of instructions a computer follows to perform a task The CPU is responsible for running and executing programs A set of instructions that a CPU can decode is called the CPU s instruction set Main memory is where data and programs are stored Data is stored using the binary numbering system CPU goes through the fetch-decode-execute cycle to execute programs 2 Review Every language has keywords, operators and syntax rules A program consists of a set of statements A statement consists of programming elements such as key words and operators arranged in the proper sequence to form an operation An interpreter is a program that can read programming statements and execute them Review Syntax errors occur if you violate the rules a language has for writing program statements Python can be used in the interactive and script mode 3 4 Typical Software Development cycle 1. Design the program 2. Write the code 3. Correct syntax errors 4. Test the program 5. Correct logic errors (debug) A logic error is an error in a program that produces incorrect results. The program will still run. Designing a Program 1. Understand the task that the program is to perform (Requirements collection) 2. Determine steps that must be taken to perform the task (algorithm) Pseudocode and flow charts are used to create models of a program 5 6 1

Algorithm An ordered well-defined set of steps taken to perform a task Example: Calculate and display gross pay for an hourly paid employee Pseudocode (algorithm written in pseudocode) Input hours worked Input hourly pay rate Calculate gross pay as hours worked * hourly pay rate Display the gross pay Flowcharts A flowchart is a diagram that graphically shows the steps in a program Four types of symbols connected by arrows Start/Stop Processing Input and Output Decision 7 8 Flowchart Example Start Input hours worked Input hourly pay Gross pay = Hours worked * hourly pay Three Basic Steps in a Program Input Data a program receives while it is running Process Output Display gross pay End Input Process Output 9 10 Displaying Output The print statement displays output on the screen. E.g., print This is what I want to output. E.g., print This is the first line print This is the second line print This is the third line Statements in a program are executed in order from top to down Introduction to Data Types Programs usually work on data that are of a particular type Examples of data types Strings (e.g., This is a string ) Numeric A string literal is the name we use when a string is used in code 11 12 2

More on Strings Comments Using single quotes Used if the string you want to display has double quotes in it E.g., I like reading Goodnight Moon Using double quotes Used if the string you want to display has single quotes in it or if it has an apostrophe. E.g, Don t fear! or Hello World Using triple single or double quotes Used if the string you want to display has single or double quotes in it Also used if the string is a multi line string 13 An important part of any program is making sure you document it so you or someone else can understand it later Documentation involves writing comments about the program statements you write You can comment each line or sections of a program In python, comment lines start with a # These lines are not interpreted Comments can also be placed at the end of a line 14 Commenting Guidelines Variables At the top of your program, you should briefly describe in comments what the program is about In the program, comment input, process and output. The process i.e, calculations on input might not be immediately obvious in bigger programs. Comments help alleviate some of the burden in understanding code especially if it is not your own. A variable is a name that represents a value stored in memory. Programs use variables to access and manipulate data stored in memory. E.g., if your hourly pay rate was $10, you can use a variable payrate to represent the data value 10. payrate references the value 10. 15 16 Creating Variables Variable Names Done using an assignment statement Variable = expression E.g., payrate = 10 What is the effect of this assignment? assignment operator (=) A variable called payrate is created that references a location in memory that contains the value 10 10 payrate Make it descriptive but not too long Don t use 1 letter variable names Don t use abbreviations that only you understand It is not easy to come up with a good variable name. 17 18 3

Expressions Expressions are Values (e.g., strings, numbers ) Code that results in a value String expression This is a test Assignment Statements Variable = expression A variable can be used only after you have assigned a value to it E.g., #This is a variable testing program room =115 print We are in room print room # displays the contents of the variable room print room # displays the string 19 20 Naming Conventions for Variables Python key words cannot be used as variables Spaces are not allowed The first character must be a letter or a _ Variables are case sensitive Rate is not the same as rate Choose a variable name based on what it is used for Use an _ in multiword variable names such as pay_rate Camel casing naming convention The word looks like a camel s hump pay_rate would be payrate in the camel casing naming convention The first letter is lowercase The first letter in subsequent words are uppercase 21 22 Displaying multiple pieces of information How do I display more than 1 piece of data in a print statement. Separate them with commas E.g., room = 115 print I am in room, room The above print statement displays a string literal and a variable that references the value 115 Notice the space added by the python interpreter. This is done for you when a comma is used 23 Variable Reassignment The values of variables can be changed. E.g., pay_rate = 10 print pay_rate pay_rate = 20 print pay_rate Draw picture depicting this in memory. 24 4

Numeric Data types Integers int E.g., 2, 101, 45, -3 Reals float E.g., 2.44, 1.51 Non-numeric data types str E.g., room, This is a test Determining Data Types Python determines the data type of variables you assign values to A number written in code is called a numeric literal. room = 115 Python will determine that 115 is an int since it is a whole number with no decimal point. 115 will be stored in memory as an int 25 26 More on Data Types You need to know the data types of your variables since the same operation might behave differently for different data types We will talk about this more as we go through examples during the course of the semester Examples # This is an example of three variables with different data types room_one = 115 room_two = 121 print We are either in, room_one, or, room_two pay_rate = 10 Pi = 3.14 27 28 What is the output of this program? val = 99 print The value is, val print The value is, val print The value is, 99 50shoes = 130 print I wonder what this does?. Key words in Python and as assert break class continue def del elif else except exec finally for from global If import in is lambda not or pass print raise return try while with yiel 29 30 5

Using python in script mode From the python shell, select File -> New Window and type in your program. Don t forget to save it. This is called the script window. Or Write your program in a file and save it with a.py extension and then open it using the Open command from the File Menu To run your program, select Run->Run Module 31 6