Avatar

@sribananas / sribananas.tumblr.com

Writer. Tech Enthusiast. Activist.
Avatar

After a long hiatus - I am back to blogging! Here’s a video of me speaking at QCon about one of the projects I’ve been working on during my time away from writing. 

I’ve been trying to think of ways to merge my interest in public speaking, with my experience in technology and my desire to serve the community. In this talk, I highlight how my project allowed me to do just that!

Avatar

From my summer working as an iOS Fellow at BuzzFeed!

Avatar

From crawling to walking! - Lesson 3

Alright guys, in the last tutorial I showed you how to write your first function. By now you know what arguments are, how to define functions, and how to compile and run a function. If you need a quick refresher, check out Lesson 2. 

In this tutorial, we're going to take it one step further and learn about while loops, and Python lists to write a bit more complex function.

To start off, what are while loops?

While loops, in most programming languages, are control statements that allow you to repeat a block of code as long as the while statement is true.

Okay, you get that, but what does it actually look like?

#here is an example of a while loop (in Python, we use hashtags (#) to denote comments. Comments are lines of text that the Python interpreter knows to ignore because they start with a hashtag. Always use comments in your code because it helps other people know what in the world you're doing, and it helps you to remember your work as well!)

toprint = "Hello, world!"

while toprint == "Hello, world!":

     print (toprint)

So basically, what this chunk of code in bold is doing is printing out toprint, which is a variable that stores the string "Hello, world!" as long as toprint is actually storing "Hello, world!". Now, if we were to have made toprint store another string, like just "Hello" then our while loop would have ignored the print statement since toprint wouldn't be equal to "Hello, world!". Get it?

Why don't we try something a bit more complicated?

          Let's set out goal for this lesson to be to write a function that takes as input (arguments), a list of numbers, and returns the average of all the numbers in that list.

You're probably thinking, "Wait a minute, Sri never told us squat about lists!!" You're right. I didn't. But, we're gonna learn about them, now!

As the name might've suggested, Python lists are what they seem...lists! They look like this:

[1, 2, 3, 4, 5, 6, 7]

Lists are denoted by the square brackets and values are comma-separated. Just like in the "real world" Python lists can hold just about anything: numbers, letters, words, sentences, other lists!

So, now that we know what lists are, let's learn a bit more about how to use them. Let's say we have a list of numbers from 1 to 10. Just like any other value (such as our "Hello, world!" string, or our numbers in the last function) we're gonna have to assign our list to a variable. In other words, we have to name our list so we can use it! To keep things simple, let's call our list "mylist."

mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note that the values in our list are integers and not string characters, this is because we know that our function wants to do mathematical things with the numbers. For that we need integer values, since adding strings does something completely different that we'll cover in the next lesson!

Now, how do we access an element in our list? Well, a weird thing about computer scientists is that we start counting at 0. So everything in mylist is numbered from 0 to 9 instead of 1 to 10 like you're used to.

So, to access the first element of the list, we type:

mylist[0]

This will return 1, as you can see in the screenshot below. Why don't you test out how to access the other values in the shell?

Done testing? Okay. Another thing to know about lists is that they are mutable. This means that lists can be modified in certain ways that some other data sequences, like strings, can't be.

Because lists are mutable, we can do things to them like this:

mylist[0] = 99

Now type mylist in your shell and hit enter. The shell should reply to you like this:

[99, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Crazy, huh? But not really. You did tell mylist that its first element should now be 99 when you set mylist[0] to 99. So, you know how mylist is a variable for the entire list. Think of mylist[0] as a variable for the first element in the list, mylist[1] as a variable for the second element of the list, and so on. If you were to set another variable equal to a number, for instance like this:

i = 1

Then you could also access the second element of mylist, or the element of mylist at the index (which is basically a fancy way to say place/placeholder) 1 in the list.

So, mylist[i] would return 2 since that's the element at index 1 in mylist.

I know this was a lot to process, so take a second to think about it and to test out how list indices and accessing elements in lists works in the shell. You can make another list with different elements if you'd like, too!

For more info on lists, and other examples check out this guide.

Let's continue.

Now that we know how to write a while loop, and how to use lists, let's go ahead and work on our function. As we learned in the last lesson, it's always good to plan out how your function should work using pseudocode. Let's write some up right now!

Pseudocode:

1. Define the function with one argument (alist).

2. Check the length of the list (don't worry, we'll talk about how to do that when we start writing the actual function!).

3. Write a while loop that goes through the elements of the list.

4. Get each element from the list and add it to the sum of the previous elements (which we'll store in a variable called sumsofar).

5. After you've gone through all the elements, divide the sum of all the elements by the number of elements in the list.

6. Return the average!

So, let's define our function. We're going to call it, averageList. Now, averageList should take one argument (alist) so our function should look a little something like this:

def averageList(alist):

Great. Now, let's check the length of our list. But how do we do that? Well, lucky for us, Python has a build in library of functions that we can just use as we like! One of those functions is len(list), which checks the length of a list and returns the integer value.

Let's put what we have so far together:

def averageList(alist):

       length = len(alist) #we are adding a comment here to explain what is happening in this line. We're getting the length of the list we passed in as input and assigning it to a variable to use later.

Next, we want to write a while loop to go through every element of the list. So, we learned that computer scientists start counting at 0 right? So, let's set a variable equal to 0 that we can change as we like as we go through elements of the list. We want to set it to 0 since we need to access the first element of the list, which is at index 0 (see above).

What we're going to do with our variable (x) is add 1 to it every time we look at an element in the list and add it to our sum. This ensures that we don't go back to the indices we looked at already. And we keep adding 1 to x as long as there are more elements in the list, or in other words, as long as x is less than the length of the list. But do we have to use the len function again to get the length of the list? Nope. Remember, we saved the length in a variable called length, so that's what we'll use.

So, our while loop should look a little like this:

x = 0

while x < length:

       #do something

        x = x + 1

Time to put our code together again:

def averageList(alist):

    length = len(alist)

    x = 0

    while x < length:  #while x < the length of our list (it's just algebra mixed                                     with words!)

              #do something

               x = x + 1

Okay, so now what? Well, we have to figure out what exactly it is we do where we have the comment "do something." Let's check our pseudocode.

We need to access each element in the list using alist[ ] ... but how do we get EVERY element? We use x! That's why we set it to 0. So, the first time around in our loop, x = 0. We call alist[x] and we get the first element in the list. Then, we add 1 to x so the next time we're in the loop, x = 1. When we call alist[x], we'll get the second element in the list and so on!

Now, what do we do with these values that we get? We create a variable called sumsofar (check psuedocode) and add the value to sumsofar like this:

sumsofar = 0 #since we don't have a sum yet

x = 0 #since we start at the first index

while x < length:

       sumsofar + alist[x]

        x = x + 1

But there's something wrong with the code above. Can you guess what it is? Each time we go through the loop, we do add sumsofar and the element at x in alist, but this value isn't being saved because we're not assigning it to anything! What do we assign it to? sumsofar! Just like the x = x + 1 line, which tells us that the new value of x is the old value of x, plus one, we need a line that tells us what the new value of sumsofar is.

sumsofar = sumsofar + alist[x]

Adding this to our code will save our progress!

At this point, our loop should be working correctly. It should be starting at the first index of the list, getting the value, adding it to the sumsofar variable, and moving on to the next index of the list. All we have left is to divide the sum by the number of elements in the list, which we can do like this:

sumsofar/length #our sumsofar variable has stored the sum of all the elements in the list, and the length variable has stored the length of the list.

All we need to do is assign the value we get from this calculation to another variable, which we'll call average. Simple.

average = sumsofar/length

Then, we just return the average.

return average.

To put it all together, check out this screencap that I posted.

You'll notice that instead of returning the correct average (which is 5.5) the function returns 5. That's because we're doing integer division. We are dividing an integer by an integer, so Python thinks we want an integer as the answer. In Python, decimals are treated differently than regular integer numbers. If we want our function to return the decimal value, or float value as Python calls it, then we have to make one of our values a float. Let's try making the sumsofar a float. We do this by typing:

float(sumsofar)

It works just like the len function, where you put the list you want to know the length of in between the brackets. Here, you're putting the integer value that you want to turn into a float.

So our calculation line (average = sumsofar/length) will look like this:

average = float(sumsofar)/length

And now, Python should be telling you that the average of the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 5.5, which it is indeed.

Test it out!

I know that this tutorial was a bit longer than the others, but that's just because we covered like three times the material in this one than we did in the other ones. To recap, in this tutorial we learned about:

1. while loops

2. lists

3. built in Python functions (len, float)

4. variables

In the next tutorial, we'll work with strings and tons of other cool Python things! Here's to you and your awesome coding abilities! Let me know if you have any questions or comments!

Avatar
Anonymous asked:

Lessons?

Yep, working on lessons. Been a little busy with school + finals. Sorry!

Avatar

It's great that there is now a Feminist Hacker Barbie, but I'm tired of still not seeing any role models for women of color. Why can't one of the names be Indian, or Chinese, or Latina, or Black? Instead, it's always a generic "white" name - like Mrs. Smith. Or, it's just generic blonde-haired, blue-eyed Barbie who, as a young girl, I thought was super cool, but couldn't relate to.

Avatar
Anonymous asked:

Any more tutorials? I a freshman in college and I am taking my first computer science class in the Spring. I'm very worried and scared that I'll be in a class full of experienced students, but I think CS is cool. Any advice?

Hey there!

Don't be scared. That is the worst thing you can do, because from what I've learned, good programming does take a lot of skill and knowledge, but it also takes a lot of willingness to learn (which I'm sure you have, just by taking the class in the first place), and it takes a lot of confidence. I can't tell you the number of times that I've doubted myself in my programming classes, and worried about people being smarter/more skilled than me, and all it did was prevent me from doing my best. If I had spent that time working on my ideas, and trying to learn more, I'd have been a lot better off.

So, don't worry! I started from scratch, not even knowing what programming really was. You will be OK. I've been behind on tutorials because I've been sick for the past week, but I'll be putting more stuff up in the weeks to come! Also, everyone in your intro to programming class is probably taking intro for a reason - they're beginners, just like you. If you ever need help with something, feel free to shoot me a message! :)

Avatar

Jezebel or Lucifer?

I recently read a very ridiculous article on Jezebel about Sarah Baartman and wanted to inform people that the article is not one to be trusted.

If you haven't already read this article, please do so after you do a bit more research on Sarah Baartman. A great documentary to watch is "The Life and Times of Sara Baartman" by Zola Maseko.

I'm not sure where to watch the film online, but you can buy the DVD here. It's probably even in your local library.

To Jezebel, here is my response:

Dear author & Jezebel,

For the sake of humanity, I hope that this article is supposed to be satire and that many people are just failing to recognize that. However, if that isn't the case, then I don't think Jezebel - a supposedly progressive, and feminist organization - is the appropriate place for such a poorly researched, and incredibly disrespectful article. Even if this is satire, it still doesn't belong on Jezebel. On The Onion? Maybe. Most of the people on the internet do not have an attention span to read this article, much less the intelligence or prior knowledge to understand (if it is satire) the satire.

It is infuriating that you would take an extremely rich, privileged celebrity and compare her to a woman who was kidnapped, put in cages like an animal, displayed in the nude as though she were an object - rather than a human being, physically abused, and continually exploited in numerous other ways by whites for the entire duration of her life.

Do you have any idea how problematic this article is in itself? Not to mention, once again, that it is being published on YOUR site? A supposedly renowned news source...

The title of the article itself is horrifying. How dare you call yourself a feminist organization while you yourself are exploiting Sarah Baartman. You're calling her the "Original Booty Queen?"

You're no better than the scientist Georges Cuvier who cut up her body parts after she died and the people who displayed her in a museum for years, instead of allowing her remains to be buried - as they rightly should've been.

Please, check yourself. And your sources. The world has enough people and organizations that sexualize women of color. We don't need "feminist" organizations that do it too. If you want to continue posting ludicrous articles and misinforming people about important aspects of history that are rarely discussed, maybe you stop claiming "feminism" - whatever that has become today, because it sure isn't what I thought it was - and change your blog's name to "Lucifer," since you are pretty much acting the role of the ignorant and privileged white colonizer.

Sincerely, A former (much embarrassed) reader of your blog.

Avatar
Anonymous asked:

The "I" in IDE does not stand for "Intelligent".. Just so you know.

Thanks for pointing out the typo! I knew what IDE stands for. I re-read the post so many times that it just slipped my eye .. I .. haha get it.

Avatar

Write a program! - Lesson 2

Now that we have some basic things down (see Intro and Lesson 1 if you're just starting out) let's figure out how to write a program!

So, whenever you write a program, the first thing you're gonna want to do is think it through with some pen and paper. It's just like when you write a paper. You think about the paper's topic, and create an outline before you actually start writing it.

What exactly is pseudocode?

It's the outline of a program in "normal" English language. You write down things you need to do for the program in a way that you can easily translate to code.

Do you feel ready to write your first program?

Let's set our goal for this lesson to be to write a program that adds three numbers and tells us the sum of all three numbers.

First: we write our pseudocode.

Pseudocode:

1. Look at the numbers we need to add.

2. Add the first number to the second number, then add the sum of the first two numbers to the third one.

3. Tell the user what the sum of all three numbers was.

Now, translate that into code form.

First of all, you're going to want to define your function. To do that, we use a Python keyword, def. The function definition consists of the def keyword, the function's name (which is whatever you decide to call it), a pair of parentheses, and a colon.

Remember this: you should never call your function something that is a Python keyword. I'll make another post with Python keywords you should know, and some other useful info).

So, let's say we want to call our program "addThree". Our function definition would look like this:

def addThree( ):

But something's wrong here. We wanted to add three numbers right? Well, the computer doesn't know which three numbers we want to add. At this point, our function isn't even telling the computer that our program should be adding three numbers. That's why we need to have arguments or parameters.

An argument or parameter is a value that you pass into your function so that it can use the value to do what the function is supposed to do. In our case, we need three arguments, because our function is supposed to add three numbers. But we don't always want to add the same numbers, right? Sometimes we want to add different ones - which is why we will use x, y, and z as our arguments. They will basically serve as placeholders for the actual values we want to add.

So, our function now looks like this:

def addThree(x, y, z):

Next, we want to tell it to add them. Well, if you were just writing out a math equation, you could do: x + y + z. Of course, you'd have to remember what x + y is and then add that to z. But lucky for us, Python has a built in calculator that can do all that work for us! The "+" sign adds numbers (and many other things, as we'll learn later on), just like it does on a calculator.

So, we have our function name/heading down, and we have our adding operation down. The final thing we need to do is tell the user what the sum was. But we already know how to tell the computer to say something to the user - we use the print statement. What do we need to print? The value of x + y + z.

With all this knowledge, let's put our function together:

def addThree(x, y, z):

       print(x+y+z)

On Wing, your program should look like this:

Now, hit the green play button. That's the Run button. It compiles your code - which means that it translates what you've written in Python to a language that the computer REALLY understands, so it can do exactly what it's supposed to do.

Once you hit the Run button, you should see your Python shell restart (the text in it will disappear for a second and reappear). Now, you can test your function out!

In your Python shell (the smaller white frame), type:

addThree(1, 2, 3)

Hit Enter.

Note that you don't need to add the def keyword here. That's because you already defined the function. Now, you are trying to use it.

The shell should return 6 - which is the right answer!

Here's a photo of what your shell should look like:

Test our your function on a bunch of different numbers and see if it always works! You can also practice writing a function that multiplies four numbers together. Here's a hint: you do pretty much the same thing as you did for this function, except you use the multiplication symbol - "*" (shift 8).

So, what we've seen today is that a python function pretty much works like a recipe. The first line (where you define the function) is like the title of the recipe. The stuff in the parentheses (arguments) are like the ingredients you need for your recipe. And whatever comes after the colon is what you need to do to cook the dish.

Let me know if you have any questions or comments!

In the next lesson, we'll be writing some more complex functions and using more of Python's built in functions. Also, watch out for a post about useful Python information!

Avatar

Made it to New York Magazine

So, I was contacted by an editor at New York Magazine a couple of weeks ago to be interviewed about binge drinking at University of Iowa. Apparently she found a column I wrote for The Daily Iowan on binge drinking and she really liked it, so she decided to message me.

Pretty freaking exciting.

Here's a snippet of where I come up in the article:

Check out the full article here:

http://nymag.com/thecut/2014/10/why-do-college-kids-drink-so-much.html

Feeling pretty awesome.

Avatar

Another question sorry, do you have any experience with PyQT or monkey studio as an IDE? :) x

Avatar

Hello! Nope. I’ve never used either of them, but I’m sure I could figure it out. Are you using one of those IDEs? And feel free to ask as many questions as you want!

You are using an unsupported browser and things might not work as intended. Please make sure you're using the latest version of Chrome, Firefox, Safari, or Edge.