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.

Similar documents
CS 2316 Individual Homework 1 Python Practice Due: Wednesday, January 20 th, before 11:55 PM Out of 100 points

Python Programming Exercises 1

Unit 7: Algorithms and Python CS 101, Fall 2018

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

Fundamentals: Expressions and Assignment

>>> * *(25**0.16) *10*(25**0.16)

Spring CS Homework 3 p. 1. CS Homework 3

Week 3: Objects, Input and Processing

Week 1: Introduction to R, part 1

CS 102 Lab 3 Fall 2012

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

Copied from: on 3/20/2017

2. Click on the Launch button and select Terminal. A window will appear which looks like:

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

Downloaded from Chapter 2. Functions

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

How many positive 3-digit integers from have an odd number of divisors?

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

CSCI 121: Anatomy of a Python Script

Introduction to Programming

Adding Content to Blackboard

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

Expressions and Variables

Skill 1: Multiplying Polynomials

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

CSI Lab 02. Tuesday, January 21st

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

MATLAB Demo. Preliminaries and Getting Started with Matlab

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Express Yourself. The Great Divide

Com S 227 Spring 2018 Topics and review problems for Exam 1 Monday, February 19, 6:45 pm Exam locations by last name:

CS1110 Lab 1 (Jan 27-28, 2015)

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

MIT AITI Python Software Development

CS 115 Lecture 4. More Python; testing software. Neil Moore

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

MITOCW watch?v=0jljzrnhwoi

CS 2316 Individual Homework 1 Python Practice Due: Wednesday, August 28th, before 11:55 PM Out of 100 points

Python Activity 2: Input and Variables in Python

1) Log on to the computer using your PU net ID and password.

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Patterning Math Lab 4a

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

15-110: Principles of Computing, Spring 2018

Lab Assignment 1 Introduction to Programming ICS 31 & CSE 41 Summer Session

CSE 115. Introduction to Computer Science I

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

CS 112: Intro to Comp Prog

Getting started with Python

Semester 2, 2018: Lab 1

One of the hardest things you have to do is to keep track of three kinds of commands when writing and running computer programs. Those commands are:

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

The Very Basics of the R Interpreter

Homework 09. Collecting Beepers

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

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Software.LifeTips.com

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

Part 1 Simple Arithmetic

Lecture 3: Functions & Modules

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Part III Appendices 165

CS 170 Java Programming 1. Week 5: Procedures and Functions

CS 112: Intro to Comp Prog

AP Computer Science Unit 1. Programs

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

Ph3 Mathematica Homework: Week 1

Python lab session 1

Ch.2: Loops and lists

4. Modules and Functions

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

Algorithms and Programming I. Lecture#12 Spring 2015

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CISC 181 Lab 2 (100 pts) Due: March 7 at midnight (This is a two-week lab)

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

CS 051 Homework Laboratory #2

COM110: Lab 2 Numeric expressions, strings, and some file processing (chapters 3 and 5)

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Structured Programming

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

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

Credit: The lecture slides are created based on previous lecture slides by Dan Zingaro.

Ascii Art. CS 1301 Individual Homework 7 Ascii Art Due: Monday April 4 th, before 11:55pm Out of 100 points

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

10 Using the PCFL Editor In this chapter

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

MITOCW watch?v=w_-sx4vr53m

4. Java Project Design, Input Methods

Introduction to Programming with JES

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

Slide 1 CS 170 Java Programming 1

Lecture 1. Types, Expressions, & Variables

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

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

MITOCW watch?v=se4p7ivcune

Transcription:

Com S 127 Lab 2 Checkpoint 0 Please open the CS 127 Blackboard page and click on Groups in the menu at left. Sign up for the group corresponding to the lab section you are attending. Also, if you haven't done so already, you should first complete Checkpoint 3 from last week's lab (submitting an assignment and checking your submission history). See http://web.cs.iastate.edu/~smkautz/cs127f16/labs/lab1/page14.html 1. A review of some Python functions For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out. We have seen all of these built-in functions: type(expression) len(string) float(number or string) int(number or string) str(number) pow(number, number) # returns the type of an expression # returns the length of a string # converts int or string to a float value # converts float or string to an int value # converts a number to a string # computes a number to a power Here some examples of calls to the built-in functions. Start by defining some variables in the shell: >>> s = "steve" >>> x = 25 >>> t = "42" And then try entering the following function calls to see what value each function returns. (Remember the shell will attempt to evaluate each expression you type and display its value, so when using the shell this way you don t actually have to type a print statement.) >>> len(s) >>> str(10 * 25) >>> len(str(10 * 25)) >>> int(t) + 1 >>> int(2.7) >>> round(2.7) There is also a Python function pow for computing powers. pow(x, y) returns x y the power y.) Try evaluating these expressions in the shell: (x raised to

>>> pow(5, 2) >>> pow(2, len(s)) Other mathematical functions What about the rest of the stuff you find on a calculator, like trig functions and logarithms and roots? They're there, but require an extra step. The functions above are all "built in" to the language. We can just use them. Python also has many additional libraries of functions available, called modules. One of these modules is called math, and includes all the functions from your calculator. As an example, consider the square root function, called sqrt, from the math module. If you just type this in the shell, >>> sqrt(25) you'll get an error. In order to use a module, we have to import it. Then to use a function from the module, we have to include the module's name. So the usage of the square root function looks like this: >>> import math >>> math.sqrt(25) >>> 5.0 To see all the functions from the math module, you would look at the Python library documentation: https://docs.python.org/3/library/math.html 2. Console input and string concatenation You may have noticed by now that you can print multiple values with one call to the print function if you separate them with commas. Assuming you still have your variable x defined from above, try this in the shell: >>>print("the answer is", x) This also puts a space between items. This is convenient for quickly printing output, but sometimes you don't want a space. For example, try >>>print("the price is $", x) Here the output looks odd because you don't want a space after the dollar sign. In a case like this, you can use string concatenation. This is a fancy word meaning that you stick two strings together to make a longer string. You use the '+' symbol. Try this: >>>first = "Hello" >>>second = "World!" >>>print(first + second)

Notice that no space is automatically added after "Hello ", so in practice you'd have to add the space yourself, for example, >>>print(first + " " + second) When combining numbers with text, you have to convert the number to a string before concatenating, using the str() function: >>>print("the price is $" + str(x)) You have also seen the input() function for reading input. Go ahead and download the example input_test.py. You can find it here http://web.cs.iastate.edu/~smkautz/cs127f15/examples/week2/input_test.py You can download the file into your cs127 directory and open it in Wing 101, or you can create a new file in Wing 101 and copy/paste the code. Read it and try it out. Remember that the input() function always returns a string, so to do arithmetic with input we have to convert to a number using the int() or float() functions. 3. Writing interactive programs In this part, you'll create two programs using the Wing 101 text editor. a) Write a short interactive program pets.py for helping the user to select the perfect pet. It should look like this, where the items in bold are just examples of values entered by the user: What's your favorite color? purple What's your favorite animal? giraffe What's your favorite number? 7 Ok, I guess your ideal pet would be a purple giraffe with 7 legs! Use string concatenation to get the spacing for the output exactly as shown in the example above. b) Next, write a short program donuts.py to calculate the total cost of an order of donuts. Suppose that donuts are.75 each or 8.00 per dozen. Prompt the user to enter a number of donuts, and print a brief message along with the total price. Before writing the code, do a concrete example, e.g., how much should it cost for 50 donuts? How about 100 donuts? After writing the code, test it on your sample values. (Don't worry about how to format the output to show exactly two decimal places, we'll come back to that later.) Checkpoint 1 Show the TA your completed programs pets.py and donuts.py, along with your example calculation for donuts.py.

4. Turtle graphics Work through the first two sections of Chapter 4 ("Python Turtle Graphics") of the textbook. Complete the exercises in the second section, including the "Extend this program..." that occurs about halfway down the page. Errors in activecode windows: If you have an error in your turtle code in an activecode window, you may have to close the browser to recover. In general it's easier to write code using turtles in Wing 101, see note below. In Wing 101, write a program that uses the turtle module to draw a red 5-pointed star whose size is entered by the user. (Note: the angle at each of the points is 36 degrees. You can start with one corner at the origin and make the first line going east.) Closing the window if you have errors: When you run a program that uses turtle, it brings up a new window for the graphics. If there is an error in your code, you'll see the error messages in the shell in Wing, but you won't be able to directly close the window by clicking on it. To close the window, click the drop-down menu labeled "Options" that is just above the shell in Wing. Select "Restart Shell". Checkpoint 2 Show the TA the completed exercises from the text, your "Extend this program" from the text, and your 5 pointed star. NOTE: How to use the turtle module in Wing 101 In order to run turtle in Wing 101, you have to make an adjustment in the Python configuration. You should only have to do this once. 1. Go to Edit -> Configure Python... 2. In the Python Configuration dialog, under Environment, select "Add to inherited environment" from the dropdown 3. Add the following two lines in the text box (you should be able to copy/paste from here) TCL_LIBRARY=C:\Python34\tcl\tcl8.6 TK_LIBRARY=C:\Python34\tcl\tk8.6 4. Click OK. (The two paths above may have to be updated when there is a newer installation of Python on the lab machines.) (See the screenshot on the next page.)