CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Slides:



Advertisements
Similar presentations
VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
Advertisements

Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Chapter 2 Writing Simple Programs
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CSC1015F – Python Chapter2 Michelle Kuttel
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Fundamentals of Python: From First Programs Through Data Structures
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Fundamentals of Python: First Programs
CS190/295 Programming in Python for Life Sciences: Lecture 2 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Munster Programming Training
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 2 Elementary Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science 101 Introduction to Programming.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) Fall, 2015.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Designing Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Getting Started With Python Brendan Routledge
Introduction to Python Lesson 2a Print and Types.
More about Iteration Victor Norman CS104. Reading Quiz.
Chapter 2 Writing Simple Programs
Topic: Iterative Statements – Part 1 -> for loop
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Input and Output Upsorn Praphamontripong CS 1110
Python Programming: An Introduction to Computer Science
Python: Control Structures
Python Programming: An Introduction to Computer Science
CS1001 Programming Fundamentals 3(3-0) Lecture 2
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
Chapter 2 Elementary Programming
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
CHAPTER 6: Control Flow Tools (for and while loops)
Chopin’s Nocturne.
The Last of the Mohicans (大地英豪)
Introduction to Programming
Presentation transcript:

CS 127 Writing Simple Programs

 Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications  Describe what your program will do  Define Inputs and Outputs  Create your design  Design algorithms  How will your program work

Writing Simple Programs  Implement the Design  Translate algorithm into computer language  Test / Debug your work  Try out the program  Does it work as expected  Bugs (errors) should be fixed  Try to “break” the program  Maintain the program  Continue making changes in response to needs

Ways to run a python program 1. From the command line  python3 myProgram.py 2. From IDLE window (IDE) 3. Drag and drop programs onto the Python Launcher

Python variable names  Must start with letter or the underscore (“_” character) which may be followed by any sequence of letters, digits or underscores  x  x12  _name  __names  first_last_name

Python Reserved Keywords

Expressions  Program code that produces or calculates new data values  Evaluation  The process of turning an expression into an underlying data type  e.g. answer = input(“What is your age”) age = eval(answer) # answer here is a string which is converted into a number on the second line Examples : x =5 print x print y #name error

Output statements  print(,, )  print()  print(,,, end=“\n”)

Assignment Statements  =  x = 3.9 * x  You assign the value of an expression to a variable  >>> myVar = 0  >>>myVar  0  >>>myVar = 7  >>> myVar  7  >>>myVar = myVar +1  >>myVar  8

Assigning Input  = input( )  is the string expression that is displayed to the user >>>name = input(“What is your name”) >>>name

Handling number inputs = eval(input( )) eval() takes its argument and evaluates the numeric representation of the string and stores it in the variable >>>x = eval(input(“What is the temperature in Farenheit?”)) >>>x

Evaluations  >>> ans = eval(input(“Enter an expression ”))  Enter an expression 3+4*5  >>> print(ans)  23  >>>

Exercise : Temperature Converter  Problem :  We need to convert temperature from degrees Celsius to Degrees Farenheit  Write an algorithm to solve this problem  What are the inputs  How are you going to compute the result  What are the outputs  Conversion : farenheit = 9/5*celsius + 32

Simultaneous Assignment ,, =,, >>> x, y = 10, 15 >>> sum, diff = x +y, y-x >>> sum >>> diff

Average Example #average.py -- using simulataneous assignment def main() : print ("This program computes the average of two numbers") num1, num2 = eval(input("Enter two numbers separated by commas")) average = (num1+num2)/2 print ("The average of the two numbers is: ",average) main()

Definite loops  Looping is an important part of programming  A statement has to be executed a specified number of times  For definite loops, Python knows how many times we will go around (iterate) over a loop Syntax: for in :

Definite Loop Example  >>> for i in [0,1,2,3] print (i)  >>> for odd in [1, 3, 5, 7, 9] print (odd*odd) >>>for i in range(10): print(i) range() is a built-in function in Python for generating a sequence of numbers on the fly

Range  >>> list(range(10))  [0, 1, 3 …, 9]  General syntax  for in range( )

Future Value Example  #future.py  def main():  print("This program calculates the future value")  print("of a 10-year investment")  principal = eval(input("Enter the initial principal: "))  apr = eval(input("Enter the annual interest rate: "))   for i in range(10):  principal = principal *(1+apr)  print("The value in 10 years is: ",principal)  main()

Exercise  Modify the previous example to allow the user to also specify the number of years so that calculation is based on the number of years the user entered

Exercise  Modify the temperature converter program so that it prints a table of Celcius temperatures and the Farenheit equivalents every 10 degress from 0C to 100 C

Exercise  Write a program that converts distances measured in kilometers to distances measured in miles  (1 km is 0.62 miles)

Exercise  Write an interactive calculator in Python. The user should be able to enter an expression and the answer should be displayed.

Exercise  Write a python program which asks a user to specify two numbers. Compute and display the average of the numbers in the range  Eg: If user enters 2 and 4 Average = (2+3+4)/3 = 3