Python 2.7 Tutorial

Python How ToI asked you guys what video I should do next, and based on your votes you wanted a Python 2.7 Tutorial! Because of the tremendous interest, I will be rolling out a Massive tutorial for Python. I actually did one on Python 3.0 if you want that instead though. Here it is Python How to Video Part 1.

The tools I use in this tutorial include the Eclipse IDE and Pydev. I document how to install them in the video below, but also in this article Free IDE.

Now on with the video. All of this code will work with Python 2.5 thru 2.7. The code will follow the video. If you have any questions or comments leave them below. Here is all the code from the entire tutorial in a zip archive.

If you find this article helpful, please click here so more people can find this πŸ™‚

#! /usr/bin/python

# You don’t define variables in Python. They are chosen based on the value assigned

from math import sqrt

integerEx = 8
longIntEx = 22000000000000000000000
floatEx = 2.2
stringEx = “Hello”
booleanEx = True

print type(integerEx)
print type(longIntEx)
print type(floatEx)
print type(stringEx)
print type(booleanEx)

# Boolean Examples

booleanTwo = False

print booleanEx and booleanTwo
print booleanEx or booleanTwo
print not booleanTwo

# Number Examples

intOne = 7
intTwo = 99
floatOne = 7.9
floatTwo = 9.8

print intTwo / intOne # This should be 14.14
print float(intTwo) / float(intOne)
print int(floatOne)
print int(booleanTwo)

print intOne > intTwo
print intOne >= intTwo
print intOne < intTwo
print intOne <= intTwo
print intOne != intTwo
print intOne == intTwo

print intOne + intTwo
print intOne – intTwo
print intOne * intTwo
print intTwo % intOne
print intOne ** intTwo

print id(floatOne)

# Math Modules

print sqrt(intOne)

# Input from users

answer = raw_input(“What is your name? “)
print “Hello ” + answer

# String Basics

strOne = “Hello”
strTwo = “World”

print strOne, strTwo
print strOne + strTwo

longStr = ”’This is a very long string that \
goes on forever and ever”’

print longStr

print longStr[0:3]

# \\Β Β Β Β  Backslash (\)
# \’Β Β Β Β  Single quote (‘)
# \”Β Β Β Β  Double quote (“)
# \nΒ Β Β Β  ASCII Linefeed

53 Responses to “Python 2.7 Tutorial”

  1. Jason says:

    I am slowly figuring things out. I’m not for sure what Eclipse to download from the vast list given on their web-page. I am trying to use regex within Python if that helps. I did not see anything for Python. Any suggestions?

    • admin says:

      I use the classic version of Eclipse. To program in Python with Eclipse you need to install a module called Pydev. I go into how to install that in this article and video http://www.newthinktank.com/2010/10/python-2-7-tutorial/

      • NYPrince says:

        Hi Derek how are you? Sorry if I’m a little thick headed but I’m watching you setting up your Python environment in eclipse and you rushed through it saying ok ok ok but I feel its very important to let novices like myself know and learn how to properly setup things in eclipse, heck that’s a small tutorial in itself working with eclipse. You know stuff like knowing how ti import libraries and short cuts that eclipse has. Anyways bud that’s for your great work I appreciate it

        • admin says:

          Thank you πŸ™‚ I’ll look into making an Eclipse tutorial because I’ve been getting these messages a lot. I think it is because the new version of Eclipse (Juno) is buggy and the plugins aren’t working. Definitely stick with Indigo. I’ll see what I can do with the tutorials, but it seems to be a windows issue and I only have a mac and linux machine

  2. tyebud says:

    Great tutorial so far. I had one quick question. What modifications have you made to your perspective? I tried adding a console view to my right sidebar, but the only relevant option is the python console which doesn’t produce the same view as shown.

    Thanks again and keep up the great work!

    • admin says:

      I clicked and dragged the console over to the right side of the screen and it snaps in place. I think that is what you were looking for. Thanks for the nice comment

  3. Mindaugas says:

    Hello, very nice guide so far, but Im having some questions about setting this UI. I’ve got everything you showed in the video, installed, but its still not the same πŸ™‚ Id like Python to color my key-words and maybe a font and size like yours. So a bit more info on your settings would be just awesome.

  4. Mindaugas says:

    I dont know, maybe Im stupid, but I cannot setup it.
    After I did everything you showed in the video I just tried it and after hitting “Run” button I get:

    “Reference to undefined variable GOOGLE_APP_ENGINE”

    or some other crap, but I dont get result in the console.

  5. I says:

    Awesome tutorial, give us more!

  6. Frontware says:

    Great tutorials serie. We give it to our new programmer to get basic Python knowledge.

  7. James says:

    i need help with this wat do i need to fix

    # This program says hello and will ask for ur name.
    print(‘Hello who ever this may be!’)
    print(‘What is your name?’)
    myName = input()
    print(‘It is good to finally know your name, ‘ + myName)
    id apprieate ur help
    thank

  8. Remi Brisson says:

    HI Derek. Great tutorials. Very straight to the point and clear. One question: I’ve been working on IDLE up until now but want to switch to ECLIPSE as you make it look like it is so much better. I have Eclipse 3.7.2 downloaded and have PyDev installed. I have an issue with assigning the Python Interpreter though. An error pops up when I try to open my Python 2.5 executable. Is this too ld of a version for my version of Eclipse?

    Thx Remi

    • admin says:

      Thank you πŸ™‚ You should have at least Python 2.6 to use this tutorial. Make sure you have the most up to date version of Eclipse. I use Eclipse Classic 3.7.2. If you have any problems you probably need to update Pydev. Click on Help and Check for Updates to get the new version

  9. Bill says:

    Hi Derek i’ve in and out this site for quite some time.Until now with you’re instruction i’ve been able to hadle most of my projects.Today i’m trying to learn some python, but when installing pydev, i get the same error over and over connection to to fetch jars has timed out.i’ve tried everything.please help πŸ˜€

  10. Rob says:

    I find this tutorial frustrating.

    You show the code in clear, basic terms, but you assume we can install the software and plugins and updates ?

    I tried for half hour to get started on this and I am getting no where.

    I downloaded eclipse classic and I installed the pydev software via eclipse, but eclipse doesn’t recognise the python interpreter.

    I am using eclipse 4.2.0 and the latest version of python 3.2.3. I also tried python 2.7.3, no to avail.

    I’m bailing.

    • admin says:

      I’m sorry it didn’t work for you. You need to use Python 2.7. Python 3 won’t work and I have a tutorial on Python 3. Many things have changed since I made this and the Pydev plugin is now a little buggy. Sorry about that

  11. Tamal says:

    Hi. Thank you very much for your awesome tutorials. I would request you if it is possible for you to make a video on F2Py. Specially on calling fortran programs from python and make gui on top of it. It is a very interesting topic on scientific community now a days so that they can use their old fortran code with an interface.I highly appreciate your effort in making python Tutorial.

  12. Khodary says:

    Can you show me how to code toontown online?

    • admin says:

      I’m going to be making a bunch of games for Android devices very soon. I’ll cover most every genre. Because everything will be written in Java, all of the games will work on any desktop as well. I hope you enjoy them. To get a head start check out my Java tutorials

  13. Jonathan says:

    I am having trouble installing the PyDev module. I typed in pydev.org after going to help>install new software. But, nothing comes up.

    • admin says:

      Click Help > Install New Software in Eclipse

      Click Add Site Button

      In popup type Pydev and Pydev Extensions in name

      type http://pydev.org/updates in Location

      Check both Pydev updates. You may have to uncheck Contact all update sites during install to find required software, but try it with it checked first.

      Hit Ok a bunch of times

      Tell me if that didn’t work

  14. NYPrince says:

    Hey Derek how are you? Is there anyway you can convert your python Tuts into HD? Can’t see it clearly and by any chance my friend do you know Ruby and Ruby on rails?

    Thanks Bud

    • admin says:

      Sorry, but I recorded them in 720p years ago and there is no way to record them in 1080p. YouTube used to limit the size of your videos back when I made those. Yes I know Ruby and will cover it, but at the moment I’m working toward making Java / Android Games and Apps. I may slip a Ruby tutorial in though because I really want to cover it. Thank you for the request πŸ™‚

  15. BaySun says:

    Hi Derek, it took some trial and error getting PyDev working on Windows 7 inside of Eclipse Indigo, but I am up and running. I notice on “Python 2.7 Tutorial Part 2” as you type… some auto-complete looking sub-menu appears. How does one activate that feature?

  16. ChrisK says:

    hey Derek,
    I’ve installed eclipse and pydev as per your instruction.
    I then began your tutorial #1. but when I “run” the console displays only:
    Finding files… done.
    Importing test modules … done.

    ———————————————————————-
    Ran 0 tests in 0.000s

    OK

    Derek? What am I doing wrong?

  17. Hi Derek

    I very much enjoyed tutorials/ My Question is this on the first video setting up the python pyDev on Eclipse I am pleased to say I had no problems. The only thing is the folder you put all your work in how should you name the folder and where should you put the folder so I can follow your python tutorial 2.7 or which file path should I specify for python 2.7.

    Thanks a lot

    D Calder

    • Derek Banas says:

      Hi Derick,

      The name of the folder is up to you. There isn’t really any rule for that. It is normally best to create the folder in your Documents folder, but that is up to you. The only issue to look out for is to make sure you have permission to execute from that folder. I hope that helps πŸ™‚

  18. Hi Derek

    I went back on eclipse and I found this in the console

    Finding files… done.
    Importing test modules …

    done.

    Thanks very much

    D A Calder

  19. Hi Derek

    Every thing is working know I had to do lot of trouble shooting

    with the configuration Run and for the Run in the tool bar.

    Thanks again for your tutorials

    Derick Alphonso Calder

    PS Great

  20. kishon diaz says:

    you are a genius derek seriously

  21. HI Derek

    Just an update I am following Your Other Tutorials on PHP, JavaScript. HTML CSS and python.

    Thanks Again from

    Derick Calder

    PS Great Great Tutorials

  22. Wayne says:

    Hello Derek,
    I have been wanting to learn Python for quite a while and have been looking for a group of videos and found yours. I really appreciate the time and effort you put into making them. My only complaint is with using Eclipse. I spent many hours going over documentation to try and get the thing working properly. I still can’t get the console to work. It would be really helpful for us beginners if you would have a video about using Eclipse. It has been the most frustrating time I have ever experienced with any program.

    • Derek Banas says:

      Hello Wayne

      I’m going to make a series of tutorials set around teaching languages in 1 30 minute video. I’ll see what I can do about Eclipse. I know it can be frustrating.

Leave a Reply

Your email address will not be published.

Google+