Welcome Guest.
Not a member yet? Why not sign up today and start posting on our forums.
[TuT]First Step Into Python

Okay, so I've been taughing myself Python over the last few weeks, so I thought that I might want to make a tutorial about starting to develop Python on Windows (As I've never used Linux or Mac, so I can't really speak about them.) What I will be showing is using Python with Notepad++ and running it from there and also a bit about input/output and OOP (classes, functions etc.) It will not be a very detailed tutorial, but I'm sure it will help Smile

First of all you need to download Python, which you can do here:

I'd recommend 2.7, unless you have a reason for downloading Python 3.

Once you have downloaded Python, then you need Notepad++, if you do not have it already.


Now let's set up a shortcut for notepad++ to run with Python.

Open Notepad++ and open Run -> Run

Now find python.exe (Usually C:\\python27\\python.exe)

Now at the end of the path (When selected python.exe) add:

It's important there is space between python.exe and $(FULL_CURRENT_PATH)

Now press on Save and you can make a short-cut for running your code in python.

Once you've done that then you can start making your first python code.

Create a new document in Notepad++ and go to Language -> P and choose Python. (This is not necessary, but for syntax-highlight only).

Now before we start you need to understand 2 things, input and output. Input is inputs whom the user gives the application and output is the output from either an input for directly from the application. Outputs is the result, which the application spits out wether it being data to the screen or to a file.

In Python there is the print keyword, which is used to print data to the screen. This is the output, which we'll be using in our application as this is only supposed to be beginner level and nothing more. To get an input from the user we got the raw_input, which is a default function in Python. It maybe take a string as a parameter. Which will be the display text for our input. Ex "What is your name?".

It could be something like:
raw_input("What is your name?")

Now let's try make our first Python script. Create a new Python file (.py) and call it whatever you want.

First we will just try to print something out to the console, so let's write "Hello World!" as it's the most common example used for beginners and/or testing.

print "Hello World!"

Notice that it will close right after spitting Hello World out. How can we prevent that? Well just ask for a user-input with an empty raw_input.

Now it should print Hello World to the console and wait for an input from the user (Eg. ENTER).

Now what about variables? Basically this is what I like of Python. It's the beauty of just writing what you want. Python is basically like speaking English. You do not have to specify a data-type, because the interpreter will find out the data-type itself (In most cases) However it's necessary to specify it sometimes, when giving a variable data.

In C++ if we wanted to declare an integer, we'd have to specify the datatype and give the variable an identifier, however Python only requires the identifier.

int MyInt = 100;

MyInt = 100

The result would be the same, but Python is really nice, because it won't require a datatype, however you might have to specify it in some cases when giving a variable a value.

Like below.

MyInt = int(100)

In the case above we can make sure it's always an integer which is given to the variable, because otherwise it will throw an error.

Ex. if we did:
MyInt = int(100)
MyInt = MyInt + "Hello"

Then it would fail, because "Hello" is a string, but MyInt is an integer.

But if we did:
MyInt = str("100")
MyInt = MyInt + "Hello"

Then it would work perfectly fine, because MyInt is now a string.

Now what about functions/methods in Python?

The def keyword is used to declare a function/method. Remember one thing, in Python white-spaces is VERY IMPORTANT, because the way Python parses your code is by white-spaces and not brackets etc. like in eg. C++.

The structure of a function in Python is like following:
def FunctionName(parameters):
A function doesn't require any parameters tho!

The following code would be just fine:
def FunctionName():

Now try make a function and add a parameter called wrtstring and then let it print out. (Use tab to go 1 indent in)

Method structure:

TAB_SPACE = the white-space from tab.

If you cannot figure it out, look below (But only if you can't! Try yourself first!)
Now when you've figured that out let's continue to the next part, which is OOP (Object Oriented Programming), which is basically classes and creating more instances of a class for usage.

The class keyword is used to declare a class and just like before white-spaces are important here.

You go one tab-space in for declaring things for the class eg. variables & functions/methods. and then another tab-space in for the functions etc.

class MyClass:
	def MyMethod():

Now you should be able to understand the actual syntax of Python, which is basically build up by white-spaces.

You've already learned how to declare variables and using functions, but now we will get more indepth with it. How we work with variables within the classes, creating more instances and for this example we'll be making a class called Person. This class should contain data about a person (Name & Age as it's only beginner.) We want to have a method to change name, age and print out the personal data.

Now that sounds like a big mouthful, but don't take it like that.

One thing at a time, so first of all let's create our class.
class Person:

We already know that we need two variables (Name & Age), so let's declare them as well.
class Person:
	Name = ""
	Age = 0

We also know that we need a method to print out the data.
So let's make that at first.
class Person:
	Name = ""
	Age = 0
	def PrintPersonData(self):
		print self.Name
		print self.Age

Now you see a new thing, which is the keyword self. Basically self is equal to this from Eg. C++ or C# etc. However it's required to declare self in the parameter of the function, but you do not have to pass it as a parameter. Self, basically means the current instance, which is itself (Hence the name of it.)

Now how would you go about declaring the name and age from a function? Well basically just add 2 parameters for the data to add and then just add the values to the class.
	def ChangeName(self, NewName):
		self.Name = NewName

And the same goes for age, if you can't figure it out look below.
Now the last thing we need to do is learning to declare a new instance of a class. Basically as you already know the variable names we uses can be whatever we want. Programming with Python is basically like speaking English, which is why I like Python. Not only does it make it an easy language, but it's still a very powerful language, but enough of that, we have to continue.

The way to declare a new class is following:
Varname = Class()

Where Varname is the identifier of the variable and Class is the name of the class we want to create a new instance of.

In our case Class would be Person and Varname would be whatever you feel like. You could call it the name of the person you wish to create an instance of eg. Bob
Bob = Person()

To access anything in the class you have to put a . after that you write either the variable or the function you wish to access.

Now Bob does not really have a name, because the variable Name does not have any value, but we've already created a function to change name, so let's just call that.

And same goes for age.

And at last we want to print out the data.

Congratulations your Python code runs just fine and will print out the name of Bob and his age.

Now to challenge yourself try create another instance of your class with another person, but keep your first instance. As well maybe try make methods for swapping the datas between 2 persons.

At last I think that's it and I wish you good luck learning Python.[/color]

That's very nice tutorial BaussHacker!!
I liked the comparisons made between C and Python.
Also I would suggest you to change color to some light ones.
Other things are fine.
Thank you very much! Biggrin
[Image: jQGP6.gif]

(05-24-2012, 08:38 AM).::MAX::. Wrote: That's very nice tutorial BaussHacker!!
I liked the comparisons made between C and Python.
Also I would suggest you to change color to some light ones.
Other things are fine.
Thank you very much! Biggrin

Will do asap Smile

WOW! i can say this is the best TUT i've seen really great job!
thanks for sharing this!

Thanks Mate ,,, Smile
i'm learning pthon ..
[Image: CF1Nu.png]

..oh wow just exactly the moment I wanted to find a good Python tutorial.

Thanks mate.

I just have one word "Awesome". The tutorial is explained very efficiently, I am also learning .py but this is one of the best tutorial on any forum for python.
[Image: 1345503315.png]