Skip to content

Commit

Permalink
Fixed non-Python-3 bug in post 03.
Browse files Browse the repository at this point in the history
  • Loading branch information
mprat committed Nov 18, 2015
1 parent 8e61822 commit 3a88fd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ _site/
output.html
output.txt
*.sublime-workspace
.DS_Store
*.lock
86 changes: 0 additions & 86 deletions Gemfile.lock

This file was deleted.

42 changes: 21 additions & 21 deletions _posts/2014-02-15-03-list-less-than-ten.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,33 @@ This week's exercise hits on a topic critical for all types and styles of progra
The way to construct an empty list is just to do

{% highlight python %}
x = []
x = []
{% endhighlight %}

And your variable `x` now holds an empty list. To add things to this list, just "append" them to the list. Like so:

{% highlight python %}
x = []
x.append(3)
x = []
x.append(3)
{% endhighlight %}

Your list `x` now looks like `[3]`.

In Python, lists are also **iterables**, which means you can loop through them with a **for loop** in a convenient way. (If you come from other languages like C++ or Java you are most likely used to using a counter to loop through indices of a list - in Python you can actually loop through the elements.) I will let the code speak for itself:

{% highlight python %}
my_list = [1, 3, "Michele", [5, 6, 7]]
for element in my_list:
print element
my_list = [1, 3, "Michele", [5, 6, 7]]
for element in my_list:
print(element)
{% endhighlight %}

Will yield the result:

{% highlight pycon %}
1
3
"Michele"
[5, 6, 7]
1
3
"Michele"
[5, 6, 7]
{% endhighlight %}

There are many other properties of lists, but for the basic exercise all you should need is this for loop property. Future weeks will address other properties of lists.
Expand All @@ -81,17 +81,17 @@ For more information about lists in Python, check out these resources:
The nice thing about conditionals is that they follow logical operations. They can also be used to test equality. Let's do a small example. Let's say I want to make a piece of code that converts from a numerical grade (1-100) to a letter grade (A, B, C, D, F). The code would look like this:

{% highlight python %}
grade = input("Enter your grade: ")
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 65:
print("D")
else:
print("F")
grade = input("Enter your grade: ")
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 65:
print("D")
else:
print("F")
{% endhighlight %}

What happens if `grade` is 50? All the conditions are false, so `"F"` gets printed on the screen. But what if `grade` is 95? Then all the conditions are true and everything gets printed, right? Nope! What happens is the program goes line by line. The first condition (grade >= 90) is satisfied, so the program enters into the code inside the `if` statement, executing `print("A")`. Once code inside a conditional has been executed, the rest of the conditions are skipped and none of the other conditionals are checked.
Expand Down

0 comments on commit 3a88fd0

Please sign in to comment.