Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Last active September 6, 2015 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/9010980 to your computer and use it in GitHub Desktop.
Save oconnor663/9010980 to your computer and use it in GitHub Desktop.
First Python problem set: if and while

If you already have a Python environment already set up that you're comfortable with, great. If not, I recommend downloading Python 3 from http://www.python.org/download/. It comes with a program called IDLE that helps you write small programs and run them. YouTube has some good videos for getting started with that, depending on whether you're running Windows, Mac, or Linux.

Here's the Python book I like to use to get started: http://openbookproject.net/thinkcs/python/english3e/

To do the problems below, you'll need to be roughly familiar with chapters 1-8 in that book, particularly chapters 5, 7, and 8. If you're starting from scratch, it might be a good idea to try the exercises in those chapters as you go along. When you're up to speed, you'll have the following basic building blocks for your programs:

  • how to print output
  • how to create variables and change their values
  • how to use if statements to make choices
  • how to use while loops to do repeated work

Those basic tools are all you'll need to solve the problems here. The next steps will be to learn about functions (which is actually chapter 4) and then lists (chapter 11), but I like to hold off on those until we're really, really comfortable with the basics.

###Example###

Question: Write a program that computes 2 times n.

Answer:

n = 4            # any starting value will do
result = 2 * n
print(result)

The first line creates the variable n, which the rest of the program treats as input. So, if we want to know 2 times 5 instead of 2 times 4, we just change the first line to n = 5. The rest of the program doesn't need to be changed.

Use this pattern in all the exercises below. Create one or more variables at the top of your code to be inputs, and then write the rest of your solution to read those variables and print out the answer. When you're done, use different input values to test your code. You might want to create a folder on your desktop, so you can organize all your solutions to these problems in one place.

###Exercises###

  1. Write a program that computes n * m. This will look a lot like the example above, with one extra input. Test your program with lots of different inputs, including zero and negative numbers. Try some very big inputs. Is there any limit to how big they can be?

  2. Write a program that computes n squared.

  3. Compute n to the power of m. There are two ways to do this. One is the Python's built in "exponentiation operator", which looks like two stars: **. The other is using ordinary multiplication inside a while loop. Write both solutions separately, and make sure they give the same answer for the same input. If you use large values of n and m, which solution runs faster?

  4. Take two inputs, n and m. If n is more than twice as big as m, print "# is very big". Otherwise, print "# isn't very big at all". Substitute the value of n in place of "#" in your output.

  5. Take one input, n. If n is even, print "even". Otherwise, print "odd". You'll want to take a look at the section in Chapter 2 called the "The modulus operator", to figure out how to tell whether a number is even or odd.

  6. Take one input, n. For all the numbers from 1 to n, print "# is odd" or "# is even", as appropriate, with the value of each number in place of "#".

  7. Compute n * (n + 1) / 2. For example, if n is 3, your answer should be 3 * 4 / 2 = 6, and you should print "6". Take a look at the section of Chapter 2 called "Operators and operands", which talks about the two different types of division in Python. Try both different types in your program. What difference do you see?

  8. Compute the sum of all the numbers from 1 to n. So for n = 4, your answer should be 1 + 2 + 3 + 4 = 10. Compare the output of this program to the output of your solution to problem 7 above. Notice anything interesting?

###Challenge problems###

  1. Compute n factorial. (http://en.wikipedia.org/wiki/Factorial) When we learn to write functions we will learn an especially clever way to solve this problem called "recursion", but for now we can solve it with just variables and a while loop. When you've got it working, try it with some huge numbers for fun.

  2. Compute the nth Fibonacci number. (http://en.wikipedia.org/wiki/Fibonacci_number) As with the factorial problem, we will see this problem again after we learn about functions and recursion.

  3. Given a number n, decide whether n is prime, and print either "prime" or "not prime". (http://en.wikipedia.org/wiki/Prime_number) How big does n have to be before your code gets really slow? Google some large prime numbers to try it out.

  4. Given two numbers n and m, print all of their common factors. If there are none, print "coprime". For example, the common factors of 12 and 18 are 2, 3, and 6, and the numbers 14 and 15 are coprime.

  5. Write a program that finds all the perfect numbers (https://en.wikipedia.org/wiki/Perfect_numbers) between 1 and 1000. Is your program fast enough to go up to 10000? How high can it go? It turns out that the ancient Greek mathematicians knew about all the perfect numbers up to 10000. To beat them, we'll have to make our program run very fast indeed, and that's going to require some more building blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment