Comp Sci 101 Introduction to Computer Science January

  • Slides: 13
Download presentation
Comp. Sci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101,

Comp. Sci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring 2015 1

Announcements • Reading, RQ 3 Due • Assignment 1 due today, Assg 2 out!

Announcements • Reading, RQ 3 Due • Assignment 1 due today, Assg 2 out! • APT 1 is due on Thursday • Finish lecture notes - last time for Sec 02 • Today – more on functions, strings compsci 101, spring 2015 2

Python – Programming Concepts • Names vs abstractions – What is http: //152. 3.

Python – Programming Concepts • 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 2015 3

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 2015 'abcbcbc' 4

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 2015 'abcbcbc' 5

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 compsci 101, spring 2015 6

Function – return or print? • Example function that returns a value def sum(a,

Function – return or print? • 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) hw("Sue") compsci 101, spring 2015 7

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 2015 8

Function Old. Mac. Pig() compsci 101, spring 2015 9

Function Old. Mac. Pig() compsci 101, spring 2015 9

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 2015 10

Discuss how to make code better bit. ly/101 S 15 -0120 -01 • Describe

Discuss how to make code better bit. ly/101 S 15 -0120 -01 • 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 2015 11

Old. Mac 3 bit. ly/101 S 15 -0120 -02 • Describe what makes this

Old. Mac 3 bit. ly/101 S 15 -0120 -02 • Describe what makes this version of Old. Mac. Donald better than the first version compsci 101, spring 2015 12

Old. Mac 3 - calls, and input passed main pig, Oink Old. Mac cow,

Old. Mac 3 - calls, and input passed main pig, Oink Old. Mac cow, Moo Refrain Say. EIEIO pig Had. Animal Oink With. Sound Old. Mac (rest not shown, similar to pic diagram) compsci 101, spring 2015 13