ECS Baruch Lab 2 Fall 2019 Name

Similar documents
ECS Baruch Lab 3 Spring 2019 Name

ECS Baruch Lab 9 Spring 2019 Name

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

Using IDLE for

ECS Baruch Lab 5 Spring 2019 Name NetID (login, like , not your SUID)

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

Once you define a new command, you can try it out by entering the command in IDLE:

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

Semester 2, 2018: Lab 1

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

Computer and Programming: Lab 1

Creating a Project in XCode

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Week - 01 Lecture - 04 Downloading and installing Python

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 3. Input, Output and Data Types

ECS Baruch Lab 12 Spring 2019 Name

Chapter 2. Editing And Compiling

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

CS1110 Lab 1 (Jan 27-28, 2015)

Lecture 02, Fall 2018 Friday September 7

CSI Lab 02. Tuesday, January 21st

Starting. Read: Chapter 1, Appendix B from textbook.

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Exploring Python Basics

3 Getting Started with Objects

CMSC 201 Fall 2018 Lab 04 While Loops

S206E Lecture 19, 5/24/2016, Python an overview

Lesson 4: Who Goes There?

Arduino 02: Using the Arduino with Python. Jeffrey A. Meunier University of Connecticut

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

SPSS Tutorial - How to Perform an Offline License Activation on a Windows Computer

How To Test Your Code A CS 1371 Homework Guide

Introduction to Computer Programming for Non-Majors

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

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

StudioPrompter Tutorials. Prepare before you start the Tutorials. Opening and importing text files. Using the Control Bar. Using Dual Monitors

Python is available at: Why Python is awesome:

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Part 6b: The effect of scale on raster calculations mean local relief and slope

9.2 Linux Essentials Exam Objectives

Programming for Engineers in Python. Autumn

ASCII Art. Introduction: Python

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3. Installing Notepad++

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

In further discussion, the books make other kinds of distinction between high level languages:

The Domino Designer QuickStart Tutorial

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Lecture 3. Functions & Modules

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

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

Chapter 2 The SAS Environment

NCMail: Microsoft Outlook User s Guide

Functions Structure and Parameters behind the scenes with diagrams!

Not-So-Mini-Lecture 6. Modules & Scripts

CS 1110, LAB 1: PYTHON EXPRESSIONS.

Heuristic Evaluation of [Pass It On]

Lesson 3 Transcript: Part 2 of 2 Tools & Scripting

Introduction to computers and Python. Matthieu Choplin

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Exploring Python Basics

Flow Control: Branches and loops

Chapter One: Getting Started With IBM SPSS for Windows

1. Please, please, please look at the style sheets job aid that I sent to you some time ago in conjunction with this document.

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

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

NCMail: Microsoft Outlook User s Guide

15-122: Principles of Imperative Computation

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

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

CS Lab 8. Part 1 - Basics of File I/O

Drupal Cloud Getting Started Guide Creating a Lab site with the MIT DLC Theme

Introduction to Python

Introduction to Java Unit 1. Using BlueJ to Write Programs

MEMBERSHIP & PARTICIPATION

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

Table of Contents EVALUATION COPY

Importing source database objects from a database

Programming Fundamentals and Python

BEGINNER PHP Table of Contents

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL

15-110: Principles of Computing, Spring 2018

Tutorial How to upgrade firmware on Phison S8 controller MyDigitalSSD

3. Now go to Edit => Effects => Brightness/Contrast... and notice that you can modify the colors you created above.

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

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

Introduction to Computation and Problem Solving

Lecture 3. Functions & Modules

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

STAT 113: R/RStudio Intro

Max and Programming Is Max a Programming Language?

Invent Your Own Computer Games with Python

nostarch.com/pfk For bulk orders, please contact us at

MITOCW watch?v=0jljzrnhwoi

Temple University Computer Science Programming Under the Linux Operating System January 2017

Algorithms and Programming I. Lecture#12 Spring 2015

Control Panel software usage guide (v beta)

Lecture 4. Defining Functions

Introduction to Python Code Quality

Transcription:

ECS 102 - Baruch Lab 2 Fall 2019 Name I. Review Recall that in lab 1, you created a folder on your H: drive, Document/Apps-SU/Python Programs The reason you need to save programs here on the lab computers is a) So they won t be erased when you log off. b) So they will run. (On your own laptop, you can put them anywhere you want.) Answer these by remembering what happened in lab 1. 1. If I enter at the shell prompt >>> 34 what will the shell print? 2. If I enter at the shell prompt>>> 2 + 10 # + 100 what will the shell print? Why? II. A. Variables Open IDLE for Python 3.7 (NOT 2.7!) You see the Python Shell. (From the title bar at the top you can see that is says Python 3.7.0 Shell ) At the prompt (>>>) type # your name (use your actual name!) At the prompt set >>> area=3*4 Evaluate by asking >>> area Create two more variables by assigning length the value 7 and width the value 5. Have the shell evaluate length and width and see that their values are 7 and 5. At the prompt set >>> area=length * width Evaluate. What did you get? Notice that the former value of area (12) has been replaced with a new value.

Give length a new value, 10 Without telling the shell to recompute area, have the shell evaluate the area. What value did it give for area? Did it use the new value of length? Once again tell it to compute area using area=length * width What value did it give for area? Did it use the new value of length? (Changing a value of a variable does not change previous computations, only those going forward.) B. Restarting the Shell From the Shell menu select Restart Shell. Notice that what you typed before is still visible in the window, but there is a line indicating where the shell was restarted. So what does restart mean? At the prompt type area Did it remember the value? (Everything that was defined in the earlier incarnation of the shell has been wiped out.) III. Some data types and commands A. Enter each of these at the prompts, as indicated. Record what the computer responds. Include single or double quotes if they are part of the output. >>> 27 >>> "27" >>> apples = 6 >>> apples >>> "apples" >>> oranges = "navel" >>> oranges >>> 3.14159 >>>type(apples)

>>>type(oranges) >>>type(3.14) >>>type("something in quotes") A string is a sequence of characters. We tell the computer we mean a string by enclosing the string in single quotes ' ' or double quotes: " ". The computer indicates a string by enclosing them in quotes. Above, a sequence of characters without quotes, like oranges, is the name of a variable. A variable could be holding an int, like 27, a float, like 3.14159, or even a string, like "navel". B. We can tell the computer to print something to the screen. Try >>>print(234) >>>print("pizza and salad") >>>print(apples) >>>print(oranges) >>>print(123 + 7) >>>print("i like " + oranges) >>>print(apples + oranges) (What happens?) Answer by looking at the examples above: What happens when you add two strings? What happens when you add a string to a number? >>>print(3, 2, 1) >>>print(apples, oranges, "plums") >>>print(apples,oranges,"plums") >>>print(apples, oranges, "plums") C. Some input. Try the following >>>name = input("enter your name: ") When the computer asks you to enter you name, type it in, followed by the enter key. Now evaluate name >>>name

Write similar code to ask for someone's age. Enter an age when asked. Evaluate age. Are there any quotation marks around the output? Is the computer interpreting your age as a number or as a string? Try >>>age2=eval(input("enter your age ")) >>>age2 Are there any quotation marks around the output? This time, is the computer interpreting your age as a number or as a string? D. Time to write your own program. Restart the shell. (Shell -> Restart shell) Write a sequence of statements that will Ask a user for their name and store it in a variable. Ask the user for the length and width of a rectangle. Compute the area of the rectangle Compute the perimeter of the rectangle Print results like this (but with your values): Hi Michelle! Your rectangle has area 50 Your rectangle has perimeter 30 IV. Congratulations! You have written a program - almost. The problem is, if you want to run it again, you will have to type it all in again. So lets do it again, but this time we will save the statements in a file, so we can reuse them. Select File -> New File A new Python editor window will open. Type in some comments like these (but use your name, not mine): # ProcessRectangle.py # by Marjory Baruch # Compute the area and perimeter of a rectangle. # Ask for name. # Ask for length and width. # Compute area and perimeter. # Print results.

The first three lines are the file name, your name, and a brief description of what the program does. Starting with the comment #Ask for name go back and retype the Python statements you typed in part D. Now there are no prompts. Put the statements right after the comments that describe them, for example # Ask for name. name = input("enter your name ") Do this for all the comments, providing Python statements that will actually do what the comments describe. Select File-> Save In the window that opens, navigate to the folder Documents\Apps-SU\Python Programs on your H: drive that you set up in lab 1. Name the file ProcessRectangle.py and click the Save button. With the ProcessRectangle.py file window selected, select Run-> Run Module from the menu. If all went well, the Python shell window restarted and you are asked to enter your name! Answer the questions when asked, pressing enter key each time, and your program should print some answers! If all didn't go well (which is usually the case), fix errors, save again, and run again. You may have to repeat this step several times. To appreciate that this script can be run over and over choose Run->Run Module again and enter different values. When you run this module, you are just giving the shell lots of lines of code at once. You can still enter commands. At the prompt type >>>length and see that you get the value you entered when you ran the module. Print the file ProcessRectangle.py to hand in later. Close the file window. V. Writing and running a program with a main. From the shell menu select File -> New File. (In the future I will just say open a new file.) Type the following in the window (use your name where it says your name) Don't cut and paste.

# miletokm.py # your name # Converts distance in miles to kilometers # Ask for the distance in miles miles = eval( input( "Enter distance in miles: ")) # Convert to kilometers km = miles * 1.609 # Print the result print(miles, "miles equals", km, "kilometers") Save the module file as milestokm.py in your Python Programs folder on your H: drive. (Recall H:Documents/Apps-SU/Python Programs ) Whenever I say to save a file, this is where you should save it. Select Run-> Run Module from the menu. (I will say Run the Module). Fix errors if there are any. When it is running properly, the shell will be selected and You should see the program's prompt. (Enter distance in miles not >>>) Enter a value, like 1 (where you know the answer) and see what the program says. Did you get a good answer? Run the module again. Did the shell restart? Give a different value. Record the input value (miles) and the output value (kilometers) miles: km: At the shell prompt type miles. What is the output? Save the Python shell window (not the module) in Python Programs as lab2shell.py. Frequently throughout lab save it again, by pressing the control key and s together. That way it won't be lost.

B. Writing a main function. We would like to be able to run a program several times without restarting the shell. Click on the module window. (It says milestokm.py on the title bar.) Click File->Save As. Add v2 (for version 2) to the name (miletokmv2.py), and make sure it is being saved in the python programs folder. Modify your program so it looks like the program below. Indentation matters. To indent a bunch of lines: highlight them and from the menu bar select Format -> indent region. # miletokmv2.py # your name # Converts distance in miles to kilometers def main(): # Ask for the distance in miles miles = eval( input( "Enter distance in miles: ")) main() # Convert to kilometers km = miles * 1.609 # Print the result print(miles, "miles equals", km, "kilometers") Run the module milestokmv2.py. Did the shell restart? Enter data when asked. Now to call main. At the shell prompt type main() and press enter >>> main() Does the program run again? Did the shell restart? C. While we can now convert lots of miles by running the program over and over, lets have the program do it. Save the program again as milestokmv3.py.

Modify the program so it looks like # miletokmv3.py # your name # Converts distances in miles to kilometers def main(): for i in range(4): # Ask for the distance in miles miles = eval( input( "Enter distance in miles: ")) main() # Convert to kilometers km = miles * 1.609 # Print the result print(miles, "miles equals", km, "kilometers") Run the module milestokmv3.py Enter data as asked. How many time does the program ask you for miles? List the values you enter and the answers: miles KM Once again, save the shell. VI. Submitting your work. Print the file lab2shell.txt to printer CST3-116. Print (if you didn't earlier) the module ProcessRectangle.py Make sure your name is on the lab packet. Staple them together in the order: packet on top, then ProcessRectangle, then Lab2Shell. Just outside the lab is a file cabinet with a slot on top and BARUCH ECS102 written on the front. Put your lab in the slot.