cisc101 reminders notes today
play

CISC101 Reminders & Notes Today Test 1 next week in your - PowerPoint PPT Presentation

CISC101 Reminders & Notes Today Test 1 next week in your tutorial Introduction to Informal review today Console I/O Functions Are you not in the lab/tutorial section(s) you want? Variable scope Contact


  1. CISC101 Reminders & Notes Today • Test 1 next week in your tutorial • Introduction to … – Informal review today – Console I/O – Functions • Are you not in the lab/tutorial section(s) you want? – Variable scope – Contact Irene LaFleche ( irene@cs.queensu.ca ) – Style • Assignment 1 due on Sunday by midnight • We’ll see more on some these topics later – Updates to description – Cover the basics for now – One change to requirements – Make sure your submission meets the requirements! • Read the description carefully Winter 2011 CISC101 - Whittaker 1 Winter 2011 CISC101 - Whittaker 2 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod The print(…) BIF Console (or Screen) I/O • Where “I/O” stands for “Input/Output” print ( string1 , string2 , ..., sep = sepString , end = endString ) • Output: use the print(…) BIF – Does not return anything • Prints the string arguments given • Input: use the input(…) BIF • Separates the arguments with sepString • Separates the arguments with sepString – Returns a string – Returns a string – sep is a space unless otherwise specified • Ends the printed sequence with endString • Format: use the format(…) string function – Returns a string – end is \n unless otherwise specified Winter 2011 CISC101 - Whittaker 3 Winter 2011 CISC101 - Whittaker 4 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  2. The input(…) BIF The format(…) String Function input ( promptString ) aString . format ( arg1 , arg2 , ...) • Displays promptString on the screen • Copies aString and inserts the given values • aString uses replacement fields to represent • Awaits keyboard input by the user the arguments the arguments – User presses Enter to end input • Returns the entered data as a string – Each field is replaced by the specified argument – Fields also specify the format of the argument – Must convert to another data type if necessary – Fields are always surrounded by {} "What is {0} + {1}?".format(2, 5.0) ‘What is 2 + 5.0?’ Winter 2011 CISC101 - Whittaker 5 Winter 2011 CISC101 - Whittaker 6 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Replacement Fields Format Specifications • Replacement fields can be complicated • Format specifications can be very complicated optional – We won't go into all of the options – We won't go into all of the options field_name : format_spec 0 width . precision type • field_name s specify arguments in two ways • field_name s specify arguments in two ways • All of the above are optional • All of the above are optional – Via indexes (starting from 0) – You can use one, some or all of them • 0 : displays leading zeroes for numbers • "My name is {0}, {1} {0}".format("Bond", "James") → 'My name is Bond, James Bond' • width : sets the minimum space occupied by the – Via identifiers that you name and assign values to data • "{numer}/{denom}".format(numer=2, denom=4) → '2/4' Winter 2011 CISC101 - Whittaker 7 Winter 2011 CISC101 - Whittaker 8 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  3. Format Specifications – Cont. What is a Function? • precision : number of digits after decimal point • A “group of statements” that accomplish a task – Specify type f for floating point – Perhaps composed of several smaller tasks … • A function contains code that is isolated • type : dictates how the data is presented – Interacts with other code through a designed interface – f specifies a float • The interface consists of … – b , o and x convert arguments to binary, octal and hex – parameters for values that go into a function – parameters for values that go into a function – s for string is optional – return value(s) that comes out • Some combinations cause errors • You can have as many parameters as you want – e.g. , you can’t give a precision for “Hello” – Including none • You can return nothing or a single thing – Or more than one thing (as we’ll see later) Winter 2011 CISC101 - Whittaker 9 Winter 2011 CISC101 - Whittaker 10 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Invoking Functions Invoking Functions - Cont. • Name the function and then use round brackets • Arguments are separated by commas • Brackets contain zero or more arguments • Arguments can be – Values for the function’s parameters – Literal values • “Parameter” and “argument” are often used interchangeably – Variables • For example: – Expressions – print() • Variables and expressions are evaluated first • Displays a linefeed on the console – Determine the resulting value before invoking – print(“Hello”) • Displays the string Hello on the console – Feed it into the function – print(“Hello”, “Alan”) • D isplays Hello and then Alan separated by a space Winter 2011 CISC101 - Whittaker 11 Winter 2011 CISC101 - Whittaker 12 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  4. The main() Function Writing Functions • Function “header” syntax: • Define and call a main() function to run your program def function_name ( parameter_list ) : – Convention in Python and many other languages • Named main() for “mainline logic” • Use the normal variable naming rules for function_name • Use main() to call and coordinate other functions • parameter_list provides a mechanism for • parameter_list provides a mechanism for – Pass data back and forth between them – Pass data back and forth between them getting values into your function • Make sure to invoke main() to start your – But it’s optional program! • The return keyword can be used to send a value out of a function – More on this in a bit … Winter 2011 CISC101 - Whittaker 13 Winter 2011 CISC101 - Whittaker 14 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod A Function with Parameters A Function with Parameters - Cont. • When you invoke this (useless) function, you Here is a (useless) function that displays the sum of need to supply two things for the parameters two numbers: – You supply two numbers as arguments def addNumbers(num1, num2) : addNumbers(3.4, 6.7) sum = num1 + num2 sum = num1 + num2 • The code in addNumbers() runs and the sum • The code in addNumbers() runs and the sum print(“The sum is”, sum) displayed • Within addNumbers() – num1 has the value 3.4 – num2 has the value 6.7 Winter 2011 CISC101 - Whittaker 15 Winter 2011 CISC101 - Whittaker 16 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  5. A Function with Parameters - Cont. Function Returns • A function may return something • To put it another way … – The “something” can be any Python type • The positional arguments 3.4 and 6.7 have • A str , an int , a float , etc. been mapped into the parameters num1 and • Functions that don’t return anything are sometimes called procedures num2 – Like print() , for example – Like print() , for example • num1 and num2 are variables that have been • Can you think of some functions that return created in the function’s parameter list and are something? local to the function – input() – float() – str() – … Winter 2011 CISC101 - Whittaker 17 Winter 2011 CISC101 - Whittaker 18 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod A Function with a Return Value Returning Values • If you don’t have a return statement, then your • How can addNumbers() be changed to return function does not return anything the sum instead of printing it out? – It is invoked without expecting any value to come out of – It is usually regarded as “tacky” to have functions print the function things instead of returning them • No assignment required when invoking – Let main() do the printing! • Execution of a function stops as soon as you • … except in Assignment 1 • … except in Assignment 1 execute the return statement execute the return statement def addNumbers(num1, num2) : sum = num1 + num2 return sum Winter 2011 CISC101 - Whittaker 19 Winter 2011 CISC101 - Whittaker 20 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

Recommend


More recommend