Page 1 of 1

Help Bio learn Python!

Posted: 2009.07.28 (06:31)
by Yoshimo
I have tried to learn Python before, and it raped me in what was one of the most unpleasant experiences of my young life. Does anyone have any good tutorial websites, or better yet, anyone willing to walk me through the ropes and hope I don't trip on them? Any form of help other than a generic rendition of 'Get good at Python, duh.' would be appreciated.

Help Bio!

Re: Help Bio learn Python!

Posted: 2009.07.28 (07:56)
by mattk210
do you know any other programming language? if you do, the documentation and help files that come with the python interpreter should be sufficient (there's an introductory section and the whole thing is well-written)

if not, i can't help you with tutorials, but the way I learnt to begin programming with was just through experimentation. I found code, tried to change it and make it do what I wanted, built up to more complex stuff from there (and read widely to discover new things and expand what I could do)
however if you do this, you'd better find some way to not learn bad practices... python's pretty good with not letting you program badly, but you may want to post your progress here.

Also I suggest it may be easier to learn things in real life than over the internet - is there a class you can take, or a friend who's willing to help you?

edit: Also, I recommend project euler to learn to program in a fun way.

Re: Help Bio learn Python!

Posted: 2009.07.28 (08:19)
by t̷s͢uk̕a͡t͜ư
I read a book and did the exercises in it.
Weird how that works.

Re: Help Bio learn Python!

Posted: 2009.07.28 (08:31)
by Adoniseppi
I learned the same way as Tsukatu. Having a structured book is a very useful way to learn. However, if you don't want to spend 30-60 dollars on a good book, you can simply read the tutorial on the Python website. Good luck, and if you ever have any questions, feel free to ask.

Re: Help Bio learn Python!

Posted: 2009.07.28 (17:15)
by T3chno
I just started too!

I've hunted for several free eBooks online, and decided this one was the best. Check it out. It's called Dive Into Python.

Re: Help Bio learn Python!

Posted: 2009.07.29 (04:16)
by scythe
Have you ever coded anything before? That's kind of instrumental. I learned Java by reading a book and doing the exercises; I learned C and Lua by just starting to code in them, and googling when I got stuck.

The tutorial gloomp linked is probably a great place to start.

Re: Help Bio learn Python!

Posted: 2009.07.29 (05:00)
by LittleViking
Some of the more useful things to know probably include:

For loops

Code: Select all

userlist = ['Tsukatu', 'blue_tetris', 'MacrossDreams']
for username in userlist:
    print "Man, I totally can't stand", username

# prints:
#
# Man, I totally can't stand Tsukatu
# Man, I totally can't stand blue_tetris
# Man, I totally can't stand MacrossDreams

---

# equivalent of a for(i=0; i<5; i++) loop
for i in range(6):
    print i

# prints: 
#
# 0
# 1
# 2
# 3
# 4
# 5

---

word = 'doggy'
for character in word:
    print character

# prints:
#
# d
# o
# g
# g
# y
"List comprehensions" are one-line for-loops

Code: Select all

print [i*i for i in range(5,11)]  # prints [25, 36, 49, 64, 81, 100]  also note: i**2 is i squared, i**3 is i cubed, etc.

---

userlist = ['Tsukatu', 'blue_tetris', 'MacrossDreams']
userlist_short = [name[:3] + '...' for name in userlist]  # note that name[:3] is a string slice operation which grabs from the start up to the third character of the string (or third item in a list)
print userlist_short  # prints ['Tsu...', 'blu...', 'Mac...']
Modules that will do good things for you
http://docs.python.org/library/random.html

Code: Select all

import random

print random.choice(['heads', 'tails'])  # prints heads or tails
print random.sample(range(1, 11), 5)  # prints 5 numbers randomly chosen from 1-10 without repeating i.e. [2, 7, 6, 3, 8]

deck_of_cards = ['9', '10', 'J', 'Q', 'K', 'A']
random.shuffle(deck_of_cards)  # (I hate in-place operations. Manipulative functions shouldn't return nothing!)
print deck_of_cards  # prints shuffled deck i.e. ['J', 'K', '9', 'A', '10', 'Q']
http://docs.python.org/library/urllib.html

Code: Select all

import urllib

u = urllib.urlopen("http://forum.therealn.com")
for line in u():
    print line  # reads and prints the forum's html code, one line at a time. one of the more pointless uses for this library, granted.
http://docs.python.org/library/re.html

Code: Select all

import re  # regular expressions

# If you don't know regular expressions, I'm afraid I probably won't teach them to you. But here's the library if you're looking for it.

import re, urllib
u = urllib.urlopen("http://therealn.com")
u_fulltext = u.read()
links = re.findall("<a href=\"?(.*?)\"?>", u_fulltext)
print links  # prints all the links on the Real N homepage
String Multiplying is kinda cool

Code: Select all

n_tileset = '0'*713 + '|'  # prints a blank N tileset. i.e. 00000000000 ... 000|

---

print 'G' + 'o'*10 + 'gle'  # prints 'Goooooooooogle'

Re: Help Bio learn Python!

Posted: 2009.07.29 (21:36)
by otters
LittleViking wrote:Some of the more useful things to know probably include:

For loops

Code: Select all

userlist = ['Tsukatu', 'blue_tetris', 'MacrossDreams']
for username in userlist:
    print "Man, I totally can't stand", username
I love you. This stuff is helpful.

Re: Help Bio learn Python!

Posted: 2009.07.30 (05:22)
by Yoshimo
Holy crap LV, are you a demi-god or something? -_-

Anyways, no previous coding experience, other then the one time I tried to learn Python earlier...

Anyways, that string multiplier looks awesome, and could be awesome for making some tileset foundations.

Thanks all. And I'm using the latest version of Python, is this good? 3.1

Re: Help Bio learn Python!

Posted: 2009.07.30 (08:35)
by LittleViking
BionicCryonic wrote:Holy crap LV, are you a demi-god or something? -_-
http://forum.therealn.com/viewtopic.php?t=2361 <-



Also, yeah, Python 3.1 is pretty fine. The only difference you'll notice between my examples and Python 3 is that anywhere where I say "print blahblahblah" should instead say "print(blahblahblah)"

Re: Help Bio learn Python!

Posted: 2009.07.30 (16:41)
by Yoshimo
LittleViking wrote:
BionicCryonic wrote:Holy crap LV, are you a demi-god or something? -_-
http://forum.therealn.com/viewtopic.php?t=2361 <-



Also, yeah, Python 3.1 is pretty fine. The only difference you'll notice between my examples and Python 3 is that anywhere where I say "print blahblahblah" should instead say "print(blahblahblah)"
I already saw the Tiny N Level Generator, and that's what tipped the scale, so to speak. And I already figured out how to print(stuff), and make some primitive list, like [1, 2, 3]+[6, 98], and that would return [1, 2, 3, 6, 98] or something to that effect.

Re: Help Bio learn Python!

Posted: 2009.08.01 (05:56)
by blackson
I learned up to loops in a day on this page: http://hetland.org/writing/instant-hacking.html

It's a great page, but I need someone to explain to me loops, they confuse me.

Re: Help Bio learn Python!

Posted: 2009.08.01 (06:40)
by mattk210
whatever code is inside the "while" block is repeated until the loop condition is no longer true.

Code: Select all

1...number=0
2...while number<5:
3...    print number
4...    number=number+1
5...print "done"
so the flow is: line 1, then check if number<5, if it is do lines 3 and 4 and go back to 2, otherwise continue to line 5

similarly,

Code: Select all

1...for number in range(5):
2...    print number
3...print "done"
range(5) returns a list consisting of 5 numbers, starting from 0. the block is executed for each element in that list, and the element that the loop is up to can be accessed with the variable name "number". So it will execute line 2, 5 times, each with a different value in "number", then execute line 3

tsuki or littleviking can probably explain it better if you don't understand

Re: Help Bio learn Python!

Posted: 2009.08.01 (17:41)
by Adoniseppi
Don't forget the beauty of the infinite loop.

Code: Select all

while True:
    print "PENUS"
You can use a different example if you wish, but that's just an example.


Here's a function that will count the numbers it processes. See how high you can go before you get bored of it.

Code: Select all

count = 1
while True:
    print count
    count += 1
Each time it runs through the loop, it prints the current count, and then increments it by one.

Re: Help Bio learn Python!

Posted: 2009.09.29 (00:42)
by Yoshimo
Alright, I'm trying to code a random level generator, using preset strings, albeit a large library. Anyways, see * at bottom for example of what I'm doing. I only have three strings., and I bet I could shorten it largely, but that's not the problem right now. The problem is that when I type rtiles(), it gives me 31 independant strings. Right now, I only want one large string. Can anyone help?


* So far

Re: Help Bio learn Python!

Posted: 2009.09.29 (01:15)
by smartalco
Maybe if you posted some code.

Re: Help Bio learn Python!

Posted: 2009.09.29 (01:28)
by LittleViking
Yeah, don't return my_variable_name. Return ''.join(my_variable_name). That's shorthand for the str.join() function which joins a list or tuple into a string. '' is the separator - in this case, a blank string so each character is put right next to the other. If you wanted comma-separated, for example, you would do ','.join.

Re: Help Bio learn Python!

Posted: 2009.09.29 (03:02)
by taaveti
You may also want to consider using loops or list comprehension to cut down on repeated code. An equivalent rtiles function would be:

Code: Select all

def rtiles()
    import random
    return ''.join([random.choice(['10215021EJ111AGOO11A0;1',
	    '1@214001E?111A00>11A0;1',
	    '111A0FJ111A00000>11MI0N']) for i in range(31)])

Re: Help Bio learn Python!

Posted: 2009.09.29 (05:09)
by t̷s͢uk̕a͡t͜ư
BionicCryonic wrote:*
*barf*

Re: Help Bio learn Python!

Posted: 2009.09.29 (10:30)
by scythe
BionicCryonic wrote:*
Can I use this to scare my friends?

Re: Help Bio learn Python!

Posted: 2009.09.29 (17:40)
by Tunco
BionicCryonic wrote:*
I somehow feel funny about that.

*Bursts out laugh*

No, really. This thread might help you.