Gw Temp

Menu

Tutorial - 'I/O in Python' by Quest-Master

An item about Programming Languages posted on

Blurb

Accomplish neat tricks relating to input and output from and to your user, all in Python.

Body

Python is probably one of the best languages available today. A very fun, perplexing, and easy task in it is to do neat things with reading and writing any file on your hard drive, and getting input from the user running your program.

This tutorial will not cover installing Python, or the basics of it, but if you know any programming whatsoever, you'll catch onto Python quickly.

We'll start off with getting input from your user, and doing things from there on. But, we must import the os package before doing anything.

import os

Add that as the first line of your .py file. Nothing too difficult. Let's move on and put in an actual function after the import:

r = raw_input("Type in something, homie: ")

Run this, and you should get a nice little greeting reading "Type in something, homie:" As if you didn't know that, hehe.

Now, in it's current from, this does nothing. We've got to go farther with some if statements, or maybe switches later. I hope you know what these mean, if not, check python.org for beginners tutorials and guides.

Add the following lines to your .py:

if r == "1337":
    print "You are so 1337."

Now, your full program should be:

import os

r = raw_input("Type in something, homie: ")

if r == "1337":
    print "You are so 1337."

Try this, and when it asks you "Type in something, homie:," type in 1337 and see what follows. ;)

Let's get a little more advanced with elseifs and their cases. This is the complete .PY, btw.

import os

r = raw_input("Type in something, homie: ")

if r == '1337':
    print 'gofag'
elif r == '666':
    print 'you satanmaen'
elif r == 'gw':
    print 'caffeine'
elif r == '101':
    print 'binary is awesome yo'
else:
    print 'stop being dumb plz?!?!'

Yup. Try it and type in the many values of r that can be seen. That about covers all of I/O, I'll be back next time with File R/W, which combined with I/O, is awesome :) For all of those that think this is a very incomplete tutorial, it is because the full tutorial is segmented into many pieces, and this is only the first part.