Python4Beginners

Ubuntu Opportunistic Developers Week Ubuntu Opp Dev Week Prep: Intro to Python for total beginners - Rick Spencer

[15:00] <rickspencer3> I am in Seattle, and I am about to try to introduce the notion of programming in Python to folks who haven't programmed before
[15:00] <rickspencer3> and I am really pumped
[15:00] <rickspencer3> cjohnston, so I just go?
[15:00] <rickspencer3> is there fanfare?
[15:00] <cjohnston> I'm wondering why ClassBot isnt setting things up
[15:00] <rickspencer3> trumpets, red carpet, curtain pulled back?
[15:01] <Pendulum> rickspencer3: well we can pull them out if you really want, but we're a bit lazy ;)
[15:01] <rickspencer3> Pendulum, yes, I require fanfare
[15:01] <cjohnston> rickspencer3: go ahead and start.. ill work on classbot
[15:01] <rickspencer3> cjohnston, thanks
[15:01] <rickspencer3> so, I guess if I ask things here, people answer in #ubuntu-classroom-chat, right?
[15:02] <cjohnston> yup
[15:02] <rickspencer3> so first, everyone who is here doesn't know a programming language yet, but is interested in getting started right?
[15:02] <binti_> yep
[15:02] <NVSBLmonstr> More or less
[15:02] <papapep> yep
[15:02] <rickspencer3> hehe
[15:02]  * CaptainHero too
[15:02] <rickspencer3> ok
[15:05] <cjohnston> there you go.. sorry
[15:05] <rickspencer3> np
[15:05] <rickspencer3> due to my compulsive personality, I found it necessary to more or less write out the class first
[15:05] <rickspencer3> https://wiki.ubuntu.com/UbuntuOpportunisticDeveloperWeek/IntroToPythonForTotalBeginners
[15:05] <rickspencer3> so you can follow along there if it helps
[15:05] <rickspencer3> what I hope to do in the next hour is to give you a taste of programming and how Python works
[15:05] <rickspencer3> so that you can go off and start really learning it
[15:06] <rickspencer3> so, when I was a boy, the line between using a computer and programming it were a bit blurry
[15:06] <rickspencer3> you had a computer, and to use a program you got a listing of the program printed it out, and you typed in the source yourself
[15:07] <rickspencer3> typically using Basic (an early and pretty easy language)
[15:07] <rickspencer3> so I wrote an "adventure game" for my first program
[15:07] <rickspencer3> one of those text adventures where you go north, look in the gave, put the bird in the cage, etc...
[15:07] <rickspencer3> does everyone know what I mean?
[15:08] <rickspencer3> k
[15:08] <rickspencer3> so we'll start making one in this session
[15:08] <rickspencer3> but first, a few things about a program
[15:08] <rickspencer3> a program is just a series of instructions that you type out for the computer to follow
[15:08] <rickspencer3> computers are not smart at all
[15:09] <rickspencer3> they are just very very obedient, and they can count really fast
[15:09] <rickspencer3> this means that if you have a small error in what you type, the computer gets confused
[15:09] <rickspencer3> like if I was "seattle" instead of "Seattle" any human can cope with that error
[15:09] <rickspencer3> but to a computer, those two words are totally different, and the program will choke
[15:10] <rickspencer3> (for the record that is called "case sensitivity")
[15:10] <rickspencer3> so that's an example of how really stupid computers are
[15:10] <rickspencer3> any questions before we start working on the game?
[15:10] <rickspencer3> .
[15:10] <rickspencer3> .
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - http://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Event: Ubuntu Opportunistic Developer Week Prep - Current Session: Intro to Python for Total Beginners - Instructor: rickspencer3 || Questions in #ubuntu-classroom-chat
[15:10] <rickspencer3> ok, let's start them
[15:11] <rickspencer3> it's a good idea usually to sketch out what your program will do sometimes
[15:11] <rickspencer3> so I designed the game, and there is a picture of it on the wiki
[15:12] <rickspencer3> basically, the player has to get the fork and the bulb from the living room ...
[15:12] <rickspencer3> go to the kitchen, stick the fork in the toaster which will cause the bulb to light up ...
[15:12] <rickspencer3> so they can see in the pantry, where they win
[15:12] <rickspencer3> so let's get ready to write the program
[15:13] <rickspencer3> open up your text editor, probably gedit, and save a blank file
[15:13] <rickspencer3> I called it "prize_hunt.py"
[15:13] <rickspencer3> it doesn't matter much what you name it so long as it ends in ".py"
[15:13] <rickspencer3> (py is for python of course)
[15:13] <rickspencer3> you also need a terminal window open
[15:14] <rickspencer3> the terminal should be in the same directory where you saved the file
[15:14] <rickspencer3> I'll give folks a moment to get that set up
[15:14] <rickspencer3> let me know when I should move on
[15:14] <rickspencer3> any questions?
[15:15] <rickspencer3> ok
[15:15] <rickspencer3> let's write some code!!
[15:15] <rickspencer3> this is the fun part :)
[15:15] <rickspencer3> let's start by making the computer say things to player
[15:15] <rickspencer3> in Python we do that with a command called "print"
[15:15] <rickspencer3> it doesn't print to the printer, it prints to console
[15:15] <rickspencer3> here
[15:15] <rickspencer3> 's my first line:
[15:15] <rickspencer3> print "You are outside the front door of the house. The prize is inside."
[15:16] <rickspencer3> that's the first line of the file
[15:16] <rickspencer3> save it
[15:16] <rickspencer3> now we need to run the program
[15:16] <rickspencer3> (it's very important to save the file!)
[15:16] <rickspencer3> we run the program with the terminal by telling python to run it:
[15:17] <rickspencer3> $python prize_hunt.py
[15:17] <rickspencer3> python prize_hunt.py
[15:17] <rickspencer3> the "$" is just a convention to show you type it on the terminal
[15:17] <rickspencer3> is ti working for everyone?
[15:17] <rickspencer3> ok
[15:17] <rickspencer3> you just wrote a program!
[15:17] <rickspencer3> believe it or not, that's pretty much all there is to it
[15:18] <rickspencer3> from here on out it's just refining that basic idea
[15:18] <rickspencer3> we probably want to tell the player some  more stuff
[15:18] <rickspencer3> so let's add some more printing to our program to get started
[15:19] <rickspencer3> print "You can enter commands to do stuff"
[15:19] <rickspencer3> print "The commands are go, look, take, use, and put"
[15:19] <rickspencer3> print "Use commands with objects that you see around"
[15:19] <rickspencer3> print "For example, you can enter 'look door'"
[15:19] <rickspencer3> just add those lines and run it
[15:19] <rickspencer3> (or similar, it's your game after all_
[15:19] <rickspencer3> any questions?
[15:19] <rickspencer3> is it working?
[15:19] <rickspencer3> save and run again to make sure it's working
[15:20] <rickspencer3> ok, great
=== stevefed5291_ is now known as stevefed5291
[15:20] <rickspencer3> so let's move on
[15:20] <rickspencer3> we can tell the player stuff, so now let's let the player tell us stuff
[15:20] <rickspencer3> this will introduce two basic concepts that you will use over and over and over again in programming
[15:20] <rickspencer3> using a variable, and calling a function
[15:20] <ClassBot> hemanth asked: how to decide on the quotes?
[15:21] <rickspencer3> can you explain the question a little more?
[15:21] <rickspencer3> is this about using " versus ' ?
[15:21] <ClassBot> hemanth asked: yes "" ' '
[15:22] <rickspencer3> ok
[15:22] <rickspencer3> so ...
[15:22] <rickspencer3> a string is a collection of characters
[15:22] <rickspencer3> surrounded by either " or '
[15:22] <rickspencer3> in python yu can choose eiher
[15:22] <rickspencer3> I prefer " most of the time
[15:23] <rickspencer3> so, moving on
[15:23] <rickspencer3> let's get some user input
[15:23] <rickspencer3> if command == "look door":
=== pd is now known as Guest66583
[15:23] <rickspencer3> oops
[15:23] <rickspencer3> wrong line
[15:23] <rickspencer3> command = raw_input("Enter a command:")
[15:23] <rickspencer3> ok, that's the correct one
[15:23] <rickspencer3> add this to your program and run it
[15:24] <rickspencer3> "command" is a variable
[15:24] <rickspencer3> a variable is a word that you use in your program that is kind of a container for values
[15:24] <rickspencer3> specifically values that can *vary* depending on what the program does
[15:25] <rickspencer3> "command" is a variable that will store the results of "raw_input"
[15:25] <rickspencer3> raw_input is a function
[15:25] <rickspencer3> You "call" a function in a program. A function will do something, and it can also provide back some information. raw_input prints out a message that you tell it to print out and then collects whatever the user types. It then "returns" what the user typed. This line of code is storing what the function returns for you. Make sense?
[15:25] <rickspencer3> Some functions need some information before they can work. raw_input needs a string to display. Information that you provide to a function is called an "argument". It is usually goes inside the parenthesis.
[15:26] <rickspencer3> this is a bit to digest, but using it may help you understand
[15:26] <rickspencer3> is everyone's program running?
[15:26] <ClassBot> liemily asked: Does raw_input expect anything other than carriage return?
=== B is now known as Guest81195
[15:27] <rickspencer3> raw_input expects a string to be typed into the console
[15:27] <rickspencer3> ended with a carriage return
[15:27] <rickspencer3> but the carriage return is not added to the string
[15:27] <ClassBot> hemanth asked: why is raw_input called so? cos it's bound to any data type when read?
[15:27] <rickspencer3> raw_input will be a string
[15:27] <rickspencer3> DON
[15:27] <rickspencer3> 'T use input()
[15:28] <rickspencer3> input is weird, it tries to interpret what is given to is as Python code, so weird stuff can happen
[15:28] <rickspencer3> so you get a string from raw_input() and it's easy to work with that way
[15:29] <rickspencer3> so are folks getting raw_input to work in the console?
[15:29] <rickspencer3> shall we go ahead and actually use the provided input?
[15:29] <ClassBot> Johonunu asked: are there types of variable like int,double,... or there is single one like in php ?
[15:29] <rickspencer3> Johonunu, yes
[15:29] <rickspencer3> but you can set a variable to any type
[15:30] <rickspencer3> if you know another language already, the next session might be good, as I will
[15:30] <rickspencer3> help you make a map from your current language to Python
[15:30] <rickspencer3> this class assumes zero programming experience
[15:31] <rickspencer3> alrighty
[15:31] <rickspencer3> let's go ahead and look at the user input
[15:31] <ClassBot> m_anish asked: Can raw_input be made to accept multiple lines, and special ASCII characters
[15:31] <rickspencer3> hmmm
[15:31] <rickspencer3> m_anish, I have never done that, so I don't know
[15:31] <rickspencer3> I assume you could add "\n" to add a carriage return
[15:32] <rickspencer3> might be something fun for you to test out!
[15:32] <rickspencer3> shall we move on to using the input?
[15:32] <rickspencer3> ok
[15:33] <rickspencer3> this next block will introduce a couple of concepts
[15:33] <rickspencer3> add this to your program
[15:33] <rickspencer3> if command == "look door":
[15:33] <rickspencer3>     print "The door to the house. It looks unlocked"
[15:33] <rickspencer3> elif command == "go door":
[15:33] <rickspencer3>     print "You go through the door into the living room"
[15:33] <rickspencer3> else:
[15:33] <rickspencer3>     print "You can't do that here"
[15:33] <rickspencer3> can you guess what this done?
[15:34] <rickspencer3> . Starting with line #1, what this code says is "if command has been set to the string 'look door' then run the lines of codes that are indented below here"
[15:34] <rickspencer3> Typically, programmers will read this as "if command equals 'go door'".
[15:34] <rickspencer3> The two "=" are important. There are two because sometimes you write code that sets the value of a variable, and that uses only one "=".
[15:35] <rickspencer3> Note that line number 2 is indented exactly 4 spaces. Python uses indentation to define what blocks of code go together. So since line 2 is indented, if the users types something other than "look door" it will skip over line 2. Early on you may write lots of bugs because you get indentation wrong.
[15:35] <rickspencer3> let me know when you've tried this out, and what questions you might have
[15:36] <rickspencer3> Line 3 says "elif" to start. elif is read "else if"
[15:36] <rickspencer3> The "else" is important, because if the user did type in "look door", you just want to run the code for that condition.
[15:36] <ClassBot> virtuald_ asked: can't i use a tab for indentation?
[15:36] <rickspencer3> virtuald_ yes
[15:36] <rickspencer3> BUT don't
[15:37] <rickspencer3> it's best to set gedit to use spaces for tabs and to use four spaces for the tabs
[15:37] <rickspencer3> while the language permits other indentation schemes, the Python programmers of the world have agreed on 4 spaces for a tab
[15:37] <ClassBot> stevefed529192 asked: I know you can use tabs for indentation as well, is there any reason one would opt for spaces over tabs?
[15:37] <rickspencer3> yes
[15:37] <rickspencer3> you can't tell the difference between spaces and tabs in most editors
[15:38] <rickspencer3> if we don't all do it the same way, we will go crazy trying to read each other's code
[15:38] <ClassBot> deegee asked: is it a must to have 4 spaces of indentation?
[15:38] <rickspencer3> also, when your programs get big, you might find it's easy to read that way
[15:39] <rickspencer3> deegee, again, technically you can use any number of spaces, but your life will be easier if you go with the standard
[15:39] <rickspencer3> a lot of my early python programs I used 1 space instead of 4
[15:39] <rickspencer3> I regret it now
[15:39] <rickspencer3> ok, is this new code making sense to folks?
[15:40] <rickspencer3> great
=== jcsims is now known as Guest56648
[15:40] <rickspencer3> btw, this is called "conditional branching" and is one of the key concepts in programming
[15:40] <rickspencer3> So you may notice that whatever happens, you code ends after testing the conditions.
[15:40] <rickspencer3> oops
[15:41] <rickspencer3> not a very fun game
[15:41] <rickspencer3> Really what you want is your code to branch off and do stuff depending on what happened.
[15:41] <rickspencer3> you probably want some code to handle the situation of the user going into the living room
[15:41] <rickspencer3> They key thing to think about is that you want your code to "do something"
[15:41] <rickspencer3> Sounds like it could use a function. So you can make your own function for entering the living room.
[15:42] <rickspencer3> here's the code for a new function to put at the top of your code file:
[15:42] <rickspencer3> def enter_living_room():
[15:42] <rickspencer3>     print "You are in the living room. There is a tv cabinet, a"
[15:42] <rickspencer3>     print "table with some dishes, and a lamp."
[15:42] <rickspencer3>     command = raw_input("Enter a command:")
[15:42] <rickspencer3> this needs to go at the top for reasons that will be clear soon
[15:42] <rickspencer3> save the file and make sure your program still runs
[15:43] <rickspencer3> still working for everyone?
[15:43] <rickspencer3> You have just created a function that you can call in the same way you call raw_input.
[15:43] <rickspencer3> "def" means define, as in define a function
[15:43] <rickspencer3> Then there is the name for your function
[15:43] <rickspencer3> The empty parens mean that your function can just work, it doesn't need any information
[15:44] <rickspencer3> raw_input needs some information to work, it needs to know what you want it to display
[15:44] <rickspencer3> so it has an "argument"
[15:44] <rickspencer3> enter_living_room has no arguments
[15:44] <ClassBot> ARR asked: Whats better to learn now, python 3 or 2.6 ? I have heard python 3 is quite different.
[15:44] <rickspencer3> hmmm
[15:45] <rickspencer3> ARR I would stick with 2.6 for now
[15:45] <rickspencer3> you will need lots of samples and people to ask, etc...
[15:45] <rickspencer3> python 3 is not yet widely adopted
[15:45] <rickspencer3> also, python 3 is not supported everywhere yet
[15:45] <rickspencer3> others may have different opinions, though
[15:46] <rickspencer3> ok, so we need to call the function that we just created, shall we do that, or are there more questions now?
[15:46] <rickspencer3> btw,
[15:46] <rickspencer3> Note that lines 2-4 are indented 4 spaces. The fact that are indented under line 1, and all indented to the same level is how python knows they are part of the same function.
[15:46] <ClassBot> immy asked: what version of python do ubuntu developers use?
[15:47] <rickspencer3> aah
[15:47] <rickspencer3> well, Ubuntu developers are comprised of folks from all over
[15:47] <rickspencer3> Canonical folks tend to use 2.6
[15:47] <rickspencer3> so do most other Ubuntu developers
[15:48] <rickspencer3> let's go ahead and call the function now
[15:48] <rickspencer3> if command == "look door":
[15:48] <rickspencer3>     print "The door to the house. It looks unlocked"
[15:48] <rickspencer3> elif command == "go door":
[15:48] <rickspencer3>     print "You go through the door into the living room"
[15:48] <rickspencer3>     enter_living_room()
[15:48] <ClassBot> virkang asked: Will Lucid have Python 3 installed by default ?
[15:48] <rickspencer3> else:
[15:48] <rickspencer3>     print "You can't do that here"
[15:48] <rickspencer3> virkang, I dunno
[15:48] <rickspencer3> if so, it would be in the repos by now
[15:49] <rickspencer3> ok, does everyone see where I added the call to our new function?
[15:49] <rickspencer3> Notice that line 5 is where you call the function, and that python knows to run it if the command is "go door" because it's indented.
[15:49] <rickspencer3> The reason that you added the function to the top of the code file is to make sure that the function got defined before you tried to use it. Here's the whole file so you can see the context:
[15:49] <rickspencer3> oops, never mind
[15:49] <rickspencer3> you can see the whole listing on the wiki
[15:49] <rickspencer3> https://wiki.ubuntu.com/UbuntuOpportunisticDeveloperWeek/IntroToPythonForTotalBeginners
[15:50] <rickspencer3> so creating and calling  your own functions is a big big part of programming
[15:50] <rickspencer3> any questions?
[15:50] <rickspencer3> ok
[15:50] <rickspencer3> let's do a bit more with variables
[15:50] <rickspencer3> Let's handle looking at the lamp and getting the bulb in the living room.
[15:51] <rickspencer3> First, we want the player to see that there is a bulb that they can take.
[15:51] <rickspencer3> But if they already took the bulb, we don't want them to keep seeing the bulb right?
[15:51] <rickspencer3> So we handle this by creating a global variable
[15:51] <rickspencer3> . "global" just means that you can use the variable everywhere in the program, not just within the function where it was defined.
[15:51] <rickspencer3> Create a global variable at the top for your code file like this:
[15:51] <rickspencer3> global has_bulb
[15:52] <rickspencer3> has_bulb = False
[15:52] <rickspencer3> save your program and make sure it runs
[15:53] <rickspencer3> If you don't want the variable to be available everywhere in your program, just leave out line 1
[15:53] <rickspencer3> but in this ase we do
[15:53] <rickspencer3> , we assigned the variable to be "False"
[15:53] <rickspencer3> note that this is False, not false !
[15:53] <rickspencer3> As in, the player does not yet have the bulb, right?
[15:54] <rickspencer3> so we start out the program saying that the player does not have the bulb
[15:54] <rickspencer3> make sense?
[15:54] <rickspencer3> you don't have to declare variable first
[15:54] <rickspencer3> you just have to declare them *before* you use them
[15:54] <ClassBot> theopensourcerer asked: so you do not have to declare variables first?
[15:54] <rickspencer3> theopensourcerer, so for global variable, it's easier to put them at the top
[15:55] <rickspencer3> also, it makes it easier to read your cod a bit
[15:55] <rickspencer3> If you assign a variable to be True or False, this is called a boolean, btw. You've already seen that variables can be strings. They can be numbers as well.
[15:55] <ClassBot> Boyan23 asked: I get NameError: name 'command' is not defined?
[15:55] <rickspencer3> oops
[15:56] <rickspencer3> this sounds like you are trying to use the command variable before you've declared it
[15:56] <rickspencer3> we're a bit running out of time, but perhaps we can try to debug it later
[15:56] <rickspencer3> are other people having this same bug?
[15:56] <rickspencer3> ok
[15:57] <rickspencer3> sorry Boyan23 I promise to help you later :)
=== himurake1 is now known as himura-mobile
[15:57] <rickspencer3> so lets use has_bulb in our enter_living_room function
[15:57] <ClassBot> ragesoss asked: I must have missed this... on the wiki, line 5 is blank. Why? A blank line means calling the function?
[15:58] <rickspencer3> a blank line is just a blank line
[15:58] <rickspencer3> it is useful for making your code easier to read
[15:58] <rickspencer3> it's usually a good idea to stick a blank line in between chunks of code that are logically seperated
[15:58] <rickspencer3> so your code forms "chunks" that are easier to skim and read
[15:59] <rickspencer3> remember you will read your code 1000 times more than you will write it!
[15:59] <rickspencer3> readability is very important
[15:59] <rickspencer3> ok, where was I
[15:59] <rickspencer3> right
[15:59] <rickspencer3>     command = raw_input("Enter a command:")
[15:59] <rickspencer3>     if command == "look lamp":
[15:59] <rickspencer3>         print "It is a floor lamp. It is not plugged in."
[15:59] <rickspencer3>         if not has_bulb:
[15:59] <rickspencer3>             print "There bulb looks new"
[15:59] <rickspencer3>         enter_living_room()
=== Christian is now known as Guest96614
[16:00] <ClassBot> ragesoss asked: thanks.  for some reason, enter_living_room wouldn't run before I added that space.  But I guess that wasn't the cause, and I'm not sure what was.
[16:00] <rickspencer3> so here I use the has bulb variable to make sure I only print the info about the bulb if the player didn't take it yet
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - http://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Event: Ubuntu Opportunistic Developer Week Prep - Current Session: Intro to Python for programmers - Instructor: rickspencer3 || Questions in #ubuntu-classroom-chat
[16:00] <rickspencer3> BUT
[16:00] <rickspencer3> (almost done, last thing)
[16:01] <rickspencer3> if you want to assign a value to has_bulb, you'll need to tell the enter_living_room() function about it
[16:01] <rickspencer3> def enter_living_room():
[16:01] <rickspencer3>     global has_bulb
[16:01] <rickspencer3>     print "You are in the living room. There is a tv cabinet, a"
[16:01] <rickspencer3>     print "table with some dishes, and a lamp."
[16:01] <rickspencer3>     command = raw_input("Enter a command:")
[16:01] <rickspencer3>     if command == "look lamp":
[16:01] <rickspencer3>         print "It is a floor lamp. It is not plugged in."
[16:01] <rickspencer3>         if not has_bulb:
[16:01] <rickspencer3>             print "There bulb looks new"
[16:01] <rickspencer3>         enter_living_room()
[16:01] <rickspencer3>     if command == "take bulb":
[16:01] <rickspencer3>         print "You now have a bulb"
[16:01] <rickspencer3>         has_bulb = True
[16:01] <rickspencer3>         enter_living_room()
[16:01] <rickspencer3> so notice I redeclared has_bulb as a global within the "scope" of the function
[16:01] <rickspencer3> also!
[16:01] <rickspencer3> notice that I called the function from within itself!!
[16:01] <rickspencer3> pretty cool, a function can call itself
[16:02] <rickspencer3> this keeps the program from exiting
[16:02] <rickspencer3> so we're two minutes over
[16:02] <rickspencer3> how about if you want to, you can try working on the game today/tonight, and then tomorrow if you have questions ...
[16:02] <rickspencer3> track me down and I can try to help
[16:02] <rickspencer3> maybe set up a follow up session for next week if folks want
[16:03] <rickspencer3> Ready for the next class?
[16:04] <ClassBot> shift_wreck asked: could you pastebin the code?
[16:04] <rickspencer3> shift_wreck, (nice nick btw) -  sure if the wiki doesn't work for you
[16:04] <rickspencer3> but doing another class now

MeetingLogs/OpWeek1002/Python4Beginners (last edited 2010-03-01 19:39:30 by port-92-194-233-237)