
-----------------------------------
apython1992
Sun Mar 20, 2011 2:17 pm

An Overview of Python - Tutorial 2: Input and Output
-----------------------------------
Hello again! This is a continuation from my last tutorial, where we discovered what kind of language Python is and how we can go about getting it.  If you haven't read that yet, be sure to check it out!  Of course, if you already have Python and know full well of its power, then you can just start here.  While I mentioned at first that these tutorials are aimed at attracting programmers to high-level languages, I've noticed that there are a lot of you Turing students here, and I think this would also be an excellent way of helping you take the leap into bigger and better.  My humble opinion states that Python is an excellent next step to take after Turing! Python is also an excellent first language, so entirely new programmers can hop on board too.  So forward we go - open up a terminal/command prompt window, type in the "python" command and behold the prompt of the interactive interpreter.

Hey, a calculator!
As we went through before, Python's interactive interpreter lets you type in python statements one at a time and immediately see the output.  If you have never experienced using interactive prompts of any kind, it might be hard to immediately understand why it's so useful and when you could possibly ever need it.  But trust me, once you see how it works and start to get a feel for it, you'll really find it useful to write small sections of code just to test something.  Well, let's start getting a feel for it.  In the window, try typing a simple mathematical expression, and see what you get.
importing a feature from future versions of Python to use in our current version. Yay! By the way, if you're wondering why we decided to stick with Python2x, it's because Python3x is still very new and a lot of third party libraries haven't caught up, so it's best to wait until it's a little more mature. 
So a little more on import: think of this as Python's #include, if you're a C/C++ programmer.  The import statement allows you to include a module outside the language's core for some additional functionality; for example, the "import math" statement allows you to use some mathematical functions (like basic trig) that aren't part of the language's core.  The "from __future__ import Finally, some programming: The Print Statement
Since this tutorial is really only going to deal with input and output, we'll stick to using the interactive prompt for now.  In the next tutorial, we'll start talking about control flow and switch over to writing real programs that we can save and run.

In Python, the print keyword is used to tell the interpreter that whatever comes after it should be output to the console.  This is like Turing's put, C's printf(), and C++'s cout.  Pretty straightforward, right? Right on.  Let's try a couple of examples:
print statement is pretty flexible.  It will output any value, no matter the type.  In a couple of later tutorials, you'll see that you can even print representations of objects.  We tried printing mathematical expressions too - really, they just resolve to a single value, which print can handle! In the last two examples, you can also see that it's possible to print mixed types of values as well.  The first example uses commas to separate values (string, int, string), where the second example uses string formatting.  With string formatting, wherever you see the curly braces with a number, that means that a value of any type will be substituted by any values that are pumped in to format().  You can have any number inputs to format(), which correspond with the numbers inside the curly braces. Example:
Getting User Input
Great, so now we're totally bored. One and a half tutorials and all we know how to do is print stuff to the screen.  Show us how to get user input please, for the sake of all of us!

Well, okay! Getting input is very easy in Python. There are really two main ways to do it: using input() or raw_input().  Both of these are implemented in the same way.  We always want to store input into a variable, so we can refer to the value later.  In Python, it looks like this:
An exercise: Using the interactive prompt, ask the user for a name and age, and use string formatting to produce a single line of output structured as follows:

[code]Hello, [name], nice to meet you! I'm [age] too![/code]

I know. This is really easy.  But I do want to see new programmers venturing into Python as well, so I apologize if this is a slow start for you.  Keep checking back though, new tutorials are coming and you can skip over whatever you want!

Now, here's something I'm going to start doing for every tutorial: a challenge question for new Pythoneers, where I will donate 10 bits to the first member who can answer correctly.  And please, exercise discretion: if you have experience at all with Python, Ruby, or anything like that, don't be a jerk! Let the newbies give this a go. If you know languages like C, C++ or Java but have never used Perl, Python, or Ruby, you're allowed in on this too.

Challenge 1:  What does the statement [code]print "Age: {0}".format(18)[/code] imply about the nature of all data in Python?

And by the way, this is the first time I've ever done any kind of tutorial, so feel free to harass me a little bit.  My goal here is to teach. I love teaching, and I want to see the Python community here grow. So if there's something you think I should do differently or add, let me know! That's all for this tutorial guys. Stick around, it's gonna get real fun real fast.  Next up is writing and running real programs with control flow.

-----------------------------------
SNIPERDUDE
Mon Mar 21, 2011 1:38 pm

RE:An Overview of Python - Tutorial 2: Input and Output
-----------------------------------
Very nice tutorial, good to see more on Python here
