Chapter 2 Writing Simple Programs



Similar documents
Writing Simple Programs

Python Programming: An Introduction to Computer Science

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

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

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Introduction to Python

Python Lists and Loops

PYTHON Basics

Python Loops and String Manipulation

Exercise 1: Python Language Basics

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Introduction to Python

Introduction to the course, Eclipse and Python

Exercise 4 Learning Python language fundamentals

Python Programming: An Introduction to Computer Science

CS 100 Introduction to Computer Science Lab 1 - Playing with Python Due September 21/23, 11:55 PM

6.170 Tutorial 3 - Ruby Basics

Introduction to Python for Text Analysis

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Some programming experience in a high-level structured programming language is recommended.

Software Development. Topic 1 The Software Development Process

Computer Science for San Francisco Youth

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

CSC 221: Computer Programming I. Fall 2011

Python Evaluation Rules

Name Spaces. Introduction into Python Python 5: Classes, Exceptions, Generators and more. Classes: Example. Classes: Briefest Introduction

Computer Science 217

COMSC 100 Programming Exercises, For SP15

Computer Science 1 CSci 1100 Lecture 3 Python Functions

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Introduction to Python

Computational Mathematics with Python

El Dorado Union High School District Educational Services

Debugging Complex Macros

CS 1133, LAB 2: FUNCTIONS AND TESTING

CS106A, Stanford Handout #38. Strings and Chars

Computational Mathematics with Python

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

Programming Exercises

Introduction to Programming Languages and Techniques. xkcd.com FULL PYTHON TUTORIAL

Programming a mathematical formula. INF1100 Lectures, Chapter 1: Computing with Formulas. How to write and run the program.


Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Chapter 13 - The Preprocessor

Programming in Access VBA

RUBY vers. 2. The strings are written between or between (but in this case they will become interpreted) for ex.:

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

ESPResSo Summer School 2012

Chapter 9: Building Bigger Programs

IVR Studio 3.0 Guide. May Knowlarity Product Team

Introduction to Python

Welcome to Introduction to programming in Python

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

CIS 192: Lecture 10 Web Development with Flask

WAYNESBORO AREA SCHOOL DISTRICT CURRICULUM INTRODUCTION TO COMPUTER SCIENCE (June 2014)

A Note for Students: How to Use This Book

Outline Basic concepts of Python language

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

2 SYSTEM DESCRIPTION TECHNIQUES

Python 3 Programming. OCR GCSE Computing

Introduction to Python

Python for Rookies. Example Examination Paper

Example of a Java program

Visual Logic Instructions and Assignments

Client Side Binding of Dynamic Drop Downs

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

NLP Lab Session Week 3 Bigram Frequencies and Mutual Information Scores in NLTK September 16, 2015

PHP Tutorial From beginner to master

Chapter 2 Elementary Programming

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Introduction to Java

smtplib SMTP protocol client

Chapter 2: Elements of Java

Problem Solving. Software Engineering. Behavior. OCD in a Nutshell. Behavior. Objects Operations Algorithm. Object-Centered Design

Pupils compare two rules for converting temperatures in Celsius to Fahrenheit, one accurate and one approximate.

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

Python Programming: An Introduction to Computer Science

CME 193: Introduction to Scientific Python Lecture 8: Unit testing, more modules, wrap up

Python Programming: An Introduction to Computer Science

Basic Programming and PC Skills: Basic Programming and PC Skills:

Maple Introductory Programming Guide

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Hands-on Exercise 1: VBA Coding Basics

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Python. KS3 Programming Workbook. Name. ICT Teacher Form. Do you speak Parseltongue?

Computer Programming I & II*

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Invent Your Own Computer Games with Python

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

FEEG Applied Programming 5 - Tutorial Session

Introduction to Programming with Python Session 3 Notes

Using Files as Input/Output in Java 5.0 Applications

Transcription:

Chapter 2 Writing Simple Programs Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle

Software Development Process Figure out the problem - for simple problems - think about how you would do the problem by hand Determine the specifcations - for a frst programming course - the specifcations are generally in the assignment handout

Software Development Create a Design - In the beginning this is an outline of the major steps Implement the design - build your software Test and debug the program - make sure to think about different things which might go wrong Maintain the program

# convert.py # A program to convert Celsius temps to Fahrenheit# by: Susan Computewell def main(): celsius = input("what is the Celsius temperature? ") fahrenheit = (9.0 / 5.0) * celsius + 32 print "The temperature is", fahrenheit, "degrees Fahrenheit. main() Input Process Output Z-28

Running the Program... $ python convert.py What is the Celsius temperature? 0 The temperature is 32.0 degrees Fahrenheit. $ python convert.py What is the Celsius temperature? 100 The temperature is 212.0 degrees Fahrenheit. Input Process Output

Variable Names / Identifers Must start with a letter or underscore _ Must consist of letters and numbers Case Sensitive Good: spam eggs spam23 Bad: 23spam #sign var.12 Different: spam Spam SPAM Z-2.3.1

Reserved Words You can not use reserved words as variable names / identifers and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print Z-2.3.1

Expressions Programming languages have lots of expressions Expressions are things that can be evaluated to a value Can be a string, number or virtually anything Can be a single value or computed from several values using operators Z-2.3.2

Expressions Everywhere celsius = input( "What is the Celsius temperature? " ) fahrenheit = (9.0 / 5.0) * celsius + 32 print "The temperature is", fahrenheit, "degrees Fahrenheit." Z-2.3.2

Expressions with Numbers Look up variables x 0.6 Do math operations in order left to right ( ) * / 0.6 0.6 3.9 * x * ( 1 - x ) 0.4 + - 0.93 Z-2.3.2

Expressions With Strings abc Bob For strings the + operator means concatenate Bob Hello + there + abc Hello there Bob Z-4.1

Output Statements The print statement takes one or more expressions separated by commas and prints the expressions on the output separated by spaces x = 6 print 2 print 2 + 3 print Hello, 4+5 2 5 Hello 9 Z-2.4

Assignment Statements variable = expression Evaluate the expression to a value and then put that value into the variable x = 1 spam = 2 + 3 spam = x + 1 x = x + 1

Slow Motion Assignment We can use the same variable on the left and right side of an assignment statement Remember that the right side is evaluated *before* the variable is updated Z-34

Input Statements input( Prompt ) - displays the prompt and waits for us to input an expression - this works for numbers In Chapter 4 we will see how to read strings >>> x = input("enter ") Enter 123 >>> print x 123 Z-34

Simultaneous Assignment variable, variable = expression, expression Both expressions on right hand side are evaluated before the right hand side variables are updated >>> x = 1 >>> y = 2 >>> x, y = y, x >>> print x, y 2 1 >>> x, spam = 2 + 3, hello Z-36

Defnite Loops

Defnite Loops Loops that run a fxed (aka defnite) number of times Loops that iterate through an ordered set Loops that run for a number of times for abc in range(5) : print Hi print abc Hi 0 Hi 1 Hi 2 Hi 3 Hi 4 Z-39

Defnite Loops Loops that run a fxed (aka defnite) number of times Loops that iterate through an ordered set Loops that run for a number of times The iteration variable change for each iteration of the loop for abc in range(5) : print Hi print abc Colon (:) defines the start of a block. Indenting determines which lines belong to the block. Hi 0 Hi 1 Hi 2 Hi 3 Hi 4 Z-39

Looking at In... The iteration variable iterates though the sequence (ordered set) Iteration variable Five-element sequence [ 0, 1, 2, 3, 4] The block (body) of code is executed once for each value in the sequence for abc in range(5) :... block of code... The iteration variable moves through all of the values in the sequence

In a FlowChart The iteration variable iterates though the sequence (ordered set) The block (body) of code is executed once for each value in the sequence The iteration variable moves through all of the values in the sequence

i = 0 print i Program: for i in range(4) : print i i = 1 print i i = 2 print i Loop body is run repeatedly i = 3 print i

Z-40 What is range(10)? range(10) is a built in function that returns a sequence of numbers The for statement can iterate through any sequence A sequence can have values of different types >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in [0, 1, 2] :... print I... 0 1 2 >>> for i in [0, "abc", 9, 2, 3.6] :... print I... 0 abc 9 2 3.6

Summary Software Development Input Processing Output Pattern Variable Names / Identifers What are legal identifers Which identifers are unique Assignment Statements Input Statements Simultaneous Assignments Definite Loops Sequences Reserved Words Expressions Output Statements