Computer Science Canada

Python Exercises

Author:  McKenzie [ Sat Jan 12, 2008 2:20 pm ]
Post subject:  Python Exercises

I assume that if you are looking in the Python Tutorial section you are interested in learning Python. There are a number of great Python tutorials out there, the problem is simply reading them and trying a few little scripts is not enough to really learn Python. The best way to learn something is by doing it. Here are a collection of exercises that I give to my grade 11 students. Keep in mind that most of my students start with no background whatsoever, so if you already know how to program then the early exercises will be very simple. I would suggest doing them anyways. The more problems you do the better ingrained the syntax of the language will become. In my class work comes in the form of exercises, assignments and projects. Exercises are problems that we simply take up in class, but are not marked. If you really want more work I can make my assignments and projects available, but the exercises should be good enough to teach the language fundamentals.

Author:  Nick [ Sat Jan 12, 2008 3:18 pm ]
Post subject:  RE:Python Exercises

I learn best through exercises so for this I thank you +10 bits

Author:  SIXAXIS [ Tue Feb 19, 2008 3:52 pm ]
Post subject:  Re: Python Exercises

Thanks.

P.S. You teach Python in grade 11? Our school teaches Java instead.

Author:  BigBear [ Tue Feb 19, 2008 4:04 pm ]
Post subject:  Re: Python Exercises

My school only teaches turing? I wonder why...

Author:  Mackie [ Tue Feb 19, 2008 5:08 pm ]
Post subject:  RE:Python Exercises

Awesome stuff. +30 bits

Author:  McKenzie [ Tue Feb 19, 2008 8:17 pm ]
Post subject:  Re: Python Exercises

Why Turing/Java/Python in grade 11?

The choice of language for grade 11 is something I assume most schools take fairly seriously. I've been teaching 13 years and every year I review my choice of languages and ask myself if they are the best choices. I assume I'm not alone in this. Every language has Pros and Cons.

Despite what you might think or hear around here I believe Turing is still a solid language in grade 11. It's a small language, so it is easy to learn almost all of the commands in one year and focus on imperative problem solving. It's a fun language to program in that makes graphics very easy to use. Turing does have a number of problems. It not being used outside of the Ontario Education system is probably the biggest. The next biggest is that the object-oriented syntax feels like it was awkwardly tacked on.

I think most schools that teach Java in grade 11 do so because they want to use the same language for gr 11 and 12 so they can get past talking about syntax and focus on key concepts. I can't imagine many teachers feeling that Java is a great starter language, but in the big picture I understand their choice. Why Java in grade 12? Lots of reasons. AP CS is in Java, universities use Java quite a bit (yes I know about Waterloo and Scheme)

I don't know of any other schools teaching Python in grade 11, but I wouldn't be shocked to find one. I chose Python for a number of reasons. It is a language that is actually being used. It has syntax that is about as easy to pick up as Turing. I'm hoping the object-oriented nature of it will allow a better transition to Java in grade 12. I'm someone who likes to have different languages each year to force students to be more adaptable and to realize that most computer concepts/algorithms transcend the language they use. I debated long and hard between Python and Ruby, but decided on Python based on the strength of pygame, and the relative maturity of the community. Most students like to do a game for their final project so pygame was very important to me.

Please don't annoy/harass your teachers about their choice of programming languages, but a long as you approach them from a point of view of genuinely wanting to know why they teach language X, and have they considered language Y most teachers will be very honest with you.

Author:  wtd [ Wed Feb 20, 2008 1:35 am ]
Post subject:  Re: Python Exercises

McKenzie @ Wed Feb 20, 2008 9:17 am wrote:
Why Turing/Java/Python in grade 11?

The choice of language for grade 11 is something I assume most schools take fairly seriously. I've been teaching 13 years and every year I review my choice of languages and ask myself if they are the best choices. I assume I'm not alone in this.


I'm not sure that's a safe assumption. You seem an uncommonly conscientious teacher. The fact that you come here and interact with students in your free time demonstrates that quite well.

Author:  McKenzie [ Wed Feb 20, 2008 7:24 am ]
Post subject:  Re: Python Exercises

It's not an accurate assumption, but it is a safe one. If kids assume that their school is teaching language X because it's all they know, or those are the books they have then if they talk to their teacher will be more likely to come off with an arrogant attitude. If they start with the assumption that the language choices were made for good reasons then ask what they are then they will come off with respect. No matter how good or bad your teacher is, respect goes a long way. Assume the best of people, if they prove you wrong, so be it.

In they old system I used to teach
gr 11 - Turing
gr 12 - C
OAC - C++
Every year I would have one or two arrogant kids in gr 11 ask why can't we learn a real language like C, and at the same time I would have good grade 12 kids fighting to learn basic C syntax and missing the "good-ol-days" of Turing. Even this year, teaching Python, I've had a kid look down their nose at Python and say we should be learning C++ instead. Not that he knew anything about C++ to make that call, just the basic assumption that no matter what we are doing it's not good enough for him.

Author:  wtd [ Wed Feb 20, 2008 10:36 am ]
Post subject:  RE:Python Exercises

It was meant more as a compliment than as a way of denigrating your peers. Still, I can understand where you're coming from.

Author:  wtd [ Tue Feb 26, 2008 4:25 pm ]
Post subject:  RE:Python Exercises

From unit 2.5:

code:
i = 0

while i < 10:
    n = i ** 2
    if i % 2 == 0:
        print n


I could be mistaken, but is this not an infinite loop, as nothing updates i?

Author:  McKenzie [ Tue Feb 26, 2008 7:34 pm ]
Post subject:  RE:Python Exercises

Oh, I never claimed they were perfect.

Author:  wtd [ Wed Feb 27, 2008 1:30 am ]
Post subject:  RE:Python Exercises

Constructive criticism. Smile

I'm a big fan of release early and realease often and constantly revising. But that only works if people are annoying and point out bugs.

Author:  swami [ Tue Apr 01, 2008 9:54 pm ]
Post subject:  Re: Python Exercises

Yea, at my school we learned the basics of Turing in grade 10 and went more in depth in grade 11. Now in grade 12 we are learning Python. Last year in grade 12 students learned Java. My teacher is deciding to teach something new and introduce a language that he himself dose not fully know yet.

Author:  wtd [ Tue Apr 01, 2008 10:02 pm ]
Post subject:  RE:Python Exercises

If he fully understood Java then he'd be quite a marvel, wouldn't he? Wink

Author:  A.J [ Tue Apr 01, 2008 10:05 pm ]
Post subject:  Re: Python Exercises

Thanks Mckenzie!
I didn't intend to learn Python, but now if I want to, I can Very Happy!

Author:  Aziz [ Tue May 06, 2008 11:58 am ]
Post subject:  RE:Python Exercises

Python's got a good tutorial on their site for those who are already familiar with programming. Its fast and in-depth.

And while I really liked my own high school CS (much better than most of my college ones), I wish he would have had the interest for it as you do, McKenzie. Do you have any advanced python examples? I've got the attachment, but I'm at work so I can't look at it too much.

(BTW, did you know a Mr. Nehmetellah that taught there (I think)? He was my CommTech teacher a couple years ago at Cardinal Carter)

Author:  McKenzie [ Tue May 06, 2008 7:41 pm ]
Post subject:  RE:Python Exercises

There are a number of good tutorials out there, but I find where most of them lacking is interesting assignments/exercises to challenge the reader and force them to apply what they have just read. It is by doing that you learn.

You will find no advanced examples in the attachment. I have a bunch if you are interested, but I find basic examples are better. They allow you to focus on just one issue at a time.

(aside, I taught with Mr. N. for one year. I didn't know him very will but he seemed cool to me)

Author:  taylankaraman [ Wed Feb 25, 2009 3:55 am ]
Post subject:  Re: Python Exercises

Thanks McKenzie! The exercises are great.
Greetings from Turkey! Very Happy

Author:  Sniper4Life [ Sun Apr 12, 2009 4:53 pm ]
Post subject:  Re: Python Exercises

k ive got a little questionn...i wanna know if my answer to the very first question on unit 2.1 is SUFFICIENT

code:
x=123
print x*3.14*2



by your standerds if it is sufficientt
it answers the question...but...still...doesnt seem like enough to me.....

Author:  Zeroth [ Sun Apr 12, 2009 6:23 pm ]
Post subject:  Re: Python Exercises

Thats it. With Python, sometimes it seems like it makes programming TOO easy.

Author:  Sniper4Life [ Sun Apr 12, 2009 8:03 pm ]
Post subject:  RE:Python Exercises

thats what makes it perfect for beginners Very Happy
Ex1:
x=123
print x*3.14*2



Ex2:
r = input("Radius: ")

print " "

print"The area of your cirlce is: "

print r*r*3.14


Ex3:
c=input ("How much Canadian dollars you have: ")

print " "
print "You will have ", c*1.6156, " Euros after your money has been converted"





Ex4:

c=raw_input ("First name: ")
r=raw_input ("Last Name: ")

print " "

print r,",", c


McKenzie can you tell me if my answers are OK?

Author:  Tony [ Sun Apr 12, 2009 9:07 pm ]
Post subject:  RE:Python Exercises

code:

print r,",", c

That is not ok. Whitespace is your friend. So are proper variable names.

Author:  Sniper4Life [ Sun Apr 12, 2009 9:23 pm ]
Post subject:  Re: RE:Python Exercises

Tony @ Sun Apr 12, 2009 8:07 pm wrote:

That is not ok. Whitespace is your friend. So are proper variable names.


its just 4 lines of coding...is there rlly a need for proper variable names?
its not that bad...it doesnt nessesscarily make the code BAD
and whitespace?

btw...this is the question

Gets the user?s first and last name and outputs them in reverse order with a comma between them.

also...what do you think about the OTHER codes?

Author:  Tony [ Sun Apr 12, 2009 9:34 pm ]
Post subject:  RE:Python Exercises

Yes, there really (vowels are your friend as well, use them Wink ) is a need to make your code easy to read and understand.

A 4 line function within a 10,000 line application is arguably still "just 4 lines of code".

If there are some terms that you don't understand, learn how to look them up quickly. I have keyboard shortcuts to Google, Wikipedia, and a Dictionary.

Author:  Sniper4Life [ Sun Apr 12, 2009 9:42 pm ]
Post subject:  Re: Python Exercises

code:
firstpx=input ("First point's x coordinate: ")
firstpy=input ("First point's y coordinate: ")

print " "
secondpx=input ("Second point's x coordinate: ")
secondpy=input ("Second point's y coordinate: ")
print " "

print "First point:  ","(", firstpx,",",firstpy,")"
print "Second point: ","(",secondpx,",",secondpy,")"
print " "

print "Distance: ""(",firstpx-secondpx,",",firstpy-secondpy,")"


there...are the new variables better now Tony?
satisfied? Razz
woops i just realized this is a different code Razz
o well....btw...how would you make it that when the Distance comes up...the numbers only go as far as 2 decimal places....????

Author:  Alexmula [ Mon Apr 13, 2009 12:14 am ]
Post subject:  RE:Python Exercises

use the round function

Author:  Sniper4Life [ Mon Apr 13, 2009 9:57 am ]
Post subject:  RE:Python Exercises

ya unluckily i dont know what that is......someone else told me it was %f"x"

but i thought it was like round(something something)

Author:  Alexmula [ Mon Apr 13, 2009 11:50 am ]
Post subject:  RE:Python Exercises

python.org has an online manual..

you can also try typing "print help()" in IDLE i think it you can also get help there

and yes there is a round function: round(x[n]) Laughing x being the number u want to round and n the number of decimal places i believe

Author:  Sniper4Life [ Mon Apr 13, 2009 12:17 pm ]
Post subject:  RE:Python Exercises

dont worry im dont that question in the exercises now ^^
i even did introEx6...im in 2.2 questions now Very Happy
unluckily for me...i dont know anything bout these:

- Concatenation (+)
- Multiplication (*)
- Slices
- Escape sequences
- len()
- String Methods
upper
lower
find
replace
count

ya...so its gonna be a bit hard for me...
i feel somewhat proud?Razz
doing grade 11 exercises Very Happy
lol they're easy exercises tho(in the beginning)...i seriously wish they had advanced math and like computer science in middle school...like an option for those things

middle school teaches how to use movie maker Razz
lol

Author:  wtd [ Mon Apr 13, 2009 5:12 pm ]
Post subject:  Re: Python Exercises

Sniper4Life @ Mon Apr 13, 2009 5:53 am wrote:
k ive got a little questionn...i wanna know if my answer to the very first question on unit 2.1 is SUFFICIENT

code:
x=123
print x*3.14*2



Again, get into the habit of using whitespace.

code:
x = 123
print x * 3.14 * 2


Author:  Sniper4Life [ Mon Apr 13, 2009 5:23 pm ]
Post subject:  RE:Python Exercises

k thanks wtd Very Happy
i kno its a habit of not using whitespace because well....when you write math...you wouldnt use whitespace Razz

Author:  Aziz [ Mon Apr 13, 2009 6:02 pm ]
Post subject:  RE:Python Exercises

You're what, in grade 10, I assume? Grade 10 math may not need to be organized (though don't you do quadratics?), but it's really an important part to organize when solving complex problems/equations

Author:  saltpro15 [ Mon Apr 13, 2009 6:26 pm ]
Post subject:  RE:Python Exercises

grade 10 math varies for neatness, some teachers don't care how messy your work is as long as it's correct, whereas the pickier ones take marks off if say equal signs don't line up, work is smudged, etc.

Author:  Sniper4Life [ Mon Apr 13, 2009 6:36 pm ]
Post subject:  RE:Python Exercises

haha grade 10....no of course not...id be much more competent by then...im only in grade 8 Razz

Author:  Aziz [ Mon Apr 13, 2009 6:45 pm ]
Post subject:  RE:Python Exercises

It's not so much about marks as it is your own benefit.

And grade 8, HOLY CRAP, that's a yougin'. Keep it up, bro... in grade 8 I knew I wanted to be a programmer, but I didn't start until grade 10 in high school. Just keep an open mind and listen to what people here tell you, you'll do good.

Author:  Sniper4Life [ Mon Apr 13, 2009 6:58 pm ]
Post subject:  RE:Python Exercises

lol trust me...im like a noob compared to some peoples in my school Razz
my friend was scripting since the age of 7...and DANM
he knows a hell lot...has learned like 6 languages Razz
no more i think
unluckily for him he needs to improve in other academic parts
like even though hes really good with computers...he needs to work on things like english and other stuff...

Author:  Tony [ Mon Apr 13, 2009 7:12 pm ]
Post subject:  Re: RE:Python Exercises

you'd be surprised how important things like English will turn out to be, even in a technical field with computers.

Sniper4Life @ Mon Apr 13, 2009 6:58 pm wrote:
has learned like 6 languages


You might find this interesting -- http://compsci.ca/blog/you-dont-know-that-programming-language/ article + some discussion in the comments

Author:  Sniper4Life [ Mon Apr 13, 2009 7:17 pm ]
Post subject:  RE:Python Exercises

still...his UNDERSTANDING of the languages and of
computer science could definitely be considered "greatly above average"

Author:  Tony [ Mon Apr 13, 2009 7:17 pm ]
Post subject:  RE:Python Exercises

I have no doubt about that.

Author:  Sniper4Life [ Mon Apr 13, 2009 7:19 pm ]
Post subject:  RE:Python Exercises

btw tony since your here can i ask u sumthing?
what grade in waterloo are you in?

Author:  Tony [ Mon Apr 13, 2009 7:43 pm ]
Post subject:  RE:Python Exercises

my transcript says 2B. And this has gotten way off topic.

Author:  bill9786 [ Thu Nov 11, 2010 8:06 pm ]
Post subject:  Re: Python Exercises

"I don't know of any other schools teaching Python in grade 11, but I wouldn't be shocked to find one."


Have a look at this site - the University of Sydney runs a python challenge for high schools kids in years 9 - 12 - beginners and advanced - over a five week period every year. I teach it as an 'antidote' to Visual Basic Express.
http://challenge.ncss.edu.au.


: