@@ -325,8 +325,6 @@ def name (x, y):
325325for item in range (6 ): # range allows you to loop as much as you want
326326 print '*' ,
327327
328- # Comments
329-
330328def print_multi (x ):
331329
332330 print locals ()
@@ -347,3 +345,126 @@ def print_multi(x):
347345def distance (cor1 , cor2 ):
348346 print locals ()
349347 return math .sqrt ( (cor1 [0 ]- cor2 [0 ])** 2 + (cor1 [0 ]- cor2 [1 ])** 2 )
348+
349+
350+ #DOC STRINGS
351+ """
352+ if a string literal is the first thing in the fuction block following the
353+ header, then it's a doc string
354+ """
355+ # example:
356+
357+ def complex_function (arg1 , arg2 , kwarg1 = u'bannana' ):
358+ """Return a value resulting from a complex calculation."""
359+ # code block here
360+
361+ """
362+ doc strings can be read in the interpreter as the "__doc__" attribute of the
363+ function object
364+
365+ a doc string should be:
366+ -a complete sentence in the form of a command describing the function
367+ -e.g., "Return a list of values based on blah blah..."
368+ -fit into a signle line
369+ -if more lines are needed, make the first line a complete sentence,
370+ then add more information later
371+ -be enclosed with triple quotes
372+ """
373+
374+
375+ #RECURSION
376+ """
377+ recursion: if a function calls itself
378+ -like other functions, a call within a call establishes a call stack
379+ """
380+
381+ #BOOLEAN EXPRESSIONS
382+ """
383+ "bool(something)" returns either true or false
384+
385+ -and, or, and not
386+ -"and": returns the first operand that evaluates to False, or the last operand
387+ if non are True
388+ -"or": returns the first operand that evaluates to True, or the last operand
389+ if none are True
390+ -"not" inverts the boolean value of its operand
391+
392+ -boolean types are subclasses of integer
393+ """
394+
395+ # this is common in programming
396+ if something :
397+ x = a_value
398+ else :
399+ x = b_value
400+
401+ # in python
402+ y = 5 if x > 2 else 3 # clased a "ternary" expression
403+
404+
405+ #CODE STRUCTURE, MODULES, AND NAMESPACES
406+
407+ """
408+ -you can put a one-liner after the colon; however this should only be done if
409+ it makes your code more readable
410+ """
411+ #example
412+ x = 12
413+ if x > 4 : print x
414+
415+ """
416+ -namespaces: these are the dots
417+ -e.g., "name.another_name"
418+ -the "." indicates that you're looking for a name in the "namespace" of a
419+ given object
420+ -the namespace of an object could be
421+ -name in the module
422+ -module in a package
423+ -attribute of an object
424+ -method of an object
425+ -a "module" is simply a namespace
426+ -it could be a single file or a collection of files
427+ -you can think of files with ".py" as modules
428+ -a "package" is a module with other modules in it
429+ -on a filesystem, this is a directory that contains one or more .py files,
430+ one of which must be called "__init__.py"
431+ -see the example below for how to import packages and modules inside
432+ """
433+ #example of importing packages and modules in packages
434+ import modulename
435+ from modulename import this , that
436+ import modulename as a_new_name
437+ from modulename import this as that
438+
439+ """
440+ -importing modules complies the python code to "bytecode"
441+ -this creates a module.pyc file
442+ -this process executes all code at the module scope
443+ -this is why it's important to avoid module-scope statments because they have
444+ global side-effects
445+ -code in a module is not rerun when you import again
446+ -you can also run modules
447+ """
448+
449+ #ways to run a python module
450+ python hello .py # must be in current working directory
451+ ./ hello .py #!/usr/env/python at top of module (Unix).
452+ """
453+ The "./" is an abbreviated way to inform the shell that the absolute path of that
454+ file is the current directory (i.e., the directory in which the user is currently
455+ working). This allows you to execute a script.
456+ """
457+
458+ """
459+ -when you import a module, the value of the symbol "__name__" is the same as the
460+ filename.
461+ -when you run a module, the value of the symbol "__name__" is "__main__"
462+ -this allows you to create blocks of code that are executed only when you
463+ run a module
464+ """
465+
466+ #this code will only execute if you run the module, not import it
467+ if __name__ == '__main__' :
468+ # Do something interesting here
469+ # It will only happen when the module is run
470+
0 commit comments