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

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

CS 1110, LAB 1: PYTHON EXPRESSIONS.

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

CS1110 Lab 1 (Jan 27-28, 2015)

CS 1110, LAB 3: STRINGS; TESTING

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

Lecture 2: Variables & Assignments

Python Programming Exercises 1

Lecture 3: Functions & Modules

Course Overview, Python Basics

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

Science One CS : Getting Started

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

Lecture 6: Specifications & Testing

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

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

Fundamentals: Expressions and Assignment

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

Lecture 1. Course Overview, Python Basics

Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

Lecture 1. Course Overview, Python Basics

Laboratory Exercise #0

Lecture 1. Types, Expressions, & Variables

Conditionals & Control Flow

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

CMSC 201 Spring 2018 Lab 01 Hello World

Lecture 24: Loop Invariants

STATS 507 Data Analysis in Python. Lecture 0: Introduction and Administrivia

Lecture 4: Defining Functions

Semester 2, 2018: Lab 1

Lecture 11: Iteration and For-Loops

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

CS 1110: Introduction to Computing Using Python Loop Invariants

Table of Contents EVALUATION COPY

Python for Bioinformatics Fall 2014

Installing Python 3 on Your Personal Computer

cs1114 REVIEW of details test closed laptop period

Lecture 2. Variables & Assignment

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

Computer Lab 1: Introduction to Python

FIT 100: Fluency with Information Technology

CMSC 201 Spring 2017 Lab 01 Hello World

CS10001: Computer Literacy Lab Assignment #3, Part #2

last time in cs recitations. computer commands. today s topics.

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CSC108: Introduction to Computer Programming. Lecture 1

STAT 213: R/RStudio Intro

Lecture 1. Course Overview Types & Expressions

Lab 7c: Rainfall patterns and drainage density

Programming for Engineers in Python. Recitation 1

Lecture 5: Strings

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lab 1: Setup 12:00 PM, Sep 10, 2017

isinstance and While Loops

Programming for Engineers in Python. Autumn

Getting Started with Python and the PyCharm IDE

Python for Non-programmers

Text Input and Conditionals

Lecture 17: Classes (Chapter 15)

NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Statistics 13, Lab 1. Getting Started. The Mac. Launching RStudio and loading data

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

Lecture 3. Functions & Modules

Rio Hondo Prep Computer Applications Class

Act like a code monkey Coding Basics

STAT 113: R/RStudio Intro

CSCI 201 Lab #11 Prof. Jeffrey Miller 1/23. Lab #11 CSCI 201. Title MySQL Installation. Lecture Topics Emphasized Databases

Programming Fundamentals and Python

Lecture 10: Lists and Sequences

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

FIT 100: Fluency with Information Technology

CMSC 201 Fall 2018 Lab 04 While Loops

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

AP Computer Science Principles: Submitting Python Code to Web-CAT

Tutorial 1: Unix Basics

LAB #5 Intro to Linux and Python on ENGR

Lecture 5. Defining Functions

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CS50 Supersection (for those less comfortable)

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Iteration and For Loops

Working with Macros. Creating a Macro

CS 102 Lab 3 Fall 2012

CMPSCI 145 Spring 2014 Lab #1. Professor William T. Verts

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CSCI 136 Data Structures & Advanced Programming. Spring 2018 Instructors Bill Jannen & Jon Park

Lecture 1: Introduction, Types & Expressions

CS1110 Lab 5: Practice for A4 (Mar 8-9, 2016) First Name: Last Name: NetID:

Lecture 17: Classes (Chapter 15)

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Lab: Supplying Inputs to Programs

CS Lecture 19: Loop invariants

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

VDX. VDX Web Admin Manual Pt.1

Transcription:

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS http://www.cs.cornell.edu/courses/cs1110/2018sp/labs/lab01/lab01.pdf First Name: Last Name: NetID: Learning goals: (1) get hands-on experience using Python in interactive mode via the command shell; (2) get hands-on experience with Python types, expressions, and variables. Learning a computer language is a lot like learning a new human language. This lab essentially works as a grammar drill before we get started with programming itself. We suggest you open this lab handout on a Web browser so that you can click on website URLs, copy-and-paste text, etc. You can copy the URL given in the title above, or go to the course website s Labs page. Getting Credit for the Lab Labs are graded on effort, not correctness. We just want to see that you tried all the exercises, and to clarify any misunderstandings or questions you have. When finished, show your written answers to a lab instructor. They will ask you some questions to ensure your understanding and record your lab completion. You can keep the physical handout. If you don t finish during today s lab session, here are some alternate times/places for checking off this lab: at ACCEL Green room consulting hours, listed at http://www.cs.cornell.edu/courses/ cs1110/2018sp/about/staff.php, from today to Tue Feb 6 inclusive; at non-professorial TA office hours from now up to Wed Feb 7 3:45pm, although at TA office hours, questions about course material or assignments take precedence over lab check-offs; during the first 10 minutes of your next scheduled lab (Tue Feb 6 or Wed Feb 7). Beyond that time, the staff have been instructed not to give you credit. 1. Getting Registered on the Lab Recording Machine Sign in with your netid and password at https://cs1110.cs.cornell.edu/labs. 2. Getting Started with Python Next, start up the command shell on the computer you are working on. This would be the Command Prompt on Windows, or the Terminal on OS X (Macs). If you are unsure of what these mean, read just the first one or two paragraphs for your operating system here: http://www.cs.cornell.edu/courses/cs1110/2018sp/materials/command.php Course authors: E. Andersen, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White 1

You do not need to read the sections on navigating directories; we will exercise that in a later lab. After you have started the command shell, type python (and then hit the return key) at the prompt. You should see banner output that looks something like this, with the word Anaconda somewhere (the following output was generated on a Mac): Python 3.6.1 Anaconda 4.4.0 (x86_64) (default, May 11 2017, 13:04:09) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 3. s The following pages contain lists of expressions. Proceed row-wise (fill in the second and third column of a row before moving on to the next row). You want to learn from earlier examples before moving on to the next one. For each expression, (1) Evaluate the expression to get its value in your head, without Python. Write down what you think the value is in the second column of the table. If you have no idea, write?. (2) Next, use Python to evaluate the same expression, by entering it in at the Python prompt, >>>, and then hitting the return key. (Alternately, copy-and-paste it from the online version of these instructions.) Write down Python s result in the third column. If the values in column two and three differ, spend a few seconds trying to figure out why Python gave the answer that it did, and put the reason in the final column. Don t waste too much time trying to figure things out yourself. If you do not understand something, ask a staff member immediately. 3.1. Types and Castin. Useful Shortcuts: The up-arrow key gives you the previous expression that you typed in, and you can hit it repeatedly. If you go too far back, you can press the down-arrow key to get to a later expression. The left and right-arrow keys move the text cursor on a single line. Using all of these keys together, you can take a previously-used expression, modify it, and try it again. type(4) (You did the entire row above before proceeding, right? :) type(4.0) float(4) int(4.3) int(5.7) 2

3.2. s with ints and floats. If any difference, why? 4 + 2 * 5 (4 + 2) * 5-4 - -4 - -4 2 ** 3 2 ** 3 ** 0 (2 ** 3) ** 0 3.0/2.0 3 // 2. 3 / 2 (Note that float division treats its arguments as floats.) float(3 // 2) 3.0 // 2.0 (Note: This is, admittedly, a weird case, but for completeness sake we include it to point out that int division on floats converts its arguments to ints, but then converts its int answer to a float) 6 % 2 7 % 2 3

3.3. Comparisons and Boolean s. 3 < 5 3 < 5 and 5 < 3 True true True and False True and True True or False False or False not True not not False not False and True not (False or True) True and False and True True or (False and True) (5 / 0 == 1) and False False and (5 / 0 == 1) Why does the last expression in the table above work but the one above it doesn t? 4

3.4. String s. Pay close attention to spaces and to the different types of quotation marks being used; we use both ' (single quote) and " (double quote). Copy-paste from the online handout might be your best option here. 'now ' + 'here now' + 'here' "now " + "here now" + "here" 'A double quote: "' 'A double quote: " 'Output: ' + '4 / 2' 'Output: ' + 4 / 2 'Output: ' + str(4 / 2) 5

3.5. Variables and Assignment Statements. The last part of this lab involves assignment statements. An assignment statement like is a command to Python to: b = 3 < 5 (1) evaluate the expression on the right-hand side of the = (in this case, 3 < 5), and (2) store the expression s value in the variable on the left-hand side of the =, in this case, b. Because the assignment statement is not an expression, Python will not output a result when you type it in; it will just perform the command silently. In the 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 (we have done the first one for you). Because some of the entries are commands, it is important that you enter the expressions or commands in exactly the order they are given. Statement or i = 2 None i j j = 1 j j = j + i j i w = 'Hello' i + w 6