CSE 142 Python Program

Python session time/place: Tuesdays, 3:30 - 4:20pm, in MOR 220

Week 10 (Pygame)

  • Lecture Slides: ppt ppt, pdf pdf
    More Pygame: ppt ppt, pdf pdf
    Whack-a-mole: zip mole
  • This week we will be discussing a third-party python library called pygame. With pygame we can easily make appealing and interactive video games. Pygame still uses python 2.6.

  • Python Homework 10: game
    Due Monday, January 4, 11:30pm. Turn in Python10 here.
    No late turnins accepted.

    The assignment: Turn in game.py, any interactive game written in python using the Pygame library except for the whack-a-mole game. Notice that the due date is in January, so your reward for turning in this assignment is straight up cool points.

Week 9 (classes, objects)

  • Lecture Slides: ppt ppt , pdf pdf
  • Python Homework 9: Personality Test OR Date/Birthday2
    Due Monday, December 7, 11:30pm. Turn in Python9 here.
    No late turnins accepted.

    The assignment: You have two choices of possible programs this time. If you want to write the personality test, turn in a file personality.py containing a Python version of your HW7 Personality.java program, producing the same output. This one doesn't practice today's new material about Python classes and objects; we are offering this option if you want a more familiar assignment and want practice with arrays.

    The second choice is to do a new object-oriented assignment that practices today's material on Python classes and objects. If you choose this option, turn in two files. The first is a file named date.py that implements a class Date, where each Date object represents a single calendar month/day date such as September 19. Each Date object should have the following behavior:

    • A constructor that accepts a month and day as parameters.
    • A days_in_month method to return the number of days in that Date object's month.
    • A next_day method that changes the state of the Date by advancing it 1 day in time. For example, the next day after 9/19 is 9/20; the next day after 9/30 is 10/1; and the next day after 12/31 is 1/1
    • A __str__ method to convert the Date into a string such as "9/19".

    For example, it should be possible to use the Date class in the following way at the interpreter:

    >>> d = Date(9, 29)
    >>> d.month
    9
    >>> d.day
    29
    >>> d.days_in_month()
    30
    >>> d.next_day()
    >>> d.month
    9
    >>> d.day
    30
    >>> d.next_day()
    >>> d.month
    10
    >>> d.day
    1
    >>> d.days_in_month()
    31
    >>> str(d)                         # calls __str__
    '10/1'
    >>> d.next_day()
    >>> str(d)                         # calls __str__
    '10/2'
    

    The second file to submit is called birthday2.py. This file should be a client program that uses your Date objects to find out how many days it is until the user's next birthday. The following is a sample run of the birthday2 program:

    Please enter today's date:
    What is the month? 7
    What is the day? 24
    
    Please enter your birthday:
    What is the month? 11
    What is the day? 6
    
    Your next birthday of 11/6 is in 105 day(s).
    

    Notice that this version of the program doesn't print the absolute day of the year for today or the birthday. You should figure out how many days it is until the user's birthday in a different way. Starting from today, if you walk forward in time until the user's birthday, one day at a time, counting how many steps you took, you'll know how many days it is until the user's next birthday. You don't need to handle the "birthday is tomorrow" case this time; just either print Happy Birthday! or print how many day(s) until the next birthday.

    Whichever program you choose to implement, the reward for turning in this program is 1 extra late day for use on your normal Java programs, and +1 charisma and 2 saving throws. (Since it's the end of the quarter, getting extra late days is less motivating than it used to be. Mostly you'd just be doing this one for the learning experience.)

Week 8 (SAGE)

  • Lecture Slides: ppt ppt, pdf pdf

    SAGE Lecture Worksheet: (To view this, you must go to the SAGE Notebook, click "File" (which is located at the top), and click "load worksheet from file") sws SAGE
  • This week is on a third-party application of Python. We will look at SAGE which is an open source mathematical software that is based in Python. It still uses Python2.6.

  • Python Homework 8: Mandelbrot Set
    Turn in Python HW7 here.
    The assignment pdf pdf
    Due Tuesday, December 1, 11:30pm.

Week 7 (file processing, lists)

  • Lecture Slides: ppt ppt, pdf pdf,
  • py midterm.py
  • py map.py
  • py underpaid_tas.py
  • txt scores.txt
  • txt cities.txt
  • txt hours.txt
  • Python Homework 7: Mad Libs
    Due Tuesday, November 24, 11:30pm. Turn in Python HW7 here.

    The assignment: Turn in mad_libs.py, a Python version of the Java mad libs assignment [pdf spec]. The reward for this program is 1 extra late day.

    You should include the following import statement in your final product. Importing the os modile will let us check to see if files exist.

    import os
    
    The os.path.exists method will return True if a file with a given name exists within the working directory.
    name = input("Input file name: ")
    if os.path.exists(name):
        ...
    

Week 6 (review, file IO)

  • Lecture Slides: ppt ppt, pdf pdf
  • py stats.py
  • py remove_lowercase.py
  • If you missed any of the python lectures in the past and you still would like to learn, then come to the python lecture this week. We will be reviewing all of the old material as well as introducing some new things.

  • Python Homework 6: You Choose
    Due Tuesday, November 17, 11:30pm. Turn in Python HW6 here.

    The assignment: Turn in one of the previous homework assignments if you didn't get a chance to before. The reward for this program is 1 extra late day. Instructions: for you first file, name it review.py. Any subsequent homeworks should have the name of the assignment such as song.py or guessing_game.py.

Week 5 (random, while loops and tuples)

  • Lecture Slides: ppt ppt, pdf pdf
  • Python Homework 5: Guessing Game
    Due Saturday, November 7, 11:30pm. Turn in Python HW5 here.

    The assignment: Turn in a file guessing_game.py containing a Python version of your HW5 guessing game program. The reward for turning in this program is 1 extra late day for use on your normal Java programs.

    For an extra challenge: implement a two-dimensional guessing game, still named guessing_game.py, according to these specifications.

    The program will look like this:

    This program is a 2-D guessing game.
    I will think of a point somewhere
    between (1, 1) and (20, 20)
    and give hints until you guess it.
    
    Guess x: 5
    Guess y: 7
    You're cold. Go south east
    Guess x: 18
    Guess y: 5
    You're cold. Go south west
    Guess x: 15
    Guess y: 2
    You're warm. Go north west
    Guess x: 12
    Guess y: 3
    You're hot! Go north west
    Guess x: 11
    Guess y: 4
    You got it right in 5 guesses!
    
    Overall results:
    Games played  = 1
    Total guesses = 5
    Guesses/game  = 5.0
    

    More sample output:

    Test 1 | Test 2 | Test 3 | Test 4 | Test 5

Week 4 (If/Else, Logic, Strings)

  • Lecture Slides: ppt ppt, pdf pdf
  • py substitute.py
  • Python Homework 4: Birthday
    Due Sunday, November 1, 11:30pm. Turn in Python HW4 here.
    No late turnins accepted.

    The assignment: Turn in a file birthday.py containing a Python program that produces output such as the output below. The task is identical to the Java HW4 except that you will also print out the user's astrological sign such as Leo or Virgo. At the bottom of your program, you should print an interesting fact about your own astrological sign, or your sign's current horoscope. Check here to find the date ranges for each sign, or look at the following table:

    Sign Date Range Absolute Day Range
    Aquarius 21 January - 18 February 21 - 49
    Pisces 19 February - 20 March 50 - 79
    Aries 21 March - 20 April 80 - 110
    Taurus 21 April - 21 May 111 - 141
    Gemini 22 May - 23 June 142 - 174
    Cancer 24 June - 22 July 175 - 204
    Leo 23 July - 23 August 205 - 235
    Virgo 24 August - 22 September 236 - 266
    Libra 23 September - 23 October267 - 296
    Scorpio 24 October - 22 November 297 - 327
    Sagittarius 23 November - 21 December328 - 355
    Capricorn 22 December - 20 January 356 - 365 and 1 - 20

    The following is an example output from your program. Notice that for simplicity, the month and day are read on separate lines.

    This program tells you how many days
    it will be until your next birthday.
    
    Please enter today's date:
    What is the month (1-12)? 7
    What is the day   (1-31)? 24
    7/24 is day #205 of 365.
    
    Please enter your birthday:
    What is the month (1-12)? 11
    What is the day   (1-30)? 6
    11/6 is day #310 of 365.
    
    Your next birthday is in 105 days.
    
    You are a Scorpio.
    << your sign's fact or horoscope here >>
    

Week 3 (parameters, return, math, graphics, parameters)

  • Lecture Slides: week3.pdf | week3.ppt
  • drawingpanel.py
  • car.py
  • Python Homework 3: Circles
    Due Sunday, October 25, 11:30pm. Turn in Python HW3 here.
    No late turnins accepted.

    The assignment: Turn in a file circles.py (lowercase c) containing a Python version of your HW3 Circles program that produces the output shown below, essentially the same as the Java version but drawn in Python. The window size and coordinates of all drawn figures are the same as in the Java version. As a reference, our sample solution is 40 lines long. The reward for turning in this program is 1 extra late day for use on your normal Java programs.

    circles output

    You won't be able to check your output since our Python DrawingPanel doesn't know how to compare images. But we will be lenient on whether the graphical output exactly matches. If you like, you can play around a bit with the exact colors and coordinates to produce a figure you like better, so long as the overall appearance closely matches the above screenshot.

    Producing the above output will count for full credit, but if that sounds too easy for you and you want an added challenge, you can instead make your circles.py program produce the output below, drawing "globe" figures instead of the normal target figures. This is harder, but probably not as hard as it looks! Read below to see how to do it.

    globes output

    globe lines globe lines The main change between this globes program and the Java Circles version is that rather than drawing concentric circles in each subfigure, this version draws two sets of contentric ovals that give each subfigure a "globe-like" appearance. In one set, all ovals have equal heights but varying widths. The other set have equal widths and varying heights. In both cases, the amount of change in oval size is the same as the "gap" from the Java HW3-Circles assignment. The dimensions and counts for this assignment are identical to those of the Java HW3 assignment.

    The other change from the Java program is that the colors of each subfigure and grid have been parameterized. Use string parameters to represent the colors used in the screenshot, such as orange (top-right), red (bottom-middle), and blue (bottom-right). Use default parameter values and/or parameter keywords as appropriate.

  • List of all Python color names
  • Visual index to the Python colors

Week 2 (Expressions, for Loops, Constants)

  • Lecture Slides: ppt ppt , pdf pdf
  • expressions.py
  • mirror.py
  • mirror2.py
  • Python Homework 2: Space Needle
    Due Saturday, October 17, 11:30pm. Turn in Python HW2 here.
    No late turnins accepted.

    The assignment: Turn in a file ascii.py (lowercase a) containing a Python version of your HW2 Space Needle program or last quarter's Rocket Ship program, producing the same output. As a reference, our sample solution is 35 lines long for the space needle with comments. The reward you'll get for turning in this program is 1 extra late day for use on your normal Java programs.

Week 1 (Basics, Functions)

  • Lecture Slides: ppt ppt , pdf pdf
  • Python Installation Instructions
  • Python Official Website
  • Python Homework 1: Song
    Due Saturday, October 10, 11:30pm. Turn in Python HW1 here.
    No late turnins accepted.

    The assignment: Turn in a file song.py (lowercase s) containing a Python version of your HW1 Song program, producing the same output. As a reference, our sample solution is 65 lines long. The reward you'll get for turning in this program is 1 extra late day for use on your normal Java programs.


General info about the CSE 142 Python program:

This quarter in CSE 142, we will conduct a special optional program to offer students a chance to learn a second programming language as you're learning Java. The second language's name is Python.

What is Python?

Python is a language that's good for writing programs to process text and other data. It's used heavily in the Linux operating system and at companies like Google.

Why would I want to learn Python, in addition to Java?

Learning two programming languages is a bit like growing up in a bilingual family: you'll not only learn those two languages well, but you may also learn some higher concepts about programming and programming languages in general.

In addition, Python is used in a lot of other fields and disciplines, so it can be useful to have experience in it. Lastly, Python is a powerful language that does a lot of things more easily than Java, so it can be fun rewriting your past Java programs in Python and seeing how much shorter and cleaner they can be solved.

Who will run these sessions? Where will I go, and what will I do?

Our Python program will be hosted by some of our TAs, under the advisement of the instructor. Each week, they will hold a 50-minute session to teach you the equivalent of that week's Java course material into Python, along with any related issues.

The work involved in this program would be the following:

  • Going to the weekly sessions
  • Completing optional Python programming exercises each week, as desired

Primarily, these projects will consist of solving the same problem as that week's Java programming assignment, but in Python, and perhaps with minor modifications to the assignment spec.

What reward do I get for doing this? Do I have to do it?

Participation is entirely optional. The reward for doing these projects will be small, to make sure that these sessions don't give students with prior experience an unfair advantage over new programmers. Right now, we're planning to reward students with 1 free late day for each Python program submitted. No grade points will be added or subtracted in any way for participating in this project.

How do I participate or learn more?

Just go to the next Python session at the time listed above, and if you find it interesting, try writing the Python program given out for that week. If you finish it, you can turn it in from a link that we'll put at the top of this page.

If you want to install and run Python programs on your own computer, follow our Python Installation Instructions below:

Can I still participate even if I don't go to the sessions or cannot make it to the sessions? Can I still come to the sessions even if I don't want to turn in the weekly Python programs?

Yes! If you miss the session, feel free to look at each week's slides and code that we post, and/or still submit the program. And even if you come to the sessions, you don't have to work on the programs we give out; if you just want to come to the sessions and that's all, that is fine with us. It's up to you.