CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.

Slides:



Advertisements
Similar presentations
CompSci 101 Introduction to Computer Science January 22, 2015 Prof. Rodger compsci101 spring151.
Advertisements

CompSci 101 Introduction to Computer Science Feb 26, 2015 Prof. Rodger.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Python Control of Flow.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014
Walk through previous lecture. TSP Questions: How many tours are there? How do you solve it? How fast can you solve it?
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CompSci 101 Introduction to Computer Science Sept. 9, 2014 Prof. Rodger President Brodhead speech graduation 2010 CompSci 101 Fall
CompSci 6 Introduction to Computer Science Sept 29, 2011 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
CompSci 6 Introduction to Computer Science November 8, 2011 Prof. Rodger.
CompSci 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 3, 2015 Prof. Rodger compsci101 spring151.
CompSci 6 Introduction to Computer Science September 13, 2011 Prof. Rodger.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CompSci 101 Introduction to Computer Science February 10, 2015 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science Feb 24, 2015 Prof. Rodger.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
CompSci 101 Introduction to Computer Science February 11, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science November 11, 2014 Prof. Rodger CompSci 101 Fall Review for exam.
CompSci 101 Introduction to Computer Science March 17, 2015 Prof. Rodger compsci 101, spring
CompSci 4 Chap 10 Nov 22, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
CompSci 101 Introduction to Computer Science February 25, 2016 Prof. Rodger compsci101 spring20161.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
CompSci 6 Introduction to Computer Science September 27, 2011 Prof. Rodger CompSci 6 Fall
CompSci 101 Introduction to Computer Science February 16, 2016 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
CompSci 101 Introduction to Computer Science April 7, 2015 Prof. Rodger.
CompSci 101 Introduction to Computer Science March 24, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CompSci 101 Introduction to Computer Science March 1, 2016 Prof. Rodger.
Input, Output and Variables GCSE Computer Science – Python.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CompSci 4 Java 1 Apr 2, 2009 Prof. Susan Rodger. Announcements Assignment 7 questions? –Beware having two events that kick in at the same time! –Beware.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Computer Programming Fundamentals
Python: Control Structures
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CSC 108H: Introduction to Computer Programming
CompSci 101 Introduction to Computer Science
Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CS190/295 Programming in Python for Life Sciences: Lecture 6
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Loops CIS 40 – Introduction to Programming in Python
LING 408/508: Computational Techniques for Linguists
Nate Brunelle Today: Conditional Decision Statements
COMPUTER PROGRAMMING SKILLS
COMPUTING.
CompSci 101 Introduction to Computer Science
Presentation transcript:

CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd

Announcements Reading RQ08 for next Assignment 3 due today, Asg 4 out APT 3 is due on Thursday Exam 1 – Feb 12 –try practice test by Feb 10 –Accommodations? Reschedule? Fill out forms! Prof Rodger – at ACM India conference Finish lecture notes from last time

Assignment 4 – Transform

Lists A list is a collection of objects scores = [99, 78, 91, 84] allAboutMe = [“Mo”,25, “ ”] club=[‘Mo’,‘Jo’,‘Po’, ‘Flo’, ‘Bo’] Lists are mutable – use [num] to change a value Lists are indexed starting at 0, or -1 from the end Functions: max, min, len, sum Slice lists [:] compsci101 spring154

List Examples scores = [10, 8, 10, 9] print scores scores[2] = 5 print scores print max(scores) print len(scores) print sum(scores) print scores[1:] print scores[1] compsci101 spring155

List before/after modification score = [10,8,10,9] score [2] = compsci101 spring156

Processing List Items Process all the items in a list, one item at a time Format: for variable in list: block Example: sum = 0 nums = [6, 7, 3, 1, 2] for value in nums: sum = sum + value print sum compsci101 spring157

Copying vs aliasing bit.ly/101S names = [‘jo’, ‘mo’, ‘bo’] club = names team = names[:] names[1] = ‘flo’ print names print club print team compsci101 spring158

Making Decisions Question ? True False compsci101 spring159

Making Decisions in Python if condition1: Block of code to do if condition is true elif condition2: Block of code to do if condition1 false, condition2 is true else: Block of code to do if other conditions false Can have many elifs, leave out elif, leave out else compsci101 spring1510

Making Decisions tools Boolean values: True, False Boolean operators: and, or, not Relational operators:, >= Equality operators: ==, != Look at if examples: miscIf.py X Y X and Y X or Y True False True False True False True False compsci101 spring1511

Compare Ifs bit.ly/101S

More on lists range (1,11) –Generates a list of numbers from 1 to 10 Example: for num in range(1,11): print num compsci101 spring1513

Example answer = 0 for num in range(1,10): if num % 3 == 0: answer = answer + num else: answer = answer + 1 compsci101 spring1514

Dissect the for loop for VARIABLE in STRUCTURE: BODY Repeat the BODY with the VARIABLE equal to each item in structure

What can the structure be? Variable be? STRUCTURE → Variable String → character List → item in list There are other types of structures we will see

Examples

Reading from Files Must open file, close file Easiest way, read one line as a string and then process it inputfile = open(“datafile.txt”) for line in inputfile: line = line.strip() ‘’’ do something with line ‘’’ inputfile.close()

Dissect the for loop (again) for VARIABLE in STRUCTURE: BODY inputFile = open(“somefile.txt”) for str in inputFile: process str

Writing to File Must open file, close file Open file for writing outfile = open(“outputfile.txt”, ‘w’) phrases = [“hello there”, … ] for phr in phrases: outfile.write(phr + “\n”) outfile.close() Note: refresh to see the file

Exercise with files Snarf code Uppity.py