Posts

The map() function: BEHIND THE SCENES!

Image
Hi! First of all, congratulations! You are on Week 3 of the course, and on your way to a wonderful future in CS!. Welcome to this section. We will discuss  the powerful function map()  and how it can help us during our coding endeavors. Let’s get started! Step by Step Example Here is an example broken down step by step of  how the function map work behind the scenes NOTE:  you won’t see them actually being executed when you call map, but you will see the final result, the list or iterable with the resulting values of applying the function to each element in the list provided as argument. Hope it helps! If you have any questions, please post them in the forums. Your classmates and Community TAs will be there to help you! :) Estefania.

Iterating over Lists and Tuples: BEHIND THE SCENES!

Image
Hi! Welcome to this section. This time, we will discuss iterables. During your problem sets you will discover the power of iterables and how they can completely change you software development thinking process. Formally, an iterable is "an object capable of returning its members one at a time . Some data types we have studied are iterables, as you can see in the slide below. Strings and lists and tuples are iterables. And later on you will study Dictionaries (you can iterate over their keys and their corresponding values). You can find more on iterables in Python's documentation:   https://docs.python.org/3.6/glossary.html But this concept seems so abstract, right? “Capable of returning its members one at a time” What does this mean? Iterables’ magic comes to life in for loops . Why? Because they return their elements one at a time on every iteration. This is better illustrated with an example to describe the required syntax: Iterables in Action!...

Return vs. Print: BEHIND THE SCENES!

Image
Return vs Print Hi! Welcome to this section. We will talk about the  difference between Returning and Printing a value inside a function . We will discuss what happens when we don’t explicitly return anything from a function and how to assign the value returned to a variable. Let get started! In the diagram you can see some essential characteristics of return and print (be sure to use lowercase letters for these keywords, otherwise Python will throw an error) Let's talk about  return : This is basically how the return statement works: Here you can see a diagram of the being picture of how return works and how you can assign the returned value to a variable. Let's talk about  print Now… let’s talk about “print”. Surely you’ve used this statement before for debugging. But what happens if we only use a print statement and not a return statement? Let’s see what happens! In this case, when a print statement is inside a funct...

Functions and Parameters: BEHIND THE SCENES!

Image
FUNCTIONS AND PARAMETERS Hi! Welcome to this section. We will discuss  FUNCTIONS AND PARAMETERS . This topic is vital for your programming future and you will use them very frequently in problem sets! :-) Let’s get started! You can think of functions as  “compartments”  inside your code that can interact with the world that surrounds them, but that have special properties of their own, like their own scope (they can have their own set of variables that can’t be used outside the body of the function) Accessing docstrings In the lecture video you can see that you can also add something called a  docstring  (the green text in ''' '''' at the top of the function body). You can access them by using: >  functionName.__doc__ They are extremely helpful for maintaining your code. They help you remember the type of your parameters, the logic used inside the function and the value returned by the function. Parameters ...

Bisection Search: BEHIND THE SCENES!

Image
BISECTION SEARCH Hi! Welcome to this section on  Bisection search  . This is an amazing algorithm (I loved it when I first saw it in the course), it can find a value much faster than a simple approach. In these diagrams you can experience how this algorithm works, and how each step is determined by an "error" or epsilon. I've used the same code from the lecture, but in this case,  we find the square root of 9 with an epsilon 0.08 , and I've added some print statement to help us see what's going on on each step. With a low starting point from 0.0 and a high point of 9, we start our algorithm.... This is what we see on the shell: Python's Shell: First, the algorithm asks if the answer found (the middle point of the search space) is close enough to the answer we are looking for (if the answer squared is within epsilon of the value for which we are calculating the square root)  below you will find a visual explanation of this process (Chec...

Control Flow: For Loops, While loops and range() BEHIND THE SCENES!

Image
Welcome to this section on Control Flow! Control flow  is an essential element of programming.  You can build very powerful programs with loops , so let's see how they work behind the scenes, and  how range() works , because you will use it in this course and in your future programming projects. So let's begin! Let's start with WHILE LOOPS: In While loops a block of code will be executed for as long as a condition is met. Every time the block of code is executed, the condition will be checked again, and if it evaluates to True, the code inside the loop runs again, otherwise it exits the loop and continues executing the code below it. When learning loops, a good strategy is to analyze each step and how values change when the code is executed. In this case, we determine if the code will be executed, the values for the variables involved each time the loop runs and what will be printed on each iteration. The value of n is updated every time the loop...

Booleans are integers?!

Image
Hi, Welcome to this short section. We will discuss Booleans and the fact that the fact that they can be used as integers in your code. This is a curious fact and I thought it would be interesting to share it with you! J Let’s get started! You can find the article on Python Documentation under numbers.Integral : https://docs.python.org/release/3.0.1/reference/datamodel.html#the-standard-type-hierarchy Here are some demonstrations in the Shell of how we can compare and operate on True and False as if they were integers 1 and 0 respectively. Hope it helps! If you have any questions, please ask them in the forums, your classmates and Community TAs will always be there to help you! Estefania.