Quantcast

Jump to content


Photo

[How To] Noobies Guide to Python


  • Please log in to reply
45 replies to this topic

#1 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 07 October 2010 - 10:26 PM

*
POPULAR POST!

Alright, so its 2:30 am and i feel like writing a tutorial. I read the admin note in the tutorials section and felt like it was mainly for neopets cheats and hacks. This tutorial will circulate around how to create simple programs in python 2.x. I use version 2 (over version 3) because of the differences implemented in this new version; long story short version 3 was completely rewritten and all of the bugs were ironed out. Version 2 is still fully supported by the python community and is still used to develop fully functional scripts :) Im not sure what version that the codex team uses but what ever, lets get this started :)

Installing Python
First you are going to need to go and get Python 2.7 (which is the current latest version for V2) [ LINK ]. Im not sure what operating system you are using (Im on linux) but this guide will work for all systems supported by python. So, download and install python and then open up the command prompt:
  • Windows: Start -> Run -> Type "cmd"
  • Linux: Applications -> Accessories -> Terminal
  • Mac: I have no clue >> not a mac person

Running an application
To run an application in windows, it is as simple as browsing to the directory where the script is located (navigation is done by typing "cd <path>") and just typing out the file name.
In Linux there is a few ways to do this. You type "python <file name>". If want another way you can include this line "#!/usr/bin/python" at the top of your file, allow the program to be executed as a program (type "chmod +x <file name>") then you can just type the file name "./<file name>" :)

Just an FYI to you mac users out there, I'm gonna stop talking about it because i REALLY have no idea how the mac system is layed out or how to program in one xP so the best of luck to you and just keep reading; there shouldn't be that much of a difference.

Choosing your editor
When i program on Linux, i just use the built-in editor gedit; it has nice syntax highlighting and formats the text nicely (Although i do change the tab width from 8, to 4)
On windows, i use Notepad++, it is just like gedit, but has a lot more functions (such as macros... and more), you can get it here [ LINK ]

Pythons Syntax
Before we actually start to code, you should know that python is whitespace sensitive. In most languages you define a block of code by surrounding it by brackets or braces. In python you increase and decrease the tab size (spaces work too).

Creating your first program
Now, open up your editor. Lets create the hello world program (as is customary when learning any new language)

print "Hello World"

Thats it; here is the break down of the code:
[code=auto:0]
print - Print is a built in method that prints what ever is following to the terminal
"Hello world" - is a string, in between the quotes is defined as a string

Sounds easy enough right :) Lets try another way of printing to the screen

import sys
sys.stdout.write("Hello World")

There are a few ways to print to the screen, this method prints without a new line at the end... if we wanted to add a new line we would add that at the end ( "Hello World\n" )

Loops
There are a few different ways that you can create a loop, but before that lets define what a loop is (and this is not a Websters definition, this is my own way of describing it xP). A loop is a quick and simple way of repeating a set number of tasks over and over again.
Here are a few sample loops, a # followed by text is a single lined comment, while ''' is a multi-line comment, you close it off with another '''

#A simple for loop with numbers
for x in range(5): #range(5) creates a list starting at 0, and ending at 4 (it has 5 items in it): > [0, 1, 2, 3, 4]
print x #We indent one so that we let python know this is part of the for block

#remove the indent, code after this is considered outside the block

print #prints a new line

#A simple loop using the list type
myList = ["one", "two", "three"] #This is how we define a variable
for listItem in myList:
print listItem #will go through each item in myList, and print the item

#Lets do a while loop
x = 0 #x is assigned a starting value of zero, it is an integer type
while x < 10: #We will continue this loop aslong as x has a value less than 10
print x
x += 1 #Will add 1 to x each time it is called


Python Methods
Python has a very extensive library and it would be impossible for me to go over all of them in one simple tutorial. For a list of them all visit the python libary page [ LINK ], i will give examples on how to use a few that might be popular in this forum, but other than that just ask for clarification and i can try to do my best :)

urllib2
This libary is used for opening web pages and other brickabrack... heres some simple examples on how to use it.

import urllib2

response = urllib2.open("http://www.google.com") #Opens the webpage
rawHtml = response.read() #Gets the html data

f = open('google.html', 'w') #Creates a new file called 'google.html'
f.write(rawHtml) #Writes the html data to the file
f.close() #Closes the file


If you want to know how to log into sites, save cookies and such, you'll have to wait... It is a MUCH harder process to do, it involves creating classes and functions. Not really suitable for this tutorial

Other notes
Im not really sure what else i can put in here to help you guys out... based on the comments i receive ill continue to edit this file and update it. I guess this is a good starting point to get you use to the language...

Happy Coding :)
~Cody Woolaver

Edited by Pyro699, 07 October 2010 - 10:28 PM.


#2 Noitidart

Noitidart
  • Neocodex Co-Founder

  • 23214 posts


Users Awards

Posted 08 October 2010 - 12:04 AM

Wow pyro THANKS!
This is really good. Top notch. I'm speechless. I'm thinking of starting python myself now. haha

#3 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 08 October 2010 - 12:15 AM

You should :) Its a very easy language to learn and master :) It is much easier to do than people think, and once you understand the methodology behind programing, you can overcome many tasks by viewing them as a programing problem ^^ Many of the silly tasks i have to get done i do with programming, such as organizing a folders, simplifying text, gathering lots of info from websites :) Its also very good for the brain ;)

~Cody

#4 5MGEDOHC

5MGEDOHC
  • 937 posts


Users Awards

Posted 08 October 2010 - 12:42 AM

haha was i the reason of this? oh and top notch dude +1

#5 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 08 October 2010 - 12:44 AM

Not just you ;) I read through the programming forum, and most of the questions were retaining to simple tasks. And most forums i visit that have a programming secion fills up with "how do i start", and these threads are usually quite popular.

~Cody

#6 iomega

iomega
  • 1070 posts


Users Awards

Posted 08 October 2010 - 01:08 AM

Thanks for the tutorial !

I've always been interested in programming, but never really had the time to learn.
i did a couple of IT electives that used VB.Net back in high school and more recently we had to learn some MATLAB code stuffs, but that never went anywhere

If you make any more tutorials, i think i might start learning as soon as classes are over. :)

#7 InsertRandomNameHere

InsertRandomNameHere
  • 340 posts

Posted 08 October 2010 - 09:14 AM

Crap... I'm so stupid I don't even know what's going on. I'm just following the guide

#8 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 08 October 2010 - 10:51 AM

What are you talking about xP Is there any area in particular your having problems understanding?

#9 WingswithBlade

WingswithBlade
  • 20 posts

Posted 08 November 2010 - 01:14 AM

Kind of an older topic but thought I should post in here instead of making a new topic.
can you make a tutorial on how to make a GUI on python like neocodex does it?

Ive kinda sort of got an understanding of using Tkinter and how to add a grid to it
I know of also using the padx and pady instead of a grid

so my question really concerns about what method neocodex uses it and could you just go more into detail of some of the important things I should know about when making the GUI

Edit:

found an older board about another user asking of GUI for python
topic http://www.neocodex....ui-programming/ for anyone that was wondering where it was

Edited by WingswithBlade, 08 November 2010 - 01:39 AM.


#10 SatansCamaro

SatansCamaro
  • 553 posts

Posted 08 November 2010 - 03:43 AM

Thanks. this really helps!!

#11 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 08 November 2010 - 04:04 PM

Kind of an older topic but thought I should post in here instead of making a new topic.
can you make a tutorial on how to make a GUI on python like neocodex does it?

Ive kinda sort of got an understanding of using Tkinter and how to add a grid to it
I know of also using the padx and pady instead of a grid

so my question really concerns about what method neocodex uses it and could you just go more into detail of some of the important things I should know about when making the GUI

Edit:

found an older board about another user asking of GUI for python
topic http://www.neocodex....ui-programming/ for anyone that was wondering where it was


GUI programming is not really something i like to do xD As im soon going to be part of the team im going to learn better methods on how to do that. I can create gui's pretty well but the only problem i have is running long tasks with the gui.

Your best bet it to use the wxPython library :) There are lots of guides on their site [Link] they even have one for the Long Running Tasks but i dont like any of their methods xD [Link]

Best of luck
~Cody

#12 Warlord.

Warlord.
  • 98 posts

Posted 08 November 2010 - 08:33 PM

Kind of an older topic but thought I should post in here instead of making a new topic.
can you make a tutorial on how to make a GUI on python like neocodex does it?

Ive kinda sort of got an understanding of using Tkinter and how to add a grid to it
I know of also using the padx and pady instead of a grid

so my question really concerns about what method neocodex uses it and could you just go more into detail of some of the important things I should know about when making the GUI

Edit:

found an older board about another user asking of GUI for python
topic http://www.neocodex....ui-programming/ for anyone that was wondering where it was


Along with what Pyro said, here's a good programming help thread that has different python GUI examples:

http://www.daniweb.c...read191210.html

And here's one for just wxPython:

http://www.daniweb.c...read128350.html

GL :)

Edited by Warlord., 08 November 2010 - 08:35 PM.


#13 Zeeknon

Zeeknon
  • 63 posts

Posted 28 December 2010 - 04:48 PM

why don't you teach people how to login to neopets with python and stuff

#14 nonotjj

nonotjj
  • 134 posts

Posted 29 December 2010 - 07:22 AM

why don't you teach people how to login to neopets with python and stuff


login to neopets with python?... easy, see below.

import urllib, urllib2, cookielib

# neopets login information
username = ''
password = ''

# user agent string for Firefox 3.6 running on Windows XP
useragentStr = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) Gecko/20101203 Firefox/3.6.13'
# cookie jar to make sure once we log in, we stay logged in.
cookiejar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)

# url where the post data will be sent.
loginURL = 'http://www.neopets.com/login.phtml'
# post data
loginData = urllib.urlencode({'destination':'', 'username':username, 'password':password})
# custome headers containing the user agent string defined above, as well as a referer address.
headers = {'User-Agent':useragentStr, 'Referer':'http://www.neopets.com/login/'}

req = urllib2.Request(loginURL, loginData, headers)
handle = urllib2.urlopen(req)

print 'Current URL, to make sure we get redirected: '
print handle.geturl()

print 'Here are the headers of the page: '
print handle.info()

print 'Cookies so far: '
for index,cookie in enumerate(cookiejar):
  print index, ' : ', cookie

Edited by nonotjj, 29 December 2010 - 07:25 AM.


#15 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 08 February 2011 - 09:13 AM

why don't you teach people how to login to neopets with python and stuff

Because i dont like competition ;) Seroiusly though, its because unless you know what your doing, you pose a great threat to your account. Having code that is logically flawed could potentially alert neopets to susputious activity. Say your program has a bug... and it keeps reloading the same page, over and over and over without you there to notice it... if you load the same page 10000 times in a day... it will look bad :p

login to neopets with python?... easy, see below.

Spoiler

While that is most defiantly a way to do things, you are missing a big piece of the puzzle. If i was to use that script to open up web pages things would start to get very very clunky and messy. The best option is to create an entire url wrapper which takes care of all the data handling; such as headers, redirects and history.

#16 nonotjj

nonotjj
  • 134 posts

Posted 08 February 2011 - 09:48 AM

While that is most defiantly a way to do things, you are missing a big piece of the puzzle. If i was to use that script to open up web pages things would start to get very very clunky and messy. The best option is to create an entire url wrapper which takes care of all the data handling; such as headers, redirects and history.

I took Zeeknon's question literally, so put together something that would

... login to neopets with python and stuff

but I see what you mean about having a wrapper to keep things from getting messy..

Why keep track of web history?.. just for the 'Referer' http header?...

#17 ilovepolkadots

ilovepolkadots
  • 724 posts

Posted 23 April 2011 - 05:03 PM

if anyone is interested in programming with Python on their Mac in OS X i suggest using Komodo Edit
http://gb.cs.unc.edu...-macosx-x86.dmg
(^)

it is very easy to use
although it does not have an obvious {run} button
you can program a key board short cut

#18 Neoquest

Neoquest
  • 1760 posts


Users Awards

Posted 23 April 2011 - 05:57 PM

Wow, I thing I'll try this out, thanks for the awesome guide. Hopefully I'm not too much of a noob for this.

#19 Kway

Kway
  • Proud to be a Brony

  • 1242 posts


Users Awards

Posted 26 April 2011 - 09:10 AM

if anyone is interested in programming with Python on their Mac in OS X i suggest using Komodo Edit
http://gb.cs.unc.edu...-macosx-x86.dmg
(^)

it is very easy to use
although it does not have an obvious {run} button
you can program a key board short cut


Either that or you can download Xcode from Apple at http://developer.apple.com/xcode/. You can log in with an iTunes account or create a new one and it is free (only the really new beta stuff requires money). Unfortunately it is Mac only (no iphone/ipad simulators for Windows users).

It is a huge download (almost too big for a DVD) but if you are going to program in other languages, Xcode will most likely support it. All the fancy tools are for Obj-C but you can tell it to run the script with python if you want. You will be needing Xcode anyways if you are planning on building packages manually via command line (fancy words for installing extra python stuff that doesn't have an installer).

#20 huevoquilmes

huevoquilmes
  • 245 posts

Posted 28 April 2011 - 07:37 PM

Really nice guide. I have a question, in your experience, if I were to do a program, lets say an Aber, which is faster? python, java, equally fast?

#21 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 21 May 2011 - 05:58 AM

Go with python, easier to write... and the speed difference (not sure who would win) would be in the millisecond range, and you dont want tobuy an item as fast as possible, you do need to wait at least 1 second.

#22 huevoquilmes

huevoquilmes
  • 245 posts

Posted 21 May 2011 - 06:34 AM

i started yesterday to work with the PIL... im too used to java, but ill give it a try :p

#23 Pyro699

Pyro699
  • 1542 posts


Users Awards

Posted 21 May 2011 - 06:36 AM

Awesome :) Feel free to ask any questions you may have and ill do my best to answer them :)

#24 huevoquilmes

huevoquilmes
  • 245 posts

Posted 21 May 2011 - 06:43 AM

great i will.. thxs a lot bro ;)

#25 lonewolf

lonewolf
  • 243 posts

Posted 08 July 2011 - 09:36 PM

Thanks for this :) ill try it out tomnight good effort!


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users