Python basics

All new tutorial are submitted here.
They will be moved to the main Tutorials section once they have been approved by mods and admins.

Python basics

Postby Hissing Cockroach » Sat Sep 28, 2013 6:34 pm

PRINT
This is a short tutorial for the basics of Python. First, you are
probably going to need to know the Print command.
what the print command does is prints whatever you
type in after it:

Code: Select all
print "I broke my back."


returns:

Code: Select all
I broke my back.


If you use only this for you'r program, you will need to use
the command line, because in IDLE(something you will learn later on),
only print "I broke my back" won't work.

Anyway, what do you do when you wan't to write "It hurts."
under "I broke my back."? Well, you simply do it like this:

Code: Select all
print "I broke my back."
print "It hurts."


"="
Not much harder, but definately not much interesting.
To make it more interesting, you can use the "=" oporator.
Go to the command line(keep it open until tutorial ends)and type in

Code: Select all
apples = "fruits"


Then, type apples.
What came up? It should be fruits.



"+"
Python can also add, subtract, multiply, and divide
numbers. For example, 2 + 2 returns 4.
Now you don't have to buy a calculator!

You can also add words together:

Code: Select all
"rotten " + "egg"


>>>

Code: Select all
rotten egg




Type "rotten " + apple into the command line and see what
comes up.

Suprised?
The command line returned rotten fruit instead
of rotten apple because apple = "fruit".
(You typed apple = "fruit" in the command line earlier)



INPUT()
Now, you know the basics of the basics of python.
However, it's still not enough to write a full program. To make one,
you'r probably going to have to know the command input:

Code: Select all
input


Won't do much...You have to at least put a pair of parantheses at
the end. Like this:

Code: Select all
input()


What this does is waits for the user to type someting and
press ENTER,then moves on to the next part of the script.
Here is a more advanced program:

Code: Select all
a = input("Type a number...")
if a == 1:
print "1"


Or, to make more choices, you can use the else, wich is met when the if is not met:

Code: Select all
a = input("Type a number...")
if a == 1:
   print "1"
input()

else:
    print "not 1"

input()





Here is a more advanced program. The #'s will tell you what everything means:

Code: Select all
a = input("type a number...")
if a == 1:
    # Do something...
input()
elif a == 2:
    # Do something else...
input()
elif a == 3:
    # Do something completely different...
input()
else:
    # If all else fails...

input()



END
Well, now that you know the basics of python, it's time to
end the tutorial.
Last edited by Hissing Cockroach on Mon Sep 30, 2013 2:13 am, edited 3 times in total.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Python basics

Postby micseydel » Sun Sep 29, 2013 5:34 am

This is very misleading. The print command of Python 2 does not "return" anything. To say so could confuse people down the line. I would also leave out input, or if you're going to mention it, just say that you might see it but use raw_input() instead. Further, most of the code where you should ifs is just wrong. It uses = instead of == and doesn't indent. If you're going to post tutorials, they should be rigorously correct.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1300
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python basics

Postby Hissing Cockroach » Mon Sep 30, 2013 2:14 am

Sorry about that.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Python basics

Postby micseydel » Mon Sep 30, 2013 3:35 am

You still have code here that won't run. You'll get either a syntax or indentation error.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1300
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to Tutorial Submissions

Who is online

Users browsing this forum: No registered users and 2 guests