Jump to content

Recommended Posts

Posted

########### This Article Is For Non-Programmers/People Just Stating Out. ###########

 

Python, where to start. Python is an amazing language, in fact, it was the first language I learnt. I think this quote from a friend provides anyone with an interest in entering the programming "scene" enough motivation to learn python as a first language.

 

What the hell is wrong with you people? Always dicking around with questions like "What programming language should I lear?" or "Should I learn to Program" or what the -beep- ever the hell your wasting your life doing. Just -beep-ing learn python and quit -beep-ing participating in this -beep-ing circle jerk conversation about nothing. Is python special? -beep- NO. Just learn a language. If you want to work in the field, you'll probably end up learning a lot more than one. And that doesn't even count all the markup languages and SQL shit you'll learn. SO SHUT THE -beep- UP AND LEARN PYTHON OR GO THE -beep- HOME. 

 

 

So, you heard the man, lets learn some python. First we're going to need an interpreter (To program in, as Python is a scripting language, it uses an interpreter, not an IDE (Integrated Development Environment) We're going to be using Python 2.6.5, although Python 3.1.2 is now released it's quite new, as there are some small changes, a beginner getting code snippets from the web may get a few small bugs, but have no idea how to fix them. You can download the interpreter from Here  (Remember, get 2.6.5, not 3.1.2)

 

Once you've installed it, and everything is ready, we're going to open up Python's GUI (As opposed to the command line version) The GUI is called IDLE, you will probably find it in your start folder under "Python 2.6 -> IDLE (Python GUI)"

 

A screen will open, this is the "Python Shell", this will let you write, and run code in what I can only describe as a live environment. In keeping with programming tradition, the first code we will run is a simple one. Displaying the words "Hello World" onto the screen. Do do this we just type:

print "Hello World"

And hit return(Enter).

 

As you can see, it will "OutPut" the words "Hello World". Congratulations, you have just made your first programming script.

 

So, lets analyse this, as you might have guessed, in Python "print" means print the text, and ""Hello World"" is the text to print. So, if we put:

print "Doggie"

print "Kittens"

print "Cake"

 

They will print the text Doggie, Kittens, and Cake respectively.

 

Now lets try some maths. (Don't lose interest, we will make a "real" program soon) Put in this into the shell.

 

1 + 4

 

Then hit return. As you can guess, it outputs 5. You can also try these:

 

4 * 7

6 / 3

25 - 8

 

As you can see, it's like a calculator, you input your numbers (Called "Integers" In programming) and your operator ( +, -, *, / ) then it will output the answer.

 

Next we're going to learn about variables, as you might have guessed, variables vary! For this we're not going to use the Python shell, as it's not really good for anything other than what we've done so far. So you need to click "File-New Window" (You can now close the shell if you like).

 

What this new window does, is allow us to write longer, more complex, multiline scripts. Which can be saved, and used again later. What we're going to do in this new window is type:

 

(I suggest for your own learning, you type, don't copy and paste)

 

Text = "Welcome To My First Real Python Program"
NumberOne = 5
NumberTwo = 7

 

This is what we call "defining" the variables. This means throughout the program, "Text" will mean ""Welcome To My First Real Python Program"". So instead of typing out "Welcome To My First Real Python Program" Every time we need the computer to say it, we can just put:

print Text
print Text
print Text

To say it three times, rather than:

 

print "Welcome To My First Real Python Program"
print "Welcome To My First Real Python Program"
print "Welcome To My First Real Python Program"

 

So, next we're going to do just that. On a new line, type:

 

print Text # Is the same as "print "Welcome To My First Real Python Program""
print NumberOne + NumberTwo #Is the same as "print 5 + 7"

 

Let me quickly explain what the "#"s are. In Python "#" Means a comment, so everything after "#" On a line will be ignored. So you can make notes on your code to help you understand it better.

 

Now we need to save the script, and run it. So we click "File-Save As",and save it somewhere (I would recommend making a "Python Scripts" Folder to keep your scripts organised in one place.) You're going to have to save it with the .py format, so "FirstProgram.py". Now to run the script, go "Run-Run Module" Or just hit F5. If you've made any changes since saving, it will make you save it again before running. Now if everything has worked, you should see this in the Shell window:

 

    >>>
   Welcome To My First Real Python Program
   12
   >>> 

 

Well done, you've made your first program with variables. Just one more "101 style lesson" Before we can make a real program. Now we've got to learn how we can make a script, interact with a user.

 

We do this with something called "raw_input", what this does it allow the user to enter and change the variables when needed. Close your current window (Save it) And again go "File-New Window" in the shell.

 

Here we're going to type:

Name = raw_input("Please Enter Your Name:")
Age = raw_input("Please Enter Your Age:")

print "Your Name Is:", Name
print "You Are:", Age, "Years Old"

 

Now save and run (Remember, the .py extension) And you should see this in the Shell:

 

Please Enter Your Name:

 

Type in your name, hit return, and then it will prompt you for your age, Enter that too. The program will now:

 

print "Your Name Is:", Name
print "You Are:", Age, "Years Old"

 

Eg:

 

>>>
Please Enter Your Name:Vorfin
Please Enter Your Age:17
Your Name Is: Vorfin
You Are: 17 Years Old
>>>

 

Right. If that's worked, we are ready to make our first real program. Here is a picture of the finished product:

 

PythonCalc.png

 

This might look confusing now, but don't worry. By the end of this article you will understand everything. Lets take a closer look at our first lines:

 

print "Welcome To Python Simple Calculator v1\n"

Name = raw_input("Please Enter Your Name: ")

print "Welcome", Name

 

You already understand 99% of this from earlier in the article. The only new bit is "\n" All this means is "NewLine" So it will print:

Welcome To Python Simple Calculator v1

Please Enter Your Name: 

 

NOT !

 

    Welcome To Python Simple Calculator v1
   Please Enter Your Name: 

 

Our next three lines you should also understand:

 



NumberOne = raw_input("Please Enter a Number: ")
NumberTwo = raw_input("Please Enter a Second Number: ")
Oper = raw_input("Please Enter an Operator[ +, -, *, / ]: ")

 

It's just assigning values to our three variables. NumberOne, NumberTwo, and Oper (#Operator)

 

Now we have something new, an if statement:

 

if Oper == "+":
OutPut = int(NumberOne) + int(NumberTwo)
print NumberOne, "+", NumberTwo, "=", OutPut
elif Oper == "-":
OutPut = int(NumberOne) - int(NumberTwo)
print NumberOne, "-", NumberTwo, "=", OutPut
elif Oper == "*":
OutPut = int(NumberOne) * int(NumberTwo)
print NumberOne, "*", NumberTwo, "=", OutPut
elif Oper == "/":
OutPut = int(NumberOne) / int(NumberTwo)
print NumberOne, "/", NumberTwo, "=", OutPut
else:
print "Invalid Operator!"

 

In "English" This would mean:

 

If Oper equals + Then
Do Something.
Else If Oper equals - Then
Do Something Else.
Else
Oper was none of the above.
Do Something Else.

 

We also have these very very scary looking lines:

 

OutPut = int(NumberOne) + int(NumberTwo)
print NumberOne, "+", NumberTwo, "=", OutPut

 

What this does, it creates a new variable called OutPut, and then assigns it's value as NumberOne + NumberTwo. However when the user inputs the numbers, they are in String format (Text) But, we need them in Integer (Number) Format to work with them. So we do "int(NumberOne)" To convert NumberOne, from a String, to an Int(ergar)

 

On the next line, we're using the NumberOne and NumberTwo variables again, we're going to print them to show the sum the user entered. Then we're going to print the answer which we calculated on the last line (OutPut)

 

We have this written four times in the if statement, so it works like this:

 

if Oper == "+":
OutPut Equals NumberOne Plus NumberTwo
print NumberOne, "+", NumberTwo, "=", OutPut
elif Oper == "-":
OutPut Equals NumberOne Minus NumberTwo
print NumberOne, "-", NumberTwo, "=", OutPut
elif Oper == "*":
OutPut Equals NumberOne Multiplied By NumberTwo
print NumberOne, "*", NumberTwo, "=", OutPut
elif Oper == "/":
OutPut Equals NumberOne Divided By NumberTwo
print NumberOne, "/", NumberTwo, "=", OutPut
else:
If None Of The Above Is Entered, Then...
print "Invalid Operator!"

 

Hopefully you understand. If not, leave a comment and I will explain.

 

Now our last line is:

 

Close = raw_input("Press Enter To Close...")

 

This is because when the program is run in command line, after it's done the calculation it will think the program is over, and close it's self. What this line does is stop it closing until the user has pressed enter.

 

Now we just save and run. Well done, you've just made your first real program. It can also be run by clicking the "Calc.py" (Or whatever you saved it as) File. This will run it in command line, like in the picture I showed you earlier.

 

I hope you understood/learnt something while reading this...

 

 

 

Credits : +||+ = ME aka Vorfin

 

  • 3 weeks later...
Posted

Lol awesome TUT dude, you have +1 from me :D

Lol You Can't Give +1... ~.~...

 

Anyways Nice Share....You Got +1 From me.

Posted

First i see your post who call me noob , webm0nst3r clean up this topic already one time , also he fixed the credits and you change them again.

Posted

First i see your post who call me noob , webm0nst3r clean up this topic already one time , also he fixed the credits and you change them again.

This topic ? are you sure?

w3bmonter clean this topic > http://www.maxcheaters.com/forum/index.php?topic=145381.0

now give the karma back if you haven't got proofs or i will report you

-1 karma to you abusser

Posted

GIVE ME THE F*CKING PROOFS FROM WHERE I C/P IT!?!?!?!

Leetcoders is down , your name is george , + you dont have idea about programming what more?
Guest
This topic is now closed to further replies.



×
×
  • Create New...