Help Bio learn Python!

Talk about computers, hardware, applications, and consumer electronics.
User avatar
The 700 Club
Posts: 744
Joined: 2008.10.17 (00:28)
NUMA Profile: http://nmaps.net/user/BionicCryonic
Location: Lethal Lava Land

Postby Yoshimo » 2009.07.28 (06:31)

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!
spoiler

Image
Image


Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2009.07.28 (07:56)

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.

User avatar
Retrofuturist
Posts: 3131
Joined: 2008.09.19 (06:55)
MBTI Type: ENTP
Location: California, USA
Contact:

Postby t̷s͢uk̕a͡t͜ư » 2009.07.28 (08:19)

I read a book and did the exercises in it.
Weird how that works.
[spoiler="you know i always joked that it would be scary as hell to run into DMX in a dark ally, but secretly when i say 'DMX' i really mean 'Tsukatu'." -kai]"... and when i say 'scary as hell' i really mean 'tight pink shirt'." -kai[/spoiler][/i]
spoiler

Image


User avatar
Hawaii Five-Oh
Posts: 921
Joined: 2009.02.16 (02:57)
NUMA Profile: http://nmaps.net/user/Guiseppi
MBTI Type: ISTP
Location: Ithaca, New York
Contact:

Postby Adoniseppi » 2009.07.28 (08:31)

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.
Image

User avatar
Cross-Galactic Train Conducter
Posts: 2354
Joined: 2008.09.27 (00:31)
NUMA Profile: http://nmaps.net/user/T3chno
MBTI Type: ENTJ
Location: foam hands
Contact:

Postby T3chno » 2009.07.28 (17:15)

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.
Image

User avatar
Global Mod
Global Mod
Posts: 1416
Joined: 2008.09.26 (05:35)
NUMA Profile: http://nmaps.net/user/scythe33
MBTI Type: ENTP
Location: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Postby scythe » 2009.07.29 (04:16)

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.
As soon as we wish to be happier, we are no longer happy.

User avatar
Damn You're Fine
Posts: 384
Joined: 2008.09.19 (01:47)
NUMA Profile: http://nmaps.net/user/littleviking001
MBTI Type: INTP
Location: Southern California
Contact:

Postby LittleViking » 2009.07.29 (05:00)

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'
Image
The last Metroid is in captivity. The galaxy is at peace...

Yes sir, no sir, three bags full sir
Posts: 1561
Joined: 2008.09.26 (12:33)
NUMA Profile: http://nmaps.net/user/incluye
MBTI Type: ENTP
Location: USofA
Contact:

Postby otters » 2009.07.29 (21:36)

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.
Image

User avatar
The 700 Club
Posts: 744
Joined: 2008.10.17 (00:28)
NUMA Profile: http://nmaps.net/user/BionicCryonic
Location: Lethal Lava Land

Postby Yoshimo » 2009.07.30 (05:22)

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
spoiler

Image
Image


User avatar
Damn You're Fine
Posts: 384
Joined: 2008.09.19 (01:47)
NUMA Profile: http://nmaps.net/user/littleviking001
MBTI Type: INTP
Location: Southern California
Contact:

Postby LittleViking » 2009.07.30 (08:35)

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)"
Image
The last Metroid is in captivity. The galaxy is at peace...

User avatar
The 700 Club
Posts: 744
Joined: 2008.10.17 (00:28)
NUMA Profile: http://nmaps.net/user/BionicCryonic
Location: Lethal Lava Land

Postby Yoshimo » 2009.07.30 (16:41)

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.
spoiler

Image
Image


Hawaii Five-Oh
Posts: 919
Joined: 2009.03.06 (19:50)

Postby blackson » 2009.08.01 (05:56)

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.

Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2009.08.01 (06:40)

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

User avatar
Hawaii Five-Oh
Posts: 921
Joined: 2009.02.16 (02:57)
NUMA Profile: http://nmaps.net/user/Guiseppi
MBTI Type: ISTP
Location: Ithaca, New York
Contact:

Postby Adoniseppi » 2009.08.01 (17:41)

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.
Image

User avatar
The 700 Club
Posts: 744
Joined: 2008.10.17 (00:28)
NUMA Profile: http://nmaps.net/user/BionicCryonic
Location: Lethal Lava Land

Postby Yoshimo » 2009.09.29 (00:42)

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
spoiler

Image
Image


User avatar
Lifer
Posts: 1099
Joined: 2008.09.26 (21:35)
NUMA Profile: http://nmaps.net/user/smartalco
MBTI Type: INTJ

Postby smartalco » 2009.09.29 (01:15)

Maybe if you posted some code.
Image
Tycho: "I don't know why people ever, ever try to stop nerds from doing things. It's really the most incredible waste of time."
Adam Savage: "I reject your reality and substitute my own!"

User avatar
Damn You're Fine
Posts: 384
Joined: 2008.09.19 (01:47)
NUMA Profile: http://nmaps.net/user/littleviking001
MBTI Type: INTP
Location: Southern California
Contact:

Postby LittleViking » 2009.09.29 (01:28)

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.
Image
The last Metroid is in captivity. The galaxy is at peace...

Plus (Size) Member
Posts: 42
Joined: 2008.09.27 (02:56)

Postby taaveti » 2009.09.29 (03:02)

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)])

User avatar
Retrofuturist
Posts: 3131
Joined: 2008.09.19 (06:55)
MBTI Type: ENTP
Location: California, USA
Contact:

Postby t̷s͢uk̕a͡t͜ư » 2009.09.29 (05:09)

BionicCryonic wrote:*
*barf*
[spoiler="you know i always joked that it would be scary as hell to run into DMX in a dark ally, but secretly when i say 'DMX' i really mean 'Tsukatu'." -kai]"... and when i say 'scary as hell' i really mean 'tight pink shirt'." -kai[/spoiler][/i]
spoiler

Image


User avatar
Global Mod
Global Mod
Posts: 1416
Joined: 2008.09.26 (05:35)
NUMA Profile: http://nmaps.net/user/scythe33
MBTI Type: ENTP
Location: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Postby scythe » 2009.09.29 (10:30)

BionicCryonic wrote:*
Can I use this to scare my friends?
As soon as we wish to be happier, we are no longer happy.

dreams slip through our fingers like hott slut sexxx
Posts: 3896
Joined: 2009.01.14 (15:41)
NUMA Profile: http://nmaps.net/user/Tunco123
MBTI Type: INTJ
Location: Istanbul

Postby Tunco » 2009.09.29 (17:40)

BionicCryonic wrote:*
I somehow feel funny about that.

*Bursts out laugh*

No, really. This thread might help you.
spoiler

Image



Who is online

Users browsing this forum: No registered users and 4 guests