Unit Two - Intermediate Python



Part One - Functions & Parameters
  • function (also known as a method) is a reusable section of code written to perform a specific task in a program. 
  • A method can take input in the form of parameters and output by returning data to whoever called it.
  • A method header looks like this:
    • Ex: def apple(num):
    • Apple is the name of the method
    • num is a parameter (input) for the method
  • The body of a method is the code it contains
  • Calling a method tells the program to find a method by the specified name and execute the code inside of it.
  • There are two ways to pass a variable in computer science, depending on the language you use.
    • Pass By Value (used in python by default, used in Java sometimes)
      • Provides the method with a copy of the data.  As a result, when you modify the variable in the method, the original is NOT changed.
    • Pass By Reference (used in Java sometimes)
      • Provides the method with a reference to the memory address.  As a result, when you modify the variable in the method, the original in also changed.
  • A method that has a variable number of parameters in python uses a feature called splat arguments.
  • To gain access to new special functions, you can import a module to Python
  • Some more useful functions:
    • max() - returns the largest number from the set of parameters
    • min() - returns the smallest number from the set of parameters
    • abs() - returns a positive version of the parameter
    • type() - returns the data type of the variable
Part Two - Lists
  • Lists are a data type you can use to store a collection of different pieces of information as a sequence under a single variable name
  • You can access an individual item on the list by its index. An index is like an address that identifies the item's place in the list
  • List indices begin with 0, not 1! You access the first item in a list like this:list_name[0]. The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero.
  • In Python, we say lists are mutable: that is, they can be changed.
  • You can use the append() method to add things to an existing list
  • You can use len() to get the length of a list, just like you can with a string
  • If you only want a small part of a list, that portion can be accessed using a special notation in the index brackets.list_name[a:b] will return a portion of list_name starting with the indexa and ending before the index b.
  • This slicing method works with strings, too!
  • You can search through a list with the index()  function.my_list.index("dog") will return the first index that contains the string"dog". An error will occur if there is no such item.
  • Items can be added to the middle of a list (instead of to the end) with theinsert() function.my_list.insert(4,"cat") adds the item "cat" at index 4 of my_list, and moves the item previously at index 4 and all items following it to the next index (that is, they all get bumped towards the end by one).
  • You can use a for loop to go through each element in the list
  • f your list is a jumbled mess, you may need to sort() it. my_list.sort()will sort the items in my_list in increasing numerical/alphabetical order.
  • A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. Dictionaries are enclosed in curly braces.
Part Three - Loops
  • The while loop is similar to an if statement: it executes the code inside of it if some condition is true. The difference is that thewhile loop will continue to execute as long as the condition is true. 
  • An infinite loop is a loop that never exits.
  • The break is a one-line statement that means "exit the current loop." An alternate way to make our counting loop exit and stop executing is with the break statement.
  • Using a for loop, you can print out each individual character in a string or each element in a list. 


No comments:

Post a Comment