SEARCH

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

You are not logged in.

#26 2013-06-04 08:14:09

otak
#! Member
Registered: 2013-01-01
Posts: 51

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

Hi Joe.  Very timely thread as it seems Zed has stopped having a comments/help section on his pages.  I worked through to the point where he said now go back and go through everything we've learnt so far.  So here I am!

vim
    If anyone wants or needs to use a non-X editor here is how I set up my vim.
    Most distros come with vim-tiny so you need to install the vim or vim-basic package.
    If that is working you can test your settings in vim at the command prompt eg.

: syntax enable

    Once you've found the settings you like you can put them in ~/.vimrc.  Here's mine:

syntax enable
set background=dark
set autoindent

autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4

style
    Amazingly Pep 8 seems to agree very well with the examples in Kernighan and Ritchie!  (not boasting, I'm only on chapter one)

attitude
    The very first part of Zed's guide I think really set me off in the right direction.
    eg.

zed shaw wrote:

The one skill that separates bad programmers from good programmers is attention to detail. In fact, it's what separates the good from the bad in any profession.

    I really recommend anyone to read this in a quiet moment.

    Well thanks Joe and good luck everyone smile

Offline

Help fund CrunchBang, donate to the project!

#27 2013-06-04 08:47:36

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

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

otak wrote:

Hi Joe.  Very timely thread as it seems Zed has stopped having a comments/help section on his pages.

We should all take a moment of silence for this.  Zed has done more this decade through his own work and the links at Programming MotherF*****r.com for freely giving out the programming skills necessary to get started than anyone else that I can think of.

Plus, he uses Mutt for e-mail, so he's okay in my book. wink

Offline

#28 2013-06-04 09:16:35

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

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

in case someone doesn't want to learn and set up vim, I can recommend a nice and simple python ide I tried out recently http://ninja-ide.org/

Offline

#29 2013-06-04 09:25:14

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

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

zalew wrote:

in case someone doesn't want to learn and set up vim, I can recommend a nice and simple python ide I tried out recently http://ninja-ide.org/

Thats pretty nice thanks man better than using geany ... but geany is nice heh..

Offline

#30 2013-06-04 09:54:37

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

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

wow, there really is a site called programmingmofo  yikes
how did ICANN let that go?

Offline

#31 2013-06-04 10:00:35

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

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

swearing is not a crime.

just in case, it's the one with the dash http://programming-motherfucker.com/

Last edited by zalew (2013-06-04 10:01:07)

Offline

#32 2013-06-04 10:23:30

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

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

Boolean Logic

We've already looked at some pretty basic examples of this.  It's essentially just relay logic put into word terms.  Is it ON or OFF?  In python, these get "True" and "False" values.  You can use variables, especially global variables, to set up a "switching system" for keeping up with the state of your program.  This is especially useful for programs that don't execute in a linear fashion.  This is an easy concept, right?  a==True, or a==False?  This can actually really help us when we start to look at how python evaluates more complex statements.  This is one of those issues that I was speaking of where what looks simple at the onset can really take a lot of time during troubleshooting to determine where things might not have gone exactly as expected:

a = True
b = False
c = True
d = False

print "First We print a, and then b"
print a 
print b

print "############"

print "Then, we evaluate NOT a"
print not a
print "Then a AND b"
print a and b
print "Then a OR b"
print a or b
print "...this gets harder with (a AND b) or a"
print (a and b) or a
print "############"
print "Or complex compares such as..."
print "(a and b) or (c and (not d))"
print (a and b) or (c and (not d)) #complex compares
print "These are difficult to figure out mentally"
print "############"

#comparison operators
# > < >= <= == !=
s = 7 > 3
print s

x = 8
y = 5
h = x >= y
print h

f = "hello" == "hello" #string compare
print f
print "##############"

g = 20.6 <= 18.3 #can use floats, integers, strings, etc.
print g

Notice that "=" assigns that (foo IS EQUAL to foo2) where "==" is used to check (IS foo equal to foo2).  For those of you totally new to programming, in the comments in the above program, I included a list for comparison operators.  These are good for setting up checks during boolean operations.

Also, "foo" which I'll use all of the time is a generic term to mean "any possible variable."  I often forget that this may not mean anything to non-programmers, but it's a common term used by people who code.

Offline

#33 2013-06-04 14:59:58

ze-hobbit
Member
Registered: 2009-07-09
Posts: 13
Website

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

One more for the ride!

Offline

#34 2013-06-04 15:03:16

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

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

Thanks for info about ninja-ide great program ! Makes writing alot easier. Hey DebianJoe you think I should go ahead and continue using this tool or go back to geany to get more hands on ?

Offline

#35 2013-06-04 15:13:16

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

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

I think that IDE's have their place.  You'll be far more careful using a text editor, but IDEs can work too.

I use emacs...so, uhh, take that for what it's worth. wink

Offline

#36 2013-06-04 15:25:50

kiiroitori
#! CrunchBanger
From: Tokyo, Japan
Registered: 2010-12-07
Posts: 229

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

Aaah I just found this thread. Sorry I'm late professor! Will do my best to catch up and be a good student.


I love #! more than my own kids. I told them and they sympathized.

Offline

#37 2013-06-04 16:13:54

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

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

Well fellas off to sleep I work the night shift. Gotta remind myself to bookmark the boolean logic class pretty straightforward...  I am picking up more and more I think I could practice on variables and stringing things together more. So far I love the class DebianJoe might have to slow it down a bit for me since this is my first programming language ever learned lol.

Offline

#38 2013-06-04 21:30:25

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

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

DebianJoe wrote:

Also, "foo" which I'll use all of the time is a generic term to mean "any possible variable."  I often forget that this may not mean anything to non-programmers, but it's a common term used by people who code.

Sometimes you will get some spam and eggs too smile

Thanks DebianJoe, these are great.

Offline

#39 2013-06-05 04:32:48

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

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

kbmonkey wrote:

Sometimes you will get some spam and eggs too smile

I use those when I'm working on making a shubbery. wink

Offline

#40 2013-06-05 05:07:29

user77
#! CrunchBanger
From: Dirty Denver
Registered: 2012-01-20
Posts: 161

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

I'm in. Been trying to get into python off and on lately.

Offline

#41 2013-06-05 05:08:48

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

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

Well jump in buddy ! DebianJoe is doing a great job just finishing up his boolean logic class above.

Offline

#42 2013-06-05 06:00:32

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

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

String Hell and String Manipulation

Everyone likes strings, because we're creatures of language and speaking comes naturally to most of us.  (I have noted that we're Linux users, so we're going to include internet speech with "speaking" as there's the age old adage that we're all actually located in dark basements around the world and only come out to feed.)

Anyhow, we've been looking at how you can use substitution methods or linear methods for placing string variables, decimal variable, etc. into raw output.  Let's continue to look at how we can bend strings to our will.  After a bit of practice with this, you'll be a full-fledged "stringmancer."

i = "I"
n = "!"
a = "%s am" % i
m = "stringmancer"
x = "%s a %s" % (a, m)

print x + n

print x[7:13] + x[-1:-2] + n
print i[0]
print a[2:4]
print x[7:17] + x[10:13] + n

Whoa!  We're now summoning strings and sections of strings to do our bidding.  I want you to really absorb what's going on here, because in this example this is just fun.  The implications, though, are pretty powerful.  What we're doing is first using substitutions to build a fun little string.  Then, once we've constructed this string, we begin to treat it more like how the computer sees it.  It's not a word so much as an array of characters that are stuck together.  This allows us the ability to grab individual characters or ranges of characters and put them where we want them to go.

Notes on ranges:

Counting in most cases with computer programming, and certainly here: START WITH ZERO.  Your kindergarten teacher screwed you all up by trying to get you to start with one, so we're going to fix that.  In the word "DOG" the d is at position [0]. 

When you're using ranges in python, the [2:14] tells it to grab from position 2 to but not including 14.  This is important, and will show up again in future discussions.

Position [-1] means the last character in the string, array, list, whatever else we may use in the future.  [-2] means the next to last one, and this holds for further progression.

Now, take this and play with it some to get a feel for how ranges and array slicing works.  If you're looking for some practice, write a few short sentences and use these methods to rearrange them into something else.  For bonus point, make sure that they're formatted correctly.

Edit: I also wanted to point out that in the substitutions, the spaces are kept in place in the sentence.  Spaces ARE characters.  If you're using concatenation (that's the "+" that we use to shove strings together) you'll notice that there isn't a space.

Last edited by DebianJoe (2013-06-05 10:50:49)

Offline

#43 2013-06-05 06:26:43

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

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

Extra Stringmancing for the lazy.

A function that's often used in some interesting ways is really for those stringmancers among us who don't like to count.  I thought I'd throw this one in for those who were unfamiliar with it, as it can be used to quickly give a numerical value to the amount of objects in a string (also tuples, lists, dictionary, but I'm getting ahead of myself here.)

i = ("Foolish string, bow unto your master and tell me," +
	" how many characters are you currently using?")
	
print i
print len(i)

Offline

#44 2013-06-05 06:27:11

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

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

Whoa.... mind blown lol I get some of it but the two big ones im lost at are...
print x[7:13] + x[-1:-2] + n
print x[7:17] + x[10:13] + n
I am not really getting how that is all coming together I see to a point but if you could break it on down for me a little better.

Offline

#45 2013-06-05 07:09:54

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

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

DebianJoe wrote:
i = ("Foolish string, bow unto your master and tell me," +
	" how many characters are you currently using?")	

why the parenthesis and plus?

Offline

#46 2013-06-05 07:20:39

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

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

zalew wrote:
DebianJoe wrote:
i = ("Foolish string, bow unto your master and tell me," +
	" how many characters are you currently using?")	

why the parenthesis and plus?

agreed

Offline

#47 2013-06-05 07:41:00

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

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

@savalaserg.  Those two lines are chopping a string we've already created.

x = "I am a stringmancer"

So x[7:13] pulls from this x-string "I am a (string)mancer"  The x[-1:-2] isn't doing jack, and was intended to make questions arise because it's ordered incorrectly (I'm asking it to index backwards from left to right....which is totally wrong.  if you put the -2 first, things change to actually work; we'll get there later).  If you remove the ":-2" from the array, it will actually give you the last digit.  big_smile  The "+n" pulls our "!" back into the string statement.

print x[7:17] + x[10:13] + n Takes our string and grabs the I am a (str{ing}manc)er out and reorders it (stringmanc){ing}.  Ask if this doesn't make sense.

Offline

#48 2013-06-05 07:50:21

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

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

Would

print 'Foolish string, bow unto your master and tell me ' \
          'how many characters you are currently using.'

Be different?

Edit: Other than the fact that I have it print as opposed to assigning it to a variable. (I actually didn't mean to do that  yikes  )

Last edited by DebianJoe (2013-06-05 07:59:18)

Offline

#49 2013-06-05 07:55:54

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

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

DebianJoe wrote:

Would

print 'Foolish string, bow unto your master and tell me ' \
          'how many characters you are currently using.'

Be different?

import this

explains it all wink

Offline

Be excellent to each other!

#50 2013-06-05 08:07:11

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

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

DebianJoe wrote:

@savalaserg.  Those two lines are chopping a string we've already created.

x = "I am a stringmancer"

So x[7:13] pulls from this x-string "I am a (string)mancer"  The x[-1:-2] isn't doing jack, and was intended to make questions arise because it's ordered incorrectly (I'm asking it to index backwards from left to right....which is totally wrong.  if you put the -2 first, things change to actually work; we'll get there later).  If you remove the ":-2" from the array, it will actually give you the last digit.  big_smile  The "+n" pulls our "!" back into the string statement.

print x[7:17] + x[10:13] + n Takes our string and grabs the I am a (str{ing}manc)er out and reorders it (stringmanc){ing}.  Ask if this doesn't make sense.

sadly  sad  a bit still lost you may have to put it in laymans for me LOL im trying.....

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