CS 112: Intro to Comp Prog

Similar documents
CS 112: Intro to Comp Prog

CS 112: Intro to Comp Prog

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Flow Control: Branches and loops

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

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

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

cs1114 REVIEW of details test closed laptop period

CSC116: Introduction to Computing - Java

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CSC116: Introduction to Computing - Java

CPSC 150 Laboratory Manual. Lab 1 Introduction to Program Creation

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

[CHAPTER] 1 INTRODUCTION 1

Programming Fundamentals

Lecture Transcript While and Do While Statements in C++

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Lecture 10. Daily Puzzle

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

CIS192: Python Programming

Lecture 1: Hello, MATLAB!

CS 111X - Fall Test 1

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

Unit 7: Algorithms and Python CS 101, Fall 2018

Part III Appendices 165

Conditionals & Control Flow

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Decisions, Decisions. Testing, testing C H A P T E R 7

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

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

Programming for Experimental Research. Flow Control

CS11001/CS11002 Programming and Data Structures Autumn/Spring Semesters. Introduction

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

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Conditionals and Recursion. Python Part 4

CSC116: Introduction to Computing - Java

Project 1. due date Sunday July 8, 2018, 12:00 noon

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Visualize ComplexCities

Syllabus COSC-051-x - Computer Science I Fall Office Hours: Daily hours will be entered on Course calendar (or by appointment)

Conditional Control Structures. Dr.T.Logeswari

CSE 115. Introduction to Computer Science I

Lecture 8. Conditionals & Control Flow

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Pattern Maker Lab. 1 Preliminaries. 1.1 Writing a Python program

CS111: PROGRAMMING LANGUAGE II

Flow Control. So Far: Writing simple statements that get executed one after another.

CS1 Lecture 5 Jan. 25, 2019

CS1110 Lab 1 (Jan 27-28, 2015)

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

CS 112: Intro to Comp Prog

CS457/557 Functional Languages

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

CS11002 Programming and Data Structures Spring Introduction

Week - 01 Lecture - 04 Downloading and installing Python

Jython. secondary. memory

Introduction. C provides two styles of flow control:

Lecture 1. Types, Expressions, & Variables

SCRATCH MODULE 3: NUMBER CONVERSIONS

CIS192 Python Programming. Robert Rand. August 27, 2015

Comp 151. Control structures.

CMSC 201 Spring 2018 Lab 01 Hello World

Lab 4: Bash Scripting

LECTURE 5 Control Structures Part 2

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Honors Computer Science Python Mr. Clausen Program 7A, 7B

Python Basics. Lecture and Lab 5 Day Course. Python Basics

Selection Statement ( if )

ENGR 102 Engineering Lab I - Computation

The source code for this lab must be submitted in a file named lab4.py. The source code file must contain a file header formatted as in previous labs.

CSE 115. Introduction to Computer Science I

Project 3: RPN Calculator

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Getting started 7. Saving data 23

CMSC 201 Spring 2017 Lab 01 Hello World

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

EE 109 Lab 8a Conversion Experience

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Python Problems MTH 151. Texas A&M University. November 8, 2017

#4: While Loop Reading: Chapter3

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

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

Computer Programming

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Department of Computer Science University of Pretoria. Introduction to Computer Science COS 151

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

Transcription:

CS 112: Intro to Comp Prog Importing modules Branching Loops Program Planning Arithmetic Program Lab Assignment #2 Upcoming

Assignment #1 Solution CODE: # lab1.py # Student Name: John Noname # Assignment: Lab #1 # Submission Date: 02/09/2009 # Honor Code Statement: I received no assistance on this assignment that # violates the guidelines as set forth by the instructor # and the class syllabus. # References: Python Programming for the absolute beginner (book) # Comments: Our first assignment # Defining the main function def main(): #printing name and major print ''' Name: John Noname Major: Electrical Engineering ''' #prompting user to hit enter to exit the program raw_input("press <Enter> to exit...") print "Good bye" #Calling the main function main()

Assignment #1 Solution Q1. What steps do you take to create a program using IDLE? (Include creating a new file, saving it, and executing it) Ans. -After opening idle, click on file, select "New Window". -Write your code in the opened script window -To save the file click on file again and select "save as" *select the directory you want to store your file in using the pop-up dialog window. *name the file followed by a.py extension. -The file can be executed clicking in the menu "Run Module" (F5). Q2. What steps do you take to create a program (using a text editor) in your operating system? (Include creating a new file, saving it, and executing it) Ans. -Open text editor (eg. notepad) -Click on file, select "new" -To save the file click on file again and select "save as" *select the directory you want to store your file in. *name the file followed by a.py extension. -To run the file, open command prompt *type "cd" with the appropriate directory to get to the location of the script file (.py), use "dir" or "ls" (depending on your OS) to verify the file is in that directory *Once you have found the file, type "python file_name" where file_name is the name of the file you saved Q3. Is it possible to create a program using the method from Q1 and execute it using the method from Q2? Ans. Yes

Lecture Topics Theory...why have these constructs in programming? Modules Can separate code into separate files grouping similar functions (math is the best example). Good for organization, usability, and distribution Branching Programs no longer have to run sequentially, i.e. always behaves the same way. Now programs can be dynamic based on user's input. Looping Allows for arbitrary code to be repeated...before code would have to be copied and pasted; thus would have to be for a set number of times.

Importing modules module: a file that contains external code that is related There are many different modules that have been created and are available to be used by your code. (Tkinter, math, os) Importing loads the code and allows your program to access specific functions Example module: random Function: randrange(<bound>) #gets a number between 0 and (<bound> - 1) # Will randomly display a number in the range 0 to 4 import random print random.randrange(5)

if-structure if <condition>: <statements to perform when condition is true> <tabbing signifies which statements are contained> <within the if-block> <code not tabbed is always run> Example: num = int(raw_input("enter a number")) if (num % 2) == 0: print str(num)+" is even!" print "It is a good number"

if-else structure if <condition>: else: <statements to perform when condition is true> <...> <statements to perform when condition is false> <...> <code not tabbed is always run> Example: num = int(raw_input("enter a number")) if (num % 2) == 0: print str(num)+" is even!" else: print str(num)+" is odd!"

Conditions Conditions are expressions that evaluate to True or False Comparison Operators are used to create these expressions == equal to!= not equal to > greater than >= greater than or equal to < less than <= less than or equal to Examples: (10 / 2) == 5 True HELLO == hello False

Conditions Values in variables can also be directly evaluated to True or False Numeric: 0 == False, everything else is true String: == False, everything else is true Condition expressions can be combined/modified using Logical Operators not #negate the boolean value (false -> true) and #both sides of operator must be true or #at least one side of operator must be true There exists several parallels with if-statements and logical operators, and many times, solutions can be found through a variety of means.

Conditions if (num % 2)!= 0: print str(num)+" is odd! #Equivalent to: if not ((num % 2) == 0): print str(num)+" is odd!" ------------------------------------------------------- if (num % 2) == 0 and (num % 3) == 0: print str(num)+" is divisible by 6!" #Equivalent to: if (num % 2) == 0: if (num % 3) == 0: print str(num)+" is divisible by 6!"

Conditions if userinput == "yes" or userinput == "YES": print "I am glad you feel that way" #Equivalent to: if userinput == "yes": print "I am glad you feel that way" if userinput == "YES": print "I am glad you feel that way"

if-elif-...-else structure if <condition1>: <statements to perform when condition1 is true> <...> elif <condition2>: <statements to perform when condition2 is true> <...> <...many elif statements may follow> else: <statements to perform when all above conditions> <are false> <...> <code not tabbed is always run>

if-elif-...-else structure Example: print "1. Print \"Hello\"" print "2. Print \"Good-bye\"" print "3. Print \"Zzzzzz\"" num = int(raw_input("enter your choice")) if num == 1: print "Hello" elif num == 2: print "Good-bye" elif num == 3: print "Zzzzzz" else: print "Not a valid choice"

while structure while <condition>: <statements to perform when condition is true> <tabbing signifies which statements are contained> <within the while-block. Once tabbed statements are> <executed program goes back to top to check> <condition again...repeat until false> <code not tabbed is always run, and is where code> <resumes when loop finishes, careful of infinite loops> Example: num = 0 while not num: num = int(raw_input("enter a non-zero number")) print "Hurray you chose: "+ str(num)

break continue Sometimes you may want to alter the way your loop runs from inside the loop rather than waiting for a single iteration of the loop to finish. We can do this with two commands: break - this will stop the loop and jump the program to run the code after the loop body (i.e. the tabbed code) continue - this will skip all lines of code inside the loop, jumping the program back to check the condition of the loop

Program Planning (Revisit) Write a program that will ask the user for two decimal numbers, then list a set of operations for them to choose (1 sum, 2 difference, 3 product, 4 quotient, 5 Quit). Display the solution to their choice. After the solution is printed redisplay the menu for them to choose another option until they choose 5, if they type any number other than 1-5 display an error message and continue to display the menu again. Terminal is as shown:

Identify the Main Steps Entering the two numbers Displaying the menu Entering a choice Branching for choice Repeating Menu

Identify the Variables Number 1 - num1 Number 2 - num2 User's Choice - choice Could have decided to have variables for each arithmetic operation, but will do the calculations in-line.

Pseudo-code Get user values for the two decimal numbers Create an infinite loop only to be stopped if 5 entered Print menu Get the user's choice of what operation to perform If choice is 1 print num1 + num2 If choice is 2 print num1 - num2 If choice is 3 print num1 * num2 If choice is 4 print num1 / num2 If choice is 5 stop the loop If choice is anything other than 1 5 restart loop ************************************************************** FOR YOU: Take the time to convert the above pseudo-code into python code.

Assignment #3 Due next week before lab You need to turn in one file Lab3.py Contains the proper comment header Reads input from the user Constructs the proper date string Gathers info using the webbrowser module Outputs the result as shown in the example output Again make sure to verify that everything was submitted

Upcoming Loops, Strings, Tuples, and Number-Base Conversion Finish Lab Assignment #3 and be sure to complete it before next week's lab Read Chapter 4 in the textbook