CMSC201 Computer Science I for Majors

Similar documents
CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

CMSC 201 Computer Science I for Majors

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

Text Input and Conditionals

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Spring 2017 Lab 05 Lists

CMSC 201 Spring 2019 Lab 06 Lists

Python Intro GIS Week 1. Jake K. Carr

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

CMSC201 Computer Science I for Majors

Types, lists & functions

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

cs1114 REVIEW of details test closed laptop period

CS108 Lecture 19: The Python DBAPI

CMSC201 Computer Science I for Majors

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Python: common syntax

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

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

The Big Python Guide

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

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

At full speed with Python

Level 3 Computing Year 2 Lecturer: Phil Smith

CMSC201 Computer Science I for Majors

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

Ch.2: Loops and lists

CMSC 201 Spring 2018 Project 3 Minesweeper

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Variable and Data Type I

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

MIT AITI Python Software Development

CMSC 201 Spring 2018

Slicing. Open pizza_slicer.py

Introduction to Python (All the Basic Stuff)

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Lecture #7: Recursion (and a data structure)

1/11/2010 Topic 2: Introduction to Programming 1 1

CMSC 341 Lecture 7 Lists

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

CMSC201 Computer Science I for Majors

Expressions and Variables

Notebook. March 30, 2019

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

Python for ArcGIS. Lab 1.

Python memento TI-Smart Grids

Variable and Data Type I

Programming Fundamentals and Python

CS 11 python track: lecture 3. n Today: Useful coding idioms

Lecture 2. Variables & Assignment

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

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

Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Iterators & Generators

CMSC202 Computer Science II for Majors

Topic 2: Introduction to Programming

Python 1: Intro! Max Dougherty Andrew Schmitt

Genome Sciences 373: Genome Informatics. Quiz section #1 March 29, 2018

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

The current topic: Python. Announcements. Python. Python

Python Programming: Lecture 2 Data Types

SIMPLE INPUT and OUTPUT:

CMSC201 Computer Science I for Majors

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

Python - Variable Types. John R. Woodward

CIS192 Python Programming. Robert Rand. August 27, 2015

Python 2 Conditionals and loops Matthew Egbert CS111

Programming with Python

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Lists, loops and decisions

Lecture 1. Types, Expressions, & Variables

Python for Non-programmers

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Expressions, Statements, Variables, Assignments, Types

Overview of List Syntax

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Expressions and Casting

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

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Intro. Scheme Basics. scm> 5 5. scm>

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Onion Language Reference Manual. By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814)

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

CMSC201 Computer Science I for Majors

PREPARING FOR PRELIM 1

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Python and Bioinformatics. Pierre Parutto

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

Transcription:

CMSC201 Computer Science I for Majors Lecture 12 Tuples All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted

Modularity Meaning Benefits Program design Last Class We Covered Top Down Design Top Down Implementation Bottom Up Implementation 2

3 Any Questions from Last Time?

Today s Objectives To learn about the tuple data structure in Python To be able to perform basic operations with tuples including: Creation, conversion, slicing, traversing To discuss casting in detail To discuss the membership in operator 4

5 Tuples

The Tuple Data Structure Tuples are sequences of objects Just like lists! Lists use square brackets [ ] Tuples use parentheses ( ) They have some other differences, but we ll discuss those later in the semester 6

7 Creating a Tuple To create a tuple, simply use parentheses around a comma-separated list of items classtup = (201, 202, 341, 313) How do you think you create an empty tuple? emptytup = () What about a tuple with one element?

One-Element Tuples At first glance, you might think that it s this: oneitem = (201) But this won t work it s an int (why?) Parentheses are used for more than just tuples Also used for order of operations 8 To create a one element tuple, use a comma oneitemtuple = (201,)

Tuple Operators and Properties Tuples are ordered, and can contain more than one type of variable, just like lists They can even contain another tuple! Tuples can also be indexed, concatenated, sliced, and can use the len() function Just like strings and lists 9

Tuple Exercises You are given the following tuple: hounds = ("Ibizan", "Afghan", "Serbian", "Bassett") Write pieces of code that do the following: 1. Print out each element followed by Hound 2. Use len() to print how many hounds there are 3. Use slicing to create a tuple called oldbreeds that includes only the Afghan and Serbian hound 4. Print out each element in reverse order 10

11 Types and Casting

Finding a Variable s Type This is a bit of a review, but we haven t covered this in detail before To find what type a variable is, use type() Example: >>> a = 3.0 >>> type(a) <class 'float'> >>> b = "moo" >>> type(b) <class 'str'> 12

Casting to a Type We can change a value from one type to another using something called casting Example: >>> e = 2.718 >>> int(e) 2 >>> str(e) '2.718' The type you want to cast to, then the variable whose value you want to cast This code means: show what e is as an integer 13

Casting to a Type: Assignment Casting alone doesn t change the variable s type >>> coursenum = "201" >>> int(coursenum) 201 >>> type(coursenum) <class 'str'> cast coursenum s value to an integer type is still a string (!?) To make an actual change, you need to save it with the assignment operator 14

Casting to a Type: Assignment Use the assignment operator (=) to actually change the variable s type >>> coursenum = "201" >>> type(coursenum) <class 'str'> >>> coursenum = int(coursenum) >>> type(coursenum) <class 'int'> this is what actually causes the variable s type to change 15

16 Membership Operator

Types of Operators in Python Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Membership Operators Bitwise Operators Identity Operators what we re covering now 17

Membership Operator The membership operator is very powerful What do you think this code does? hounds = ("Ibizan", "Afghan", "Serbian", "Bassett") guess = input("please enter a dog: ") while guess not in hounds: print("you guessed wrong!") guess = input("guess again: ") 18 Runs until the user guesses a dog in the tuple

Membership in Operator Syntax: element in sequence element to look for in keyword may be a list, tuple, or string Checks to see if element exists in sequence Evaluates to either True or False Use it together with while, if, or elif Can also use not in to test for absence 19

20 It s Time For

The Tuple of Secrets Write a function that takes in a tuple, and asks the user to guess what is in the tuple Counting the number of correct guesses made Use a sentinel loop to let them keep guessing Return number of correct guesses You ll want to use: Actual parameters and return While loops and membership in 21 Image from wikimedia.org

Tuple of Secrets Sample Run With a tuple whose contents are set in main() bash-4.1$ python fxnprac.py Please enter your guess (stop to quit): hello hello is NOT in the tuple of secrets Please enter your guess (stop to quit): Hrabowski? Hrabowski? is NOT in the tuple of secrets Please enter your guess (stop to quit): dogs are great dogs are great is in the tuple of secrets! Please enter your guess (stop to quit): cats are great cats are great is NOT in the tuple of secrets Please enter your guess (stop to quit): stop You got 1 correct guesses 22

Announcements Project 1 is out on Blackboard now Must use the design (posted on Blackboard now) Design due by Saturday (March 11th) at 8:59:59 PM Project due by Friday (March 17th) at 8:59:59 PM 23 Midterm will be next week We ll have an in-class review on Monday/Tuesday Review worksheet only available in class! Start studying on your own now!