Welcome, Guest. Please login or register.
Pages: [1]
  Print  
Author Topic: Introduction to Python  (Read 1769 times)
Spy
Administrator
Hero Member
*****

Karma: 70
Posts: 182



View Profile WWW
« on: August 16, 2010, 10:31:07 PM »

Hai guys. This is the first of many tutorials on Python that i'm going to do. This is just the introduction, so if you think that it's way to easy, that's a good thing. Anyway, let's get started!

First off, what is Python?
Python is a general purpose programming language that is often applied in scripting roles. It is object-oriented, and very easy to use. Because of these traits, it is often called an object-oriented scripting language.

So what can I do with it?
The real question is, "What can't you do with Python?". You can code Shell tools, GUIs, Games (both 2D and 3D), Server-side Scripts, Client-side Scripts, and the list goes on and on. It's almost ridiculous that Python has so few limitations and is so powerful.  

What do I need to get started?
If you are running Windows or a Mac-OS, you need to download Python from http://python.org/. If you are running any good Linux distro, then you already have it! I would recommend downloading 2.6.4 and not 3.1.1. 2.6 is used the most around the world, and is generally more stable.

Awesome! Let's get to it!
Ok, but before we actually jump into coding, we need to learn the fundamentals. FUN-damentals.
After downloading, you should have a new, sexy little program called "IDLE". IDLE is the Python command line GUI and code editor. You can also run and edit Python scripts from the command line, but let's stick to IDLE for now.
To make a new Python script, right click on the desktop, and select "New Text Document". Now rename it to "new.py". The file for a Python extension is .py. Throughout your Python carriers, you may see a .pyc file. A .pyc file is a compiled Python script. That means that it has already been compiled by the Python interpreter; you can still run it, but you cannot edit or see the code inside it.

Can we get to coding already?
Alright, alright. Let's get started with the coding.
First, open up your "new.py" document that you created. You can do this by right-clicking on the document, and selecting "Edit with IDLE". This should bring up two windows; one with a blank text editor, and one with the Python Shell. You can close the Shell for now, you won't need it yet.
You can now start to write your code! Easy, huh?
For starters, let's make a "Hello World" script.

Code:
print 'Hello World'

Yeah, it's a one line code. Save it, and then hit f5 to run it.
A Shell window should pop-up, and it should look like this:

IDLE 2.6.4      ==== No Subprocess ====
>>>
hello world
>>>

You have just been introduced to the print command.

The print Command
Yes, it is that easy. To display text in a shell, all you need to do is use the print command.
The actual text part can be in single quotes, such as 'text', or in double quotes, such as "text". It doesn't actually mater, Python treats them the same way. The stuff inside the quotes are called strings.

Important Note:
Python Commands are case sensitive! That means that Print is different from print!

You can play with the print command all you like, but the fun doesn't really start until you start using variables.

The Basics of Python Variables
Hopefully, you already know what a variable is. Just in-case you don't, a variable is a container of a value. It holds some value (a number or string); but in Python, variables act a little differentially. They act as sort of "pointers" to a value. Defining variables in Python is much easer that in other languages. To define a variable, all you need to do is state it. This can be done with one simple = sign.
Say we wanted to create and set the variable Spy and set it to the string "cool". We could do this by using an equals sign. Here, i'll show you.

Code:
Spy = "cool"

Easy huh? Now try the print command with our new variable.

Code:
Spy = "cool"
print Spy

This should return:

IDLE 2.6.4      ==== No Subprocess ====
>>>
cool
>>>

Now you may be thinking that this is all pretty basic stuff, and you would be right. Let's get to some more fun commands.

User-Defined Variables
You can prompt the user for text by using the raw_input() function. In order to use raw_input(), you have to assign it to a variable.

Code:
x = raw_input("Your Prompt Here: ")

When using raw_input(), you have to put a string in the (). This string will be the prompt that the use sees when they are asked for input.
We can use a print command in conjunction with the raw_input() function to make a neat little program that asks the user for their name, and then says hello to them.

Code:
name = raw_input("What is your name? ")
print "Hello", name

This should return:

IDLE 2.6.4      ==== No Subprocess ====
>>>
What is your name? Spy
Hello Spy
>>>


Cool no?

Functions
Functions are basically commands. They tell the code to go through a list of protocols. raw_input() is a function. It is a good model to use to explain functions. Let's pick it apart.
raw_input is the name of the function. It tells Python which function you want to use. Now onto the good part. The () is the place were you can put arguments. Arguments are variables defined inside the function's code. In the raw_input() function, the () holds a string that you want to use for the prompt. The argument that you are suposted to put inside the () differs from function to function.

Modules
What are Modules you ask? Modules are the backbone of Python.
In actuality, they are Python codes that contain defined functions. We will go over defining our own functions later.
In order to use a Module, you have to import it first. For example, if I wanted to use the math module, I would use the code:

Code:
import math

This would allow me to use all of the functions contained in the math module.
In order to use one of these functions, you have to state that you are using it from the math Module. You state this fact before you reference the actual function, such as:

Code:
import math
x = math.sqrt(10)
print x

This code will use the sqrt function located in the math module to find the square root of 10. It will then print the output of that function.

Each Module contains very different functions, and I don't know them all. You will have to look up documentation for specific functions.

Code Documentation
Comments are easy to do in python. On the same line as some code, you can put a # (hash), and all the text after the hash will be ignored by the interpreter. If you are using IDLE, it should also turn red.

if Tests
In simple terms, the if statement selects actions to preform. The syntax for if statements goes like this:

Code:
if <test1>:
    <statements1>
elif <test2>:
    <statements2>
else:
    <statements3>

An actual code would look like this:

Code:
name = raw_input("What is your name? ")
if name == "Spy":
    print "All Hail", name
elif name == "Alex":
    print "Damn you are sexy", name
else:
    print "GTFO", name

This asks the user for their name. If the name is equal to the string "Spy", then it returns the string "All Hail" and the name variable. If that is not true, it checks to see is name is equal to "Alex". If this is true, it returns "Damn you are sexy" and the name variable. If neither of these checks are true, it returns "GTFO" and the name variable.

== IS NOT THE SAME AS =
A common mistake is to use = instead of == in if tests. This is a big no no, as your code will not work if you do this. The = sign is used to define a varable, and the == literally means "equals".

while Loops
There are two types of loops in Python, the while, and the for loop.
The while loop looks like this:

Code:
while <test1>:
    <statements1>
    if <test2>: break
    if <test3>: continue
else:
    <statements2>

The break and continue tests are optional. The do an addition check for arguments, and break or continue the loop accordingly.
An actual loop would look like this:

Code:
a=0
b=11
while a < b:
    print a,
    a = a+1

The output would look like:

IDLE 2.6.4      ==== No Subprocess ====
>>>
0 1 2 3 4 5 6 7 8 9 10
>>>


As you can see from this example, when Python defines a variable as it's self and an argument, it overwrites the original variable.

Final Words
Thanks for reading my Introduction to Python tutorial. In the next tutorial, I will go over for Loops, Working With Files, More on Functions, Defining your own Functions, and Making your own modules. Have fun with the most sexy language out there!

-Spy
Logged

Check yo dick for the HIV
LOOK!
I'm Owl City!
And i'm totally, 100% hetrosexual!

rokrboy
Hero Member
*****

Karma: 3
Posts: 208


Look Into My Eyes.


View Profile
« Reply #1 on: August 16, 2010, 10:32:56 PM »

Need to pay close attention to this for my work. Thanks for the tutorial +karma

Offtopic:
Would you happen to know a lot about tcsh shell scripting?
Logged
Spy
Administrator
Hero Member
*****

Karma: 70
Posts: 182



View Profile WWW
« Reply #2 on: August 16, 2010, 10:38:45 PM »

Need to pay close attention to this for my work. Thanks for the tutorial +karma

Offtopic:
Would you happen to know a lot about tcsh shell scripting?

Thanks,

And no; I don't know anything about tcsh shell scripting. I do know about bash scripting though if you ever need help with that.
Logged

Check yo dick for the HIV
LOOK!
I'm Owl City!
And i'm totally, 100% hetrosexual!

rokrboy
Hero Member
*****

Karma: 3
Posts: 208


Look Into My Eyes.


View Profile
« Reply #3 on: August 16, 2010, 10:40:28 PM »

Alright, good to know. Right now I'm using tcsh, but I may be using bash later on.
Logged
Pages: [1]
  Print  
 
Jump to: