DClark

Untitled

May 24th, 2019
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. Chapter 3 notes:
  2. +Functions
  3. >Functions are like mini programs, and you can make your own.
  4. >>Create one by DEF [FUNCTION NAME]() then indent under it.
  5. >Copying straight code to use elsewhere, then any changes you make will have to be repeated for each copy.
  6. >>The custom function feature allows you to change the code at one part and have it be reflected everywhere, since the function is called instead of static code.
  7. >Deduplication is the term for consolidating code to make it look nicer. In other words, getting rid of duplicated/copy-pasted code.
  8. >A parameter is a variable that a function calls. But this means that the parameter is bound to the call, not the function. So if you try to reuse the same parameter, you’re gonna have a bad time.
  9. >>Exa: If you call Alice and Bob for the name variable, you'll get an error if you try again, since name isn’t bound to anything.
  10. >Return Value is the value that a function call evaluates to.
  11. >RETURN statement is useful for DEF because it’ll give a static answer. See Magic8Ball.py for more.
  12. +None value
  13. >Represents absence of a value(duh)
  14. >Only value of the NoneType data type.
  15. >>Other languages call this things like null, nil, undefined
  16. >Must be type with a capital N
  17. >Print() basically always contains the None value, because it doesn't need to return anything. Since Print(), like all functions, needs to return a value, the value-that-isn't-a-value None works just fine.
  18. >>Also why WHILE and FOR loops implicitly end with CONTINUE statements.
  19. >>RETURN statement without a value gives NONE
  20. +Keyword Arguments and Print()
  21. >Position is Paramount in function calls. Random.randint(1, 10) will pick a number between 1-10, but random.randint(10, 1) gives an error.
  22. >So what about keyword arguments?
  23. >>Defined by keyword before then in function calls.
  24. >>Mostly used for optional stuff
  25. >>>Exa: print() can have END or SEP
  26. >>>>END for end of argument, SEP for between arguments to separate them.
  27. >>>>>END makes the space after a line go away. So two lines of print() would have them on two lines when running, but having end='’ be the end of the first print contents, the second line is immediately placed there. Hello World on two lines is then HelloWorld.
  28. >>>>Normally you have spaces between each argument of print()…
  29. >>>>>Exa: Print('cats', 'dogs') would be “cats dogs”
  30. >>>>But if you put SEP after the end…
  31. >>>>>Print('cats', 'dogs', sep=',') you get “cats,dogs”
  32. #Would print('cats', 'dogs', sep=', ') give “cats, dogs”? Must check at home.
  33. ##Yep.
  34. +Local and Global Scope
  35. >Parameters/variables assigned within a function are LOCAL SCOPES. Assigned outside of a function? GLOBAL SCOPE. LOCAL VARIABLE and GLOBAL VARIABLE, same. Variable can’t be both at same time.
  36. >>But there can be, say, two things assigned to the Spam variable; as long as they’re global and local varients.
  37. >SCOPE is a container for variables. When it’s destroyed, the values are forgotten. This also applies to when the program shuts down.
  38. #Does this explain why None==spam is true up there?
  39. >Whenever a function is called, a global scope is created, so any variables made only exist in the function.
  40. >Properties of scope:
  41. >>Code in global(not a function) cannot use local(a function's scope
  42. >>Opposite is true for local scope; can access global variables
  43. >>Local scopes can’t share variables with each other
  44. >>Same name for variables in different scopes, see above.
  45. >Python does just have everything be a global variables so when code modifies a variable it only affects that instance; the only interaction with the larger program as a whole is the parameters/output. Makes troubleshooting a lot easier when it’s just a few lines of code giving difficulties instead of trying to parse what global variable is causing the trouble.
  46. >>For this reason, generally a bad idea to rely on global variables if you have a large program.
  47. +Local scopes can't use variables in other local scopes
  48. >Refer to spam99.py for this bit
  49. >>Defines things then runs the last bit of code. However, eggs still ultimately prints as 99 instead of 0 because the eggs variable in the def spam() is separate from the one in def bacon(). And although bacon() is called after eggs is set to 99 in spam(), spam()'s egg variable is still the same as it was, since Bacon's Egg variable was destroyed when it finished, just like how spam()'s egg variable is destroyed when that's finished.
  50. #However, putting print(eggs) in an extra line under where it was defined as 0 ultimately outputs 0, then 99 on a new line.
  51. +Global variables used by local scopes
  52. >Example sets up a global variable without defining a local one, so program automatically uses the global variable
  53. +Conflicting global and local variable names and how to sort them
  54. >Book recommends avoiding this if possible…but if you really want to…
  55. >Refer to sameName.py for this bit
  56. >>Three variables all named eggs
  57. >>>When spam () is called it creates eggs locally
  58. >>>Bacon() makes another local one
  59. >>>Egg='global' on it’s own makes a global one
  60. #This program kind of works in reverse. Things are first defined, but not called. The executor of this program is the final lines of code, which calls bacon()(the previous block of code above), which in turn calls spam()(the first lines of code).
  61. ##Is all code like this, defining things and then executing them after? These notes, for example, are the reverse, where things are “executed” and then defined.
  62. #What I don’t get is why def bacon() is indented and def spam() isn’t. Does that even matter? Check when at home.
  63. +The Global Statement
  64. >You can modify global variables with a global statement. Putting the global statement(exa: global eggs) makes the same variable indicate it’s the global one, so changing ‘eggs’ would change the global ‘eggs’ instead of making a local ‘eggs'.
  65. >>See sameName2.py
  66. >Ways to tell if a variable is local or global:
  67. >>If it’s being used globally(outside functions) then global, a GLOBAL statement for that variable is present then global, variable is used in assignment statement in a function then local, and if the variable isn’t used in an assignment statement, then global.
  68. >>>See sameName3.py for more info.
  69. >local and global variables named the same are mutually exclusive within functions; you can’t use a local one and then a global one within the same function, and vice versa.
  70. >See sameName4.py for this bit
  71. >>Uhhhhhh why doesn’t it pull the global variable for eggs here and instead gives an error? I thought that isn’t what’s supposed to happen, as per above.
  72. >>>Seems to be because Python recognizes that there’s a call for a local ‘eggs’ but it doesn’t have local eggs set to anything yet. So it tries to pull the local variable and gets an error. Swapping the eggs assignment and print fixes this issue, but, as expected, pulls the local variable and not the global one.
  73. >”Black box” tangent
  74. >>All you really need to know about functions are the inputs and the outputs. Apparently this is how most programming is done. And, judging by how the random import was done earlier…yeah. In addition, the self-sufficient nature of local variables almost ensures that the function won’t even mess up globals.
  75. >Error/exception handling
  76. >>Normally if you get an error(otherwise known as an exception), your program quits out. To avoid that…
  77. >>See zeroDivide.py for more info.
  78. >>>ZeroDivisionError is…exactly that.
  79. >>>You can prevent errors from crashing your program with a TRY clause, paired with an EXCEPT clause. TRY is for potential trouble code, and if an error happens, it skips to the EXCEPT clause to see what to do.
  80. >>>You can put TRY/EXCEPT clauses anywhere, where it’ll be caught within a function or without, with all the attributes that entails. Of note here is that when used within a function, the function can still be used later if it crashed, while outside of a function, like the print() example, it will move on to the next line after the EXCEPT clause, skipping everything else. Presumably it does the same with the function too, but it doesn’t really matter since the function is repeated every line.
  81. +Guessing Game Program
  82. >Ahhhh so many lines! ;_;
  83. >See guessTheNumber.py for more info
  84. >Program is pretty self explanatory and doesn’t show anything new, just synthesizes it all.
  85. >>You definitely couldn’t have made this on your own without way too many iterations.
  86. >>This does show, however, that ranges can be used to count user input. Here, it limits guesses to 6 times to guess the right number, and does a double BREAK(instead of BREAK to IF) if the number isn’t guessed in 6 times.
  87. >Interesting note here that friend brought up: FOR loops are for loops that you know have a limited number of loops, while WHILE loops are for an unknown quantity(ongoing, I guess?)
  88. End chapter 3 notes
Advertisement
Add Comment
Please, Sign In to add comment