Comp Sci 101 Introduction to Computer Science Jan

  • Slides: 22
Download presentation
Comp. Sci 101 Introduction to Computer Science Jan. 24, 2017 Prof. Rodger compsci 101,

Comp. Sci 101 Introduction to Computer Science Jan. 24, 2017 Prof. Rodger compsci 101, spring 2017 1

Announcements • • Reading and RQ 4 due next time Asgn 2 out, APT

Announcements • • Reading and RQ 4 due next time Asgn 2 out, APT 1 is due Thursday Lab 2 this week Add class or change sections? – see forms: www. cs. duke. edu/courses/compsci 101/spring 17 • Today – more APT practice – functions, parameters – Names, types and values compsci 101, spring 2017 2

Organization matters • https: //www. youtube. com/watch? v=1 ve 57 l 3 c 19

Organization matters • https: //www. youtube. com/watch? v=1 ve 57 l 3 c 19 g compsci 101, spring 2017 3

APT organization, Code organization • You’ve written the BMI. py APT – Where is

APT organization, Code organization • You’ve written the BMI. py APT – Where is that module? How do you test it? – Py. Dev console, but then must import it – Adding print statements in BMI. py to test • Putting sentences together in order… – “Once upon a time…” “It was the best of times…” “Aujord’hui ma maman est morte” • Putting code together in order – Takes judgment and experience compsci 101, spring 2017 4

REVIEW: Solving APT BMI • Write your code in Eclipse – Create python file

REVIEW: Solving APT BMI • Write your code in Eclipse – Create python file - with Module: Main – Name of file important – case matters – name of function important – cut and paste this – Write your code – Test a few examples in Eclipse • Run online on using APT Tester – Test on examples, Debug, fix, get all GREEN • Submit on APT page – must run again, then check score – Fill out README form too 5

Functions can print info • Some functions only print info • Note there is

Functions can print info • Some functions only print info • Note there is no return statement in the function def hello. Person(name): print "hello" + name hello. Person("Susan") hello. Person("Ademola") compsci 101, spring 2017 6

Functions return values • Most functions return values – Sometimes used to make things

Functions return values • Most functions return values – Sometimes used to make things simpler, but returning values is a good idea def inch 2 centi(inches): return 2. 54*inches xh = inch 2 centi(72) def pluralize(word): return word + "es" pf = pluralize("fish") compsci 101, spring 2017 7

Function Detective • http: //bit. ly/101 s 17 -0124 -1 compsci 101, spring 2017

Function Detective • http: //bit. ly/101 s 17 -0124 -1 compsci 101, spring 2017 8

Results of Code Analysis • For details on plurals: http: //bit. ly/1 N 49

Results of Code Analysis • For details on plurals: http: //bit. ly/1 N 49 u 6 b • How did we call pluralize many times? – Loop. What is an alternative? • What does the 'if' statement do? – Selects a code block to execute (more next week) • If you have a question? Write and run code!9

Python – Names and Types • Names vs abstractions – What is http: //152.

Python – Names and Types • Names vs abstractions – What is http: //152. 3. 140. 1 – What is http: //www. amazon. com • Types are important – What is foo. pdf, foo. mp 4, foo. jpg, foo. wav – Do the file extensions guarantee file type? • Python – what types are these? first = "Susan" x = 6 y = 3. 4 compsci 101, spring 2017 10

Strings • Sequence of characters in quotes "I" + 'Love' + "I" 'Love' '''Python'''

Strings • Sequence of characters in quotes "I" + 'Love' + "I" 'Love' '''Python''' 'ILove. Python' • String operators: concatenation (+), repeat(*) • Precedence? "a" + "b" + "c" * 3 'abccc' • Precedence? "a" + "b" "c" * 3 compsci 101, spring 2017 'abcbcbc' 11

Strings • Sequence of characters in quotes (same result) "I" + 'Love' + "I"

Strings • Sequence of characters in quotes (same result) "I" + 'Love' + "I" 'Love' '''Python''' 'ILove. Python' • String operators: concatenation (+), repeat(*) • Precedence? "a" + "b" + "c" * 3 'abccc' • Precedence? "a" + "b" "c" * 3 compsci 101, spring 2017 'abcbcbc' 12

Function • def function. Name(parameters): block of code • Parameters – place holder, will

Function • def function. Name(parameters): block of code • Parameters – place holder, will store value passed in • Arguments – values in the call, that you pass to the function to use as input • Body of function must be indented compsci 101, spring 2017 13

Function – return or print? bit. ly/101 s 17 -0124 -2 • Example function

Function – return or print? bit. ly/101 s 17 -0124 -2 • Example function that returns a value def sum(a, b): return a+b • Example function that prints def hw(name): print "Hello " + name • Call Functions print sum(4, 7) answer = sum(4, 7) name = hw(“Sue”) print hw(“Jo”)

Function – return or print? bit. ly/101 s 17 -0124 -2 • Example function

Function – return or print? bit. ly/101 s 17 -0124 -2 • Example function that returns a value def sum(a, b): return a+b • Example function that prints def hw(name): print "Hello " + name • Call Functions print sum(4, 7) answer = sum(4, 7) x x x name = hw(“Sue”) print hw(“Jo”)

Old Mac. Donald Song • Write a Program to print this song compsci 101,

Old Mac. Donald Song • Write a Program to print this song compsci 101, spring 2017 16

Function Old. Mac. Pig() compsci 101, spring 2017 17

Function Old. Mac. Pig() compsci 101, spring 2017 17

Rest of Code • Function Old. Mac. Cow – Replace “pig” with “cow” –

Rest of Code • Function Old. Mac. Cow – Replace “pig” with “cow” – Replace “Oink” with “Moo” • Code to start: compsci 101, spring 2017 18

Discuss how to make code better bit. ly/101 s 17 -0124 -3 • Describe

Discuss how to make code better bit. ly/101 s 17 -0124 -3 • Describe in words how you can make the code better? More efficient? – Fewer lines of code? – Use more functions? – Discuss your changes. • What advantages do the changes you make have? compsci 101, spring 2017 19

Demo – Old Mac improvements • What does the horse say? • What does

Demo – Old Mac improvements • What does the horse say? • What does the cow say? • What does the fox say? compsci 101, spring 2017 20

Assignment 2 out • Totem poles – printing heads – functions 21

Assignment 2 out • Totem poles – printing heads – functions 21

Names, Types and Values • bit. ly/101 s 17 -0124 -4 compsci 101, spring

Names, Types and Values • bit. ly/101 s 17 -0124 -4 compsci 101, spring 2017 22