CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring 20151
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 20152
Python – Programming Concepts Names vs abstractions –What is –What is Types are important –What is foo.pdf, foo.mp4, 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 20153
Strings Sequence of characters in quotes "I" + 'Love' + '''Python''' "I" 'Love' '''Python''' 'ILovePython' String operators: concatenation (+), repeat(*) Precedence? "a" + "b" + "c" * 3 'abccc' Precedence? "a" + "b" "c" * 3 'abcbcbc' 4compsci 101, spring 2015
Strings Sequence of characters in quotes (same result) "I" + 'Love' + '''Python''' "I" 'Love' '''Python''' 'ILovePython' String operators: concatenation (+), repeat(*) Precedence? "a" + "b" + "c" * 3 'abccc' Precedence? "a" + "b" "c" * 3 'abcbcbc' 5compsci 101, spring 2015
Function def functionName(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 6compsci 101, spring 2015
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") 7compsci 101, spring 2015
Old MacDonald Song Write a Program to print this song compsci 101, spring 20158
Function OldMacPig() compsci 101, spring 20159
Rest of Code Function OldMacCow –Replace “pig” with “cow” –Replace “Oink” with “Moo” Code to start: compsci 101, spring
Discuss how to make code better bit.ly/101S 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
OldMac3 bit.ly/101S Describe what makes this version of OldMacDonald better than the first version compsci 101, spring
OldMac3 - calls, and input passed pig OldMac main WithSound HadAnimal Refrain SayEIEIO OldMac Oink pig, Oink cow, Moo (rest not shown, similar to pic diagram) compsci 101, spring