SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-06-02 11:22:59

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Baby's First Programming, (In Python)

Introduction

Well, my projects have really dwindled down recently.  I'm about to be done with the classes I've been taking this semester, and my son's baseball season has finally come to an end.  I'll have a bit more time to play around.  There's been a few people show an active interest in Python programming recently, so I thought we'd at least give this a go if Corenominal/VastOne/pvsage/LCafiero/etc. are cool with it.

I figure that everyone who'd like to start programming should begin somewhere, and I should be able to put up some decent info and take a Q&A about programming in Python if you guys would like.  Since we're all running Linux (or should be, I mean...this is a Crunchbang LINUX Distro forum), then setting up everything will be a piece of cake.

If this is something that people would  be interested in, we'll start from ground zero and work our way forward into drastically more difficult territory (hopefully dominating full-featured GUI object-oriented-programming) by the time we're done.  I'm certainly not going to pretend that I'm the best programmer who's ever lived, but I think that I could at least help those that were into it reach levels of being mildly dangerous. wink

So, if you'd be up for this, then speak up!  Otherwise, I'm going back to trying to finish out my dungeon crawler. big_smile

Offline

Be excellent to each other!

#2 2013-06-02 11:50:37

joek
#! Junkie
Registered: 2011-09-06
Posts: 497

Re: Baby's First Programming, (In Python)

Well, we wouldn't want to distract you from your dungeon crawler tongue
But if you're posting this I shall read and hope to pick up some stuff I didn't know. My knowledge of python is by no means systematic...

Offline

#3 2013-06-02 13:11:09

ItsAllGoneWierd
Member
From: Norway
Registered: 2013-05-06
Posts: 32

Re: Baby's First Programming, (In Python)

I am up for this!
Sounds like alot of fun!

Offline

#4 2013-06-02 13:27:02

bodek
Member
From: Limuru, Kenya
Registered: 2010-07-13
Posts: 29

Re: Baby's First Programming, (In Python)

Count me in  big_smile
When shall we begin?

Offline

#5 2013-06-02 13:43:28

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

Class is in Session: Hello World

I personally believe that I should have no difficulty in managing to keep up with both my no-X project and this once I'm done with this semester's classes.  Since a few people have shown an interest in learning, I'd be glad to share what little bit I know.  If for some reason someone has anything to add, subtract, correct, etc.  Then by all means, point out where I'm wrong.

Step 1: We need an editor.  I will not shove my preferences for a text editor down anyone's throat, and if I did the first two weeks would be nothing but teaching key-chain-commands.  So, pick an editor that has the ability to color up your coding once it knows what type of program is being written.  Good choices would be: Gedit, Geany, Medit, vim, Sublime Text, or emacs.  I'm sure there's a lot of other great choices out there, because if there's one thing we have plenty of in the Linux world, it's good text editors.

Step 2: We need a compiler.  This is even easier in Linux than finding a text editor.  99.5% of you will have a Python compiler already.  The only weirdness we'll see is if you've got Python3 set up, because we're going to be using 2...like most of the world still uses.  Just for kicks, you should be able to open a terminal and type "python" or "python2" and get your release information.  This also puts us into an interactive python terminal.  Use Ctrl^d to get out of the shell.  This is a great way to try out a single line of code to see if it works.  If you don't have it already:

sudo apt-get install python2.7

Should fix that if you're running #! or any Debian derivative.

That's all it takes to write basic python.  A text editor, a terminal that can run python, and coffee (I forgot that step, but you MUST drink lots of coffee or else you'll never accomplish anything during troubleshooting.)  So, assuming that we have this all set up and working, let's go back to our terminal.

In the terminal, type python to bring up our interactive shell.  Now, in the shell...type this in exactly.

print "Hello World!"

If all has gone according to plan, you now have a line below your original line of code.  I strongly considered breaking tradition and having you type out some gangster rap lyrics, but I just couldn't bring myself to do it...or Google gangster rap.  Now, that's pretty simple, and not really very impressive.  Let's try it again, but a little bit differently.

Open up your text editor and let's set it up.  First off, if it has the option to set Tab Width, set it to "4" because I like 4-space indentation.  Since Python uses forced indentation (we'll get into this later), you don't want it spacing over 8 spaces every time you hit the tab button.  Most editors will automatically color our scripts correctly if we save our files with a ".py" extension.  So, open up a blank document in your text editor and let's save this project as "hello.py".

Now, we just need to put something in our program.  Hmmm......let's keep this "traditional" thing going.  Type this in exactly:

print "Hello World"
print 'this is my first python program'
print 'well, it may not be my "first" python'
print "but it's a pretty good one."

Now, save our program again.  Man, python is a really great language to let us get away with such simple statements.  "Hello World" can be so much harder than just typing print and then putting in quotations what we want to print.

After we have saved our changes, go back to our terminal and navigate to the directory that you saved the file in.  (I'll be using a "python" folder for all of this, and if you want to set one of those up from the terminal we can use "mkdir python" and then "cd python" to navigate to it.  Now once we've successfully navigated to our python folder, in the terminal type

python hello.py

If all goes as expected, we should be greeted by the stuff that we'd typed in our hello.py file.  If you were paying attention, you'll notice that I used BOTH single quotations ( ' ' ' ' ' ) and double quotations (" " " " ") in the above code.  Either will work, and I have made sure to include some situations where we'd want to use both.  In the case of the word "it's" above, if you start that print statement with a single quotation, you'll end up with it closing the line out too soon.  The easiest way to get around this is to alter our quotation marks to fit the situation.  (In a later discussion, we'll cover some much more interesting ways...but that comes later.)

So, if we did this right, it wasn't that much fun.  For study until later, remove the quotation from the end of the last line and run your program again.  What does it say in the terminal?  We'll break things after we get them to work so that when things get more complicated, we can use these little hints to troubleshoot.  Also, just for fun, put an  '#' (octothorpe, or "crunch" just for the sake of the community) at the beginning of one of the lines.  What did that do?

That's the Super-Simple Basics of Python.  If you have trouble with anything from today, just ask and I'll see if we can't help you get it straightened out.

Last edited by DebianJoe (2013-06-02 13:45:34)

Offline

#6 2013-06-02 16:57:02

wuxmedia
wookiee madclaw
From: Back in Blighty
Registered: 2012-03-09
Posts: 1,478
Website

Re: Baby's First Programming, (In Python)

that was baby step-tastic!
thanks, ( i think i might cheat a bit and have a look at the python link you put up in another thread)  big_smile

Offline

#7 2013-06-02 17:12:58

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

Class 2:  Comments.

I figure that the first lesson was REALLY easy, as well it should be.  You must crawl before you walk before you run.  On the other hand, I don't want to leave the first day of posting alone there.

So, if anyone slapped the old "crunch" or "octothorpe" or "pound-sign" or whatever your country refers to it as, you already know how to comment out a line, but there are a few caveats to this rule.  Let's make a "comment.py" program.  We'll start it off with some interesting uses of the octothorpe.

#!/usr/bin/env python

#This is a comment!
print " Do you see how that works?"
#print "This lines doesn't get printed"
print "But what happens with the # that's in this line?"
print "Oh, so that's what it does!" #but what about this one?

I won't ruin the fun, so type it up and run it.  You'll notice that this is where a coloring editor works really well at showing you what the compiler will ignore.  Comments make reading your code much easier if they're used correctly, but please don't be the guy/girl who writes an epic novel about how their "for loop" works.  Where they're really useful is for where you want to remember how a function was designed to work, or if you're getting very abstract, how the math of an extended equation works.

But what about that old shebang at the beginning (that's a "#!" or we can call it the "crunchbang" if you'd like. wink )?  Well, if you've used bash before, then this one is obvious.  The call at the beginning allows us to define what environment we want our script or program interpreted in.  So, let's say we've saved our file as "comment.py".  We COULD remove the ".py" from the end now, and then set this script to be executable.  To do that, we need to tell Linux that it's cool for us to run this.

chmod +x comment
./comment

You can leave the .py on there if you want.  It's not a bad idea anyhow.  The point is that the #!/usr/bin/env python is our call to let the system know what we want done with our script.  In this case, we want to let our system know that we're using a Python script.  Why?  In Unix, an executable file that's meant to be interpreted must indicate what interpreter to use by having a #! at the start of the first line, followed by the interpreter (and any flags it may need); otherwise, the default is /bin/sh.

So, comments....are a boring subject.  In the process of writing a program, if I have a part of an idea sketched out, I'll throw "#" in front of every line so that I can run the rest of it without having to deal with the parts that I know don't work yet.  They're good for that too.

Last edited by DebianJoe (2013-06-02 17:17:09)

Offline

#8 2013-06-02 17:32:43

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

Go ahead and cheat by reading ahead and learn more all you want. big_smile  We'll get to everything covered there and quite a bit beyond if you guys hang with me.  If I'm going TOO slow, though, let me know.  I'll pick up the pace if everyone is comfortable so far.

EXTRA CREDIT. (Since Wux isn't happy with how slow I'm going)

The big advantage of computers is that they're capable of doing math very accurately at an extreme rate.  In the next little bit, I'm going to use some fun terms for computer mathematics to explain different numerical formats. 

Int, Float, Bit, Long/Double, etc. are all different ways to format numbers.  In industrial electronics, a memory word (16-bit binary) are often used to hold decimal values in relay format.  If a larger word is needed, then Longs or Doubles (which are two words put together 32-bit binary numbers).  Since these are a pain to figure out by seeing "ML3450 == 1634" just to determine what relays or bits are on, I wrote this fun script to just tell you which ones are on for any given potential value for doubles:

#!/usr/bin/env python
print "\n\n"
word_n = int(raw_input("\033[1;31mWhat is your memory word's number?\033[1;m "))
word_v = int(raw_input("\033[1;32mWhat is the value of the word? \033[1;m"))
v = 0
getBin = lambda x: x >= 0 and str(bin(x))[2:] or "-" + str(bin(x))[3:]
total = (getBin(int(word_v)))
print "\n" + total + "\n"
lst=[int(i) for i in str(total)]
lst.reverse()

if word_v < 65536:
	for bit in lst:
		if lst[v] == 1:
			print "\t\t\033[1;36mMB" + str(word_n) + format(v, 'x') + " is ON\033[1;m"
			v += 1
		else:
			v += 1
else:
	for bit in lst:
		if lst[v] == 1:
			if v < 16:
				print "\t\t\033[1;36mMB" + str(word_n) + format(v, 'x') + " is ON\033[1;m"	
			else:
				print "\t\t\033[1;36mMB" + str((word_n)+1) + format(v-16, 'x') + " is ON\033[1;m"
			v += 1
		else:
			v += 1

This way, you could just enter in the Long's address, and what it's decimal value is...and out pops what bits are on in it.  (If you aren't following, don't worry...this is for my overachievers.)

Offline

#9 2013-06-02 17:55:26

ItsAllGoneWierd
Member
From: Norway
Registered: 2013-05-06
Posts: 32

Re: Baby's First Programming, (In Python)

Very nice this!

I like the way you write and explain. Even if this is stuff I already know, its good to get it confirmed and explained in new words. And I think you have added relevant information in a fun way!

Looking forward to learn more!

Offline

#10 2013-06-02 18:04:32

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

Class 3: Math

I have a good bit of time on my hands today, so I'm going to keep going with this for now.  There will probably be days where I don't post at all and I don't want to bore everyone to death who's actually interested in learning.

So, let's make a SUPER FUN MEGA COOL MATH PYTHON PROGRAM.

print "I will now put my swagger in numerical value:"

print "I have 30 plus 30 divided by 5 swaggers", 30 + 30 / 5
print "that means 100 minus 25 times 3 modulo 4 ladies be on me at the club", 100 - 25 * 3 % 4

print "I had 3 dranks, then 2 dranks and another and lost 5 dranks:"
print "I added 4 modulo 2 dranks then spilled a quarter of those...then had 6 more"

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print "Does Wuxmedia got mo' swagger than me?"
print "well, is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 - 7

print "How about some more boolean checks?"

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

print "So that's like 4 to the 3rd power levels of math", 4**3

Most of that should be pretty simple, except perhaps "modulo" which is a neat way to handle "The Remainder of x/y."  If I was going to have something happen on ONLY even numbers, I could use a statement that was only executed when: x%2==0...or that when x was divided by 2, there was no remainder.  If you bring up that Python shell we were looking at earlier, you can enter in an equation, and it will simply spit the output back out at you.  Also note:  Python uses the same order of operations that you'd use in traditional mathematics.  You will get exponents and roots first, multiplication and division next (which includes modulus operations), and finally addition and subtraction.  Assuming that you want it to handle an addition first, then just like in high-school algebra, you put parenthesis around it.

(5+2+3) / 5

The above function first takes the numbers in parenthesis and adds them, and then divides afterwards.  This is a really simple concept, but in much more complex programming, these are the little things that will drive you nuts.

You may also notice that when we have division by numbers that do NOT divide out evenly, that there seems to be something wrong.  Well, by our choice of input, we're telling them to be treated as integers.  Open your Python shell and enter 13/4...then enter 13.0/4.0.  Do you see a difference?

Last edited by DebianJoe (2013-06-02 18:05:28)

Offline

#11 2013-06-02 18:07:12

dkeg
#! Die Hard
From: Mid-Atlantic Grill
Registered: 2011-12-05
Posts: 727

Re: Baby's First Programming, (In Python)

holy crap!  I go and paint my kitchen and look what I miss!   class just started and I'm already behind.  Gotta catch up


grill it | the rocky path may just be the best path

Offline

#12 2013-06-02 20:04:42

kbmonkey
#! Die Hard
From: South Africa
Registered: 2011-01-14
Posts: 879
Website

Re: Baby's First Programming, (In Python)

Ooh fun! I'm in. *fires up bpython*

Offline

#13 2013-06-03 04:53:41

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

I've already lied to you guys for the sake of education to see if someone would correct me.  "Muh hwa ha ha ha" - Mad Scientist Laugh

The modulus function "%" isn't EXACTLY a remainder function.  (It's not percentage, ever, though.)  It does behave like a remainder function until you start dealing with negatives.  Try this out in your python shell.

9 % 12
-3 % 12

What is that all about?  Well, if we were going to to explain it with pure math, it would be something like (x%y == x-[x/y]*y) which is as accurate as I'm capable of being.  Now, this may not really make any sense at all at first.  What I want you to do is to look at a 12 hour clock.  This is the explanation that shows reasonable application to negatives in modulus functions.

First, how many hours is it from 12 noon to 9 pm?  That's correct...9!  So, it stands to reason that if we take 3 away from 12, we'll end back up at 9.  So, that's why -3 % 12 == 9.  This is a very useful function for finding objects that are set in a circular pattern, so I want to be sure that we don't miss out on the specifics of the behavior of this simple little function.

Offline

#14 2013-06-03 07:46:33

wuxmedia
wookiee madclaw
From: Back in Blighty
Registered: 2012-03-09
Posts: 1,478
Website

Re: Baby's First Programming, (In Python)

*brain explodes*

Offline

#15 2013-06-03 07:58:02

zalew
#! Junkie
From: Warsaw, .PL
Registered: 2012-03-28
Posts: 374

Re: Baby's First Programming, (In Python)

worth checking out: http://learnpythonthehardway.org/

Offline

#16 2013-06-03 12:46:12

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

zalew wrote:

Yes, yes it is.  Zed Shaw is one of my personal favorites, and I'll be pretty much following the exact same progression he uses at first.  It's the right way to learn, IMHO.

For the first little bit, I'll follow his layout for learning the basics of Python because it's probably the best way to learn.  I'll perhaps devolve into some deeper concepts along the way, but not by much.  If you're wanting to get ahead, though, that's certainly an excellent plan.  There's nothing there that I won't also cover, and we're hopefully going to go a LOT further into what you can do with it.  Until then...please learn as much as is possible.  It might make a lot more sense in one person's explanation than another's.

Last edited by DebianJoe (2013-06-03 13:19:15)

Offline

#17 2013-06-03 13:58:30

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

Variables

Variables are going to eventually be so overused and confusing that you'll all grow to hate them...except you'll love them because programming without them is drastically more difficult.  Here, and hopefully everywhere, I'll do my best to stick to a PEP 8 naming convention for variables.  Read more here.

These conventions make reading code much easier for everyone.  I "may" have broken conventions before in my own work, but I assure you...it was accidental.  Let's play with variables a bit:

dogs = 50
cats = 60
silly_animals = cats + dogs

print "We have", cats, "feline friends."
print "We have", dogs, "canine buddies."
print "In all, we should have", silly_animals, "four-legged companions."
print cats + dogs
print str(cats) + str(dogs), "this is cats and dogs in STRING format."
print int(cats) + int(dogs), "this is cats and dogs in INTEGER format."
print float(cats) + float(dogs), "this is cats and dogs in FLOAT format."
print "So, can we NOT use comma's?" + str(cats) + str(dogs)

Save this as a file and run it.  I think that my uses should be pretty self-explanatory.  By throwing in the "int(foo_varable)" format, we can call them in whatever format we want to use them as.  In this case, we've used strings, integers, and floats.  Pay special attention to the fact that "str(8) + str(9) == 89" and not 17.  All we're doing here is shoving the two strings next to each other, and not actually performing any math at all with them.

Also, in the last line, I use the "+" sign to shove the string that I've started the sentence with against the call to the variables.  We can use little tricks like this to allow long lines of text to follow normal line-length conventions.  Since PEP 8 informs us that we should limit all lines to 79 characters, we may need to use little tricks to prevent a line that would technically be longer than 80 characters to be broken into smaller sections and then assembled later on.  Also, note that I divide the variable calls in my program from the calls to print with a single blank line.  This is also part of the convention to help make sections of code more readable.

Now, it will still work if you DON'T do this, but you not only make things harder on yourself, but you also make it much harder for someone else coming behind you to be able to figure out what you're doing.  In the world of open and free software, many times other people will be picking up your work and adding to it.  Keep them in mind.

Offline

#18 2013-06-03 17:54:55

ItsAllGoneWierd
Member
From: Norway
Registered: 2013-05-06
Posts: 32

Re: Baby's First Programming, (In Python)

Ok, I have a question about variables.

What is the difference:

What you did...

and

cat = 60
dogs = 50
monkeys = 1

print '%s, %s and %s' % (cat, dogs, monkeys)

and

cat = '60'
dogs = '50'
monkeys = '1'

print ('{0}, {1} and {2}' .format(cat, dogs, monkeys))

This programming school of yours is a great idea! Are you a teacher or something? Very good reading *thumbs up*

Offline

#19 2013-06-03 19:53:37

kbmonkey
#! Die Hard
From: South Africa
Registered: 2011-01-14
Posts: 879
Website

Re: Baby's First Programming, (In Python)

I concur with ItsAllGoneWierd it's fun reading material smile

I'd like to add that you can use multiplication on strings too:

>>> '#' * 4
'####'

Coming back to your modulo example, you can visually represent what modulo does by multiplying the result:

>>> l = range(1, 20)
>>> for i in l:
...     print('#' * (i % 4))
...     
... 
#
##
###

#
##
###

#
##
###

#
##
###

#
##
###
>>> for i in l:
...     print('#' * (i % 10))
...     
... 
#
##
###
####
#####
######
#######
########
#########

#
##
###
####
#####
######
#######
########
#########

Offline

#20 2013-06-03 21:02:37

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

@itsallgoneweird, in myexample above, cats and dogs are treated as integers for the most part, but I assume you're talking about the string section. 

The big difference is that both of your provided examples are calling the string class using positional arguments.  '{}, {}, {}'.format('a', 'b', 'c') didn't even exist as an option before Python 2.7 I don't believe.  All 3 options are a form of calling a string.  The %s, %d, %r, %f, etc. tuple substitution method is great as long as it remains clear what you're trying to do with it.  It's especially useful when calling from a list or argument vector.  As I mentioned, though, these substitutions are tuples, and can't be mutated directly.  Plus, I like keeping the formatting next to the variable.  The %s method is borrowed from "C" formatting, and is useful to know as well.

Read more here.  ...and No, I'm not a teacher.  I'm a "coding problem solver" by trade.  (Can you call hacking someone else's work to change functionality a career?  I hope so, because that's the most honest I can be with a job description.)  I tend to value getting things done over the particular methods used.

@kbmonkey:  Cool example. big_smile

Offline

#21 2013-06-03 21:11:42

wuxmedia
wookiee madclaw
From: Back in Blighty
Registered: 2012-03-09
Posts: 1,478
Website

Re: Baby's First Programming, (In Python)

ok brain re-assembled, that substitution looks tricky.
enjoying it 8)

Offline

#22 2013-06-04 04:26:03

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Baby's First Programming, (In Python)

Hey Joe - great idea! A chance to finally get my teeth into Python a bit. Bash is lots of fun, but maybe it's time...

Please don't go too fast though, yikes I've missed all the first 3 or 4 lessons and will have to catch up, later this evening maybe.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

#23 2013-06-04 06:15:13

CaptainIndifferent
#! CrunchBanger
From: The Vale of Si
Registered: 2012-05-05
Posts: 202

Re: Baby's First Programming, (In Python)

Hi Joe, great idea. Thanks for doing this.

I'm just learning Python myself through a combination of The Hard Way and the Coursera / Rice University intro class so I will be following along and trying to keep up.


Just because I don't care doesn't mean I don't understand.

Offline

#24 2013-06-04 06:40:14

savalaserg
#! Member
Registered: 2013-05-15
Posts: 91

Re: Baby's First Programming, (In Python)

Hey Ill jump in ! DebianJoe actually recommended me to read this and ive been learning alot I think im on step 5 or 6 now using variables! Great stuff Ive written a couple basic programs so far! never wrote a program in my life lol !

Offline

Help fund CrunchBang, donate to the project!

#25 2013-06-04 06:45:40

DebianJoe
#! Code Whisperer
From: The Bleeding Edge
Registered: 2013-03-13
Posts: 1,207
Website

Re: Baby's First Programming, (In Python)

@johnraff.  I'm going to try to keep the progress to where someone picking it up for the very first time, even if it was a first language, would be able to follow without too much difficulty.  There may be some things in the answers to questions that not everyone will get on the first reading.  Don't stress about that, it will all come together.

A Short Discussion on Style:

There is a saying, "There's more than one way to skin a cat."  I have no idea where this saying originated, as I've never been in a situation where skinning a cat would be necessary.  What it essentially means is that there are often times a lot of ways to do the same thing.  ItsAllGoneWierd pointed out that we can get the same final result in a few different ways with string formatting.  The reason to choose one over the other is often dependent on what you need out of them, and what's the easiest to understand.  I learned the substitution methods first, and honestly don't use them very often now.  The reason being is when you start to tie huge "sentences" of variables together, they make more sense to me in a linear fashion than trying to remember which substitutions I should be using at the end of a line.

my_spoons = 350
my_forks = 154
my_knives = 167

print "If I add %d, %d, and %d I get %d." % (
    my_spoons, my_forks, my_knives, my_spoons + my_forks + my_knives)

Here, I'm using the %d (decimal substitution) to add three sections.  This is pretty much the same method that is pointed out with strings above.  There is no reason that this method isn't just as acceptable as the one used from my other example.  The only reason I tend to shy away from it is the way that the last line looks.  It can get confusing when doing very complicated equations.

Pick the one that suits the situation.

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo