Friday, July 18, 2014

NEW AND UPDATED SITE

This site has been updated and moved!! Check out

http://practicepython.org

for the exact same exercises, and all further posts will go there!

Thursday, July 10, 2014

SOLUTIONS Exercise 17: How to Decode a Web Page

As promised, the exercise for How To Decode a Web Page was a hard one, and I wrote up a very detailed explanation of the solutions. However, the formatting allowed to me on Blogger were not adequate enough for what I needed, given the complexity of the solution. Instead, I wrote it up on my own blog, where the formatting options are much more customizable. Enjoy: How To Decode a Webpage Detailed Solutions!

(The format on the new site is also a trial for migrating this entire blog to that platform. Any comments appreciated!)

Saturday, July 5, 2014

Exercise 18: Cows and Bulls and a Main Method

18 Cows And Bulls
I again must apologize for being delinquent on exercise posting. I hope the quality makes up for their decrease in quantity.

Exercise

Create a program that will play the “cows and bulls” game with the user. The game works like this:
Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.
Say the number generated by the computer is 1038. An example interaction could look like this:
  Welcome to the Cows and Bulls Game! 
  Enter a number: 
  >>> 1234
  2 cows, 0 bulls
  >>> 1256
  1 cow, 1 bull
  ...
Until the user guesses the number.

Concepts for this week:

  1. Randomness (we’ve covered this a few times before. Mainly in a previous exercise.)
  2. Functions (covered in a previous exercise also)
  3. Main method

Main method

Since we have covered randomness and functions, we still need to cover the idea of a “main” method. If you have programmed before, you will wonder why you haven’t needed a main method so far in your Python code.
First, here is how you do it:
  def square(num):
    return num * num

  if __name__=="__main__":
    user_num = input("Give me a number: ")
    print(square(num))
Note that in both __name__ and __main__, there are TWO underscore (_) characters.
If you run this program, it behaves as expected: asking the use for a number and printing out the square in return. However, you might be wondering - how is this different from the way we’ve been writing programs until this point?
  def square(num):
    return num * num

  user_num = input("Give me a number: ")
  print(square(num))
You are correct if you think the second example will have THE SAME BEHAVIOR as in the first case. At least the way we’ve been running Python files until this point. I will attempt to explain.
Python does not have a specified entry point like many other languages (for example, C++ always looks for a void main() method to run). Python files and programs are executed line by line by the Python interpreter, from however the Python file or program is run. When the definition of a function is reached by the interpreter, the function is not run, but rather loaded to be run later. (We discussed this when we talked about functions in a previous post.) Whatever is written outside of a function will get executed immediately - this includes creating variables and calling the functions that were previously loaded.
There are (most commonly) two ways to run a Python file:
  1. “Just running it” (which you can do from the terminal, through the “Run” button in IDLE, etc.)
  2. Importing the file as a module
The first way is the way that you most likely have been using if you have been doing the exercises in this blog. If you have many files in a large project, this is how you run the “entry point” file in the project.
The second way is more subtle: it is what happens when you write an import statement at the top of your file. In this exercise for example, you need to import random into your program to use the random module. Somewhere on your computer there is a file (or a group of Python files) that make up the random module, and in the process of importing them, what you are actually doing is running the file(s) from that module.
When you have functions defined inside a file (with the def keyword, like the def square function above) and run a file, the function is not immediately run. You can think what happens is the function is stored for future use.
Anything else in the file - variables created, functions that are called, operations done, etc - are executed immediately when a Python file is run.
However, in the case where you are importing a Python file into another, you want to load all of the functions without creating variables or executing any functions. You just want to load them to use them later. How do you reconcile this problem? With the if __name__=="__main__" statement. Create your variables and all your functions inside this statement, and when you import your file into another, it will not mess up your program by creating variables or calling functions.
What the if __name__=="__main__" statement from above does is ensure that variables that are created, functions that are called, operations that are done, etc ONLY when you directly run the file, not when you import it into another.

Happy coding!

Forgot how to submit exercises?

SOLUTION Exercise 17: Decode a Web Page

Exercise

Use the BeautifulSoup and requests Python packages to print out a list of all the article titles on the New York Times homepage.

Solutions?

I did not get any sample solutions for this Exercise. What I will do is write the sample program, and comment every line - dissect my own program for readers to see themselves. This will be forthcoming - this post is meanwhile a placeholder.

Thursday, June 19, 2014

Exercise 16: Decode a Web Page

My sincerest apologies for being late in posting these exercises. My boyfriend came to visit me in Jerusalem for the last two weeks, so I haven't had any spare cycles to tackle the Python problems. I should learn to write a few in advance in case I am ever put in this kind of situation again. In any case, this exercise should make up for the lack of exercises the last few weeks - it's a fun one. This is a slightly longer and more involved exercise than many previous ones, so I will not post the solution for two weeks, but I will post a new exercise next week. Enjoy!

Exercise


Use the BeautifulSoup and requests Python packages to print out a list of all the article titles on the New York Times homepage.

Discussion

Concepts for this week:
  • Libraries
  • requests
  • BeautifulSoup

    Libraries

    Many people have written libraries in Python that do not come with the standard distribution of Python (like the random library mentioned in a previous post). These libraries can do anything from machine learning to date and time formatting to meme generation. If you have a task you need done, most likely someone has written a library for it.

    There are three main things to keep in mind when using a library:
    1. You need to install it. Installation in GNU/Linux based systems will generally be easier than on Windows or OSX, but there will always be documentation for how to do it.
    2. You need to import it. At the top of your program, make sure you write the line import requests, or whatever the name of your library is. Then you can use it to your heart's content.
    3. You need to read documentation. Someone else wrote it, so the rules might not be so obvious. Anyone (or any group) that writes a Python package writes documentation for it. Eventually, reading documentation will become second nature.

      Requests


      One of the most useful libraries written for Python recently, requests does "HTTP for humans." What this means in laymen's terms is that it asks the internet things from Python. When you type "facebook.com" into the browser, you are asking the internet to show you Facebook's homepage.

      In the same way, a program can ask the internet something. It might not be "show me Facebook", but you can for example ask for a list of all the repositories that the user "mprat" has. You can do this with an API (Application Programming Interface). This exercise doesn't use APIs, so we'll talk more about those in a later post.

      Back to showing the user a webpage. When I type "facebook.com" into the browser, Facebook sends my browser a bunch of HTML (basically, code for how the website looks). The browser then takes this HTML and shows it to me in a pretty way. (Fun fact: to see the HTML of any page in a browser, right click on the page and "Inspect Element" or "View Source" depending on your browser. In Chrome, "Inspect Element" will pop up a module at the bottom of your page where you can see the HTML from the page. This trick will come in handy when you're doing the exercise. If you need to DO anything with this HTML, better to use a program. More posts about this coming later.) If I want to "see" a webpage with a program, all I need to do is ask it for it's HTML and read it.

      The 'requests' library does half of that job: it asks (requests, if you will) a server for information. This could be just data (through an API - more later) or in the case of this exercise, HTML.
      Look at the documentation for all the details you need. In this particular latest version, all you need to do to ask a website for it's HTML is:

      import requests
      url = 'http://.com'
      r = requests.get(url)
      r_html = r.text

      Now inside the variable r_html, you have the HTML of the page as a string. Reading (otherwise called parsing) happens with a different Python package.

      BeautifulSoup


      To solve our problem of parsing (reading, understanding, interpreting) the string of HTML we got from requests, we use the BeautifulSoup library.

      What it does is give a hierarchical (a pyramid structure) to the HTML in the document. If you don't know anything about HTML, the Wikipedia article is a good summary. For the purposes of this exercise, you don't need to know anything about HTML beyond being able to look at it quickly.

      Because BeautifulSoup takes care of interpreting our HTML for us, we can ask it things like: "give me all the lines with <p> tags" or "find me the parent element to the <title> element", etc.

      Your code would look something like this:

      from bs4 import BeautifulSoup
      
      # some requests code here for getting r_html 
      
      soup = BeautifulSoup(r_html)
      title = soup.find('span', 'articletitle').string

      And you can do many more things in BeautifulSoup, but I will leave you to explore those by yourself or through other later exercises.

      Happy coding!


      Explore away!
      Forgot how to submit exercises?

      SOLUTION Exercise 15: A Password Generator

      Exercise

      Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your code in a main method.

      Extra:
      • Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.

        Sample solution

        There are so many possible solutions to this exercise, really depending on how far you want to challenge yourself. The field of security, especially as it relates to computing, is an ever-growing field with countless experts, theories, principles, and more.
        The sample solution here is one possible way to answer the question: it generates a string of random characters. It is clean, simple, and elegant.

        Wednesday, May 28, 2014

        Exercise 15: A Password Generator

        Exercise

        Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method.
        Extra:
        • Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.

          Discussion

          There are no new topics this week, but you will need to use Python's random module, described in this post.

          Happy coding!

          Explore away!
          Forgot how to submit exercises?