CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

Similar documents
CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

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

CS 102 Lab 3 Fall 2012

Downloaded from Chapter 2. Functions

Lecture 3. Functions & Modules

Lecture 3. Functions & Modules

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

CS 1110, LAB 1: PYTHON EXPRESSIONS.

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

CS 1110, LAB 03: STRINGS; TESTING First Name: Last Name: NetID:

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

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

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

Lecture 3: Functions & Modules

CS1110 Lab 1 (Jan 27-28, 2015)

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Module 01: Introduction to Programming in Python

Lab 1 Introduction to UNIX and C

CSE 115. Introduction to Computer Science I

Variable Manipulator Driver. Installation and Usage Guide. Revision: 1.0 Date: Monday, July 10, 2017 Authors: Alan Chow

CS1110 Lab 6 (Mar 17-18, 2015)

CS Lecture 19: Loop invariants

CS 1110, LAB 3: STRINGS; TESTING

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.

CS1 Lecture 4 Jan. 24, 2018

Lecture 1. Course Overview, Python Basics

CS 1110 SPRING 2016: LAB 3: PRACTICE FOR A2 (Feb 23-24)

An Introduction to Python for KS4!

PA101 Learning the Ropes

The Standard Library

Conditionals & Control Flow

CS1 Lecture 4 Jan. 23, 2019

Database Concepts Using Microsoft Access

Lecture 4: Defining Functions

Lecture 1. Course Overview, Python Basics

Using external Media such as CD's and Pen Drives. Reading from a CD and writing to a pen drive Contributers: Aruna Prabahala Std: III Grade Level

Computer Science 1 CSCI-1100 Spring Semester 2015 Exam 1 Overview and Practice Questions

CMSC 201 Spring 2019 Lab 06 Lists

CS110: PROGRAMMING LANGUAGE I

CS 1110: Introduction to Computing Using Python Loop Invariants

Lecture 3. Strings, Functions, & Modules

STAT 113: R/RStudio Intro

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

Week 1: Introduction to R, part 1

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

The Math Class. Using various math class methods. Formatting the values.

Lecture 6: Specifications & Testing

Lecture 1. Course Overview Types & Expressions

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

Submitting Assignments

Intro to Python & Programming. C-START Python PD Workshop

STAT 213: R/RStudio Intro

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Lecture 2: Variables & Assignments

Lecture 1. Types, Expressions, & Variables

CS 126 Lecture P1: Introduction to C

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

WWU CSCI 101 Project 2. Project 2 - Fence Pricing Calculator. Goals: Guideline: 1. Familiarity with web page development using HTML and JavaScript

Python Programming Exercises 1

The Hong Kong Polytechnic University

CMSC 201 Spring 2017 Lab 05 Lists

CS 1110, LAB 13: SEQUENCE ALGORITHMS

Unit 7: Algorithms and Python CS 101, Fall 2018

EXCEL BASICS: MICROSOFT OFFICE 2010

Departmental Schedule Validator (DSV) Procedure Manual

APPM 2460 Matlab Basics

EXCEL BASICS: MICROSOFT OFFICE 2007

Lecture 4. Defining Functions

Lecture 5. Defining Functions

Cmpt 101 Lab 1 - Outline

CSI Lab 02. Tuesday, January 21st

Lab 1 Introduction to UNIX and C

CMSC 201 Spring 2017 Lab 01 Hello World

cs1114 REVIEW of details test closed laptop period

Valuable points from Lesson 6 Adobe Flash CS5 Professional Classroom in a Book

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

CSC D84 Assignment 2 Game Trees and Mini-Max

Lab 2 Building on Linux

CISC 110 Week 3. Expressions, Statements, Programming Style, and Test Review

CS 051 Homework Laboratory #2

Document Maker 1.0 User Guide

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

Scientific Programming Algolab. Wednesday 21 Dec 2016

JAVASCRIPT BASICS. JavaScript Math Functions. The Math functions helps you to perform mathematical tasks

Using the Dev C++ Compiler to Create a Program

2. Modules, Scripts, and I/O. Quick Note on print. The Windchill Calculation. Script Mode. Motivating Script Mode 1/22/2016

CSCI 121: Anatomy of a Python Script

n! = 1 * 2 * 3 * 4 * * (n-1) * n

What is the Best Way for Children to Learn Computer Programming?

EDIT202 Spreadsheet Lab Prep Sheet

2. Modules, Scripts, and I/O

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

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

Iteration and For Loops

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

EXCEL BASICS: PROJECTS

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

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

Laboratory Exercise #0

Transcription:

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS http://www.cs.cornell.edu/courses/cs1110/2017fa/labs/lab2/ First Name: Last Name: NetID: The purpose of this lab is to get you comfortable with using assignment statements, functions, and their associated modules. These are all a major part of the first assignment. The early labs are designed to get you ready for that assignment. This lab is very similar to the previous one, in that you will be typing commands into the Python interactive shell and recording the results. As before, there are two tables. The first table challenges you to evaluate an expression. The second table asks you to fill in the blank, completing an expression to give you the provided value. Getting Credit for the Lab. Once again, you have a choice between getting credit through the online system or your instructor. The online lab is available at the web page http://www.cs.cornell.edu/courses/cs1110/2017fa/labs/lab2/ The advantage of the online system is that you can do it on your own time and verify that you got credit. The disadvantage is that your answers must be correct. If you want more g uided help on this lab, you should use the worksheet instead. Despite the demands of the online system, labs are graded on effort, not correctness. If you use this worksheet, your answer will include both a sheet of paper (or the sheet provided to you in lab) and file called dice.py. This is a file that you will create frome scratch. When you are finished, you should show both this file and your written answers to your lab instructor, who will record that you did it. This lab is long enough that you may not finish during class time. If you do not finish, you have until the beginning of lab next week to finish it. Over the next week, you may either (1) complete the lab online, (2) show your lab to a consultant during consulting hours, or (3) show your lab to an instructor at the beginning of the next lab session. The online version will stop receiving submissions after Wednesday at midnight. Course authors: D. Gries, L. Lee, S. Marschner, W. White 1

1. Built-In Functions Below are two sets of tables. The first table challenges you to evaluate an expression. Use Python to evaluate the expression and type the result into the box. It is possible that the expression may produce an error. In that case, simply write error in the box. The second table is looks very similar to the first table, but this time we have given you both the expression and the value. However, the expression is missing a literal, as indicated by the box. In the third column you are to fill in the missing literal. Remember that a literal is an expression that looks like a value. So 12 and True are literals, while (10+2) is not. Expression min(25, 4) max(25, 4) min(5,max(7,4)) abs(25) abs(-25) round(25.6) round(-25.6) round(25.64, 0) round(25.64, 1) round(25.64, 2) len('hello') chr(65) chr(66) ord('a') ord('ab') Calculated Expression Calculated Missing min(,4) 2 max(,4) 6 min(4,max(,2)) 3 abs( )+ 4 abs( )- 4 round( +0.4) 2 round( +0.4) -2 round(5.18, ) 5.0 round(5.18, ) 5.2 round(5.18, ) 10.0 len( ) 2 len( ) 0 chr( ) 'C' ord( ) 66 ord(chr( )) 121 2

2. Using a Python Module To use a module, you must import it. Type the following into the Python interactive prompt: >>> import math You can now access all of the functions and variables in math. However, to use any of them, you have to put math. before the function or variable name. For example, to access the variable pi, you must type math.pi. With this in mind, fill out the tables below. The instructions for these tables is the same as for the previous activity. Read the web page for the math module if you are having trouble. Expression math.sqrt(9) math.sqrt(-9) sqrt(4) math.floor(3.7) math.ceil(3.7) math.ceil(-3.7) math.trunc(3.7) math.trunc(-3.7) math.pi math.cos(math.pi) math.acos(1.0) math.e math.log(math.e) math.log(4,2) Calculated Expression Calculated Missing math.sqrt( ) 4.0 math.sqrt( )**2 4.0.sqrt(16) 4.0 math.floor( +0.5) -2 math.floor( +0.5) 2 math.ceil( +0.5) 2 math.trunc( +0.5) 2 math.trunc( +0.5) -2 math.cos( ) 1.0 math.sin( ) 0.0 math.asin( ) 0.0 math.log( ) 0.0 math.log(math.e** ) 4.0 math.log(,2) 3.0 3

3. Variables and Assignment Statements As we saw in class, assignment statements are different from expressions. A statement like >>> b = 3 < 5 is a command to do something. In particular, this command (1) Evaluates the expression on the right-hand side of the = (in this case, 3 < 5). (2) Stores its value in the variable on the left-hand side of the = (in this case, b). Because it is not an expression, Python will not actually output a result when you type it in; it will just perform the command silently. It is important that you understand the difference. In the first table below, the first column contains either an expression or a command. If it is an expression, write the value. If it is a command, you should just write None (capitalization is important here). However, you need to remember this command as it will affect the answers for later rows in the table. This is why it is very important that you enter the expressions or commands in exactly the order they are given. The second table is a bit different from the previous activities. This time the first column only has assignment statements. However, these statements are either missing a literal or a variable, as indicated by the box. The second column has an expression that should be True when evaluated. You are to fill in the blank with either a literal or a variable that makes this expression true (this implies that all variables exist, so there are no errors). For example the answer to the first row should be x. Once again, assignments are assumed to be in order; answers to one row may affect later rows. Expression i = 2 i j j = 1 j j = j+i j i w = 'Hello' i+w Calculated Expression Calculated Missing =4 x == 4 =6 x!= 4 =2 x == 3*y x=y+ x == 8 z = ( == x) z 4

4. Die Roller Script For the last part of this lab, you will create a module/script. This is a single file that will work as both a module and a script, depending upon how you use it. This is an important exercise. The lessons you learn here will form a core part of the lab next week. A lot of board games require you to roll two (six-sided) dice and add the results together. In this lab you will create a script that essentially does this. Of course, Python does not have any physical dice to roll. But it can generate random numbers, which is the same thing. To understand how random numbers work in python, you should read the documentation for the module random. http://docs.python.org/3/library/random.html In particular, you want to pay attention to the function randint(a,b). We want you create a file called dice.py. In that file, you should do three things: (1) Generate two random numbers in the range 1 to 6 (inclusive) (2) Add the numbers together and assign them to the variable roll (3) Print the variable roll at the end. In addition to these steps, we would like you to place a document comment at the start of the file. A document comment starts and ends with three quotes ("""). It also contains the following four lines: (1) The first line is a one-line description of the module. (2) The second line is blank. (3) The third line your name and net-id (4) The last line is today s date. As we shall see in class, sometimes there are more lines between line two and three. But four is enough for now, and is typically the format we will use for this class. As a general rule, all but the last two lines will be provided for you, and you will need to write these last two lines (name, net-id, and date) yourself. For the file dice.py we want the first line to be A simple die roller Complete the file dice.py before going on to the next questions. 5. Questions While we can test your script, we want you to test it on your own. Create a folder called lab02 and store dice.py in this folder. Like you did last week, open up a command shell and navigate to this folder. Start Python in interactive mode and type >>> import dice 5

What do you see? Now type the command >>> dice.roll Again, what do you see? How does it compare to what you saw before? The document comment that you wrote at the top is important, as it provides the user with information on how to use the script. Type the command. >>> help(dice) What do you see just below the word Name (which is in bold)? Finally, quit Python so that you are back in the general command shell from last week s lab. Now we want you to run dice.py as a script. In your command shell, type python dice.py One last time, what do you see? 6