Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Intro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Adam MeyersNew York University Introduction to: Computers & Programming Defining Identifiers: Objects with NamesIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Outline• The types of objects with names– Functions, Variables, Programs, Modules, etc.•Defining functions• Defining variables• The Scope of Variables• Introducing For Loops•SummaryIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Identifiers (Things with Names)• Variables – pointers to objects (values of variables). When a variable is assigned a new value, it points to a new object.•Functions– mappings from input to output– blocks of code that perform actions– or both•Library Files (Python Modules)– Functions and variables in a (.py) file– Kept in special library folders• MAC OS – /Library/Python/3.1/site-packages/• Windows – c:\python31\lib\• Programs (Python scripts) –– (.py) files like modules, but have 1 main function– /Applications/Python\ 3.1/Extras/Demo/turtle/tdemo_planet_and_moon.pyIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Limits on Function/Variable Names • Cannot be identical to Python keywords–For a list of keywords type the following in IDLEimport keyword keyword.kwlist• Must begin with a letter (a, b, A, B) or underscore (_)•Other characters can be letters, numbers or _• Illegal names: –5ways_to_do_it (First character is not a letter)–the\ big\ bad\ wolf (Spaces and slashes are not allowed)–import (Python keywords cannot be redefined)Intro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Good Practices for Choosing Names• Choose names that clearly represent the role of the function, variable, etc. in the code•Use underscores _ instead of spaces in long names•OR use the camelCase convention, in which uppercase letters signify the beginning of new wordsIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001An Example from Physics• def get_distance_traveled (speed, time): print('Calculating the distance traveled given speed of ', speed, 'and time of ', time) return (speed * time) •get_distance_traveled is a function–Models a mathematical function from the pair (speed, time) to the distance traveled if an object travels at that speed for that amount of time.–Prints what the function is modeling as a side effect–Parameters speed and time model speed (e.g., in km per hour) and time (e.g., in hours)Intro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Dissection of get_distance_traveled• Line 1: def get_distance_traveled (speed, time):– “def” begins a function definition–Next, is the name of the function (get_distance_traveled)–Then are zero or more variables •Surrounded by parentheses and separated by commas– Ends in a colon (which always precedes a block of statements)– The indent of the rest of the function indicates it forms a block•All the statements within a block are indented the same•Line 2: a print statement (a side effect) – executes 1st •Line 3: return(speed * time) – executes 2nd –return 'returns' its value from a block•In this case, that means it determines the value of the functionIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Why is get_distance_traveled easy to read and to use?•The names of the function and the variables– Are based on the role that they play– Are spelled out and not abbreviated•The print statement tells the user what is going on•It is a very simple function•Logically equivalent, but hard to understand– def gds (s, t): return (s* t)– Other people looking at the code would not understand what it is supposed to mean– The author would not understand it a month after writing itIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Not all (Python) Functions have Input or Output• This function has neither input nor outputdef why_D_Cheney_should_be_in_jail():print('He used a firearm while intoxicated, a felony')• This function has input, but no outputdef print_something_3_times(that_thing): print(that_thing, that_thing, that_thing)•This function has output, but no inputdef the_secret_of_life() return(42)Intro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Choosing a Good Function Name• It should be legal: –Begin, with a letter or _–Contain only letter, numbers and _–Not match a Python Keyword• The name should not already be used for a different function–You may unintentionally redefine a function or it just may be confusing•The name should be self-explanatory–One reading the code should understand its purposeIntro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Variables•A variable points to another object (its value)•The object pointed to changes if a variable is assigned a new value•These objects typically include integers, floats, strings, among other types (the variable has the same type as its value)•Variables have scope, which determines when they are active•Parameters of a functions are variables whose scope is the function (they are only active while the function is active)Intro to: Computers & Programming Defining Identifiers: Objects with Names V22.0002-001Some Properties of Variables in Other Languages• Some languages require variable to have permanent types–This improves efficiency and may make debugging easier– Is a little difficult to get used to•Some languages allow variables to point to functions– For example, in LISP it is possible to write a function that takes a function as one of its arguments and applies that function to another one of its arguments•Some


View Full Document

NYU CSCI-UA 0002 - Defining Identifiers

Download Defining Identifiers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Defining Identifiers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Defining Identifiers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?