Setting up python to start learning
So I am interested in learning to code in python. I started with html and css which requires no set up just using gedit to write my code and testing on chromium. I was recently told to learn to do more with html and css. I would need to get a setup for java and start learning some of that.
However I am more interested in learning python. So far all I know Ubuntu 12.10 comes with a version of python. I need to know what would it take to get things set up to the point I can actually open python 3.3.0 on Ubuntu 12.10 and starting trying out commands?
I'm not sure the suggested duplicate quite applies at the moment. Fact is you don't need to set up virtualenv environments or follow best practices to get up off the ground and personally, I think trying to do so will scare you off Python, which is a beautiful language and a very powerful community of tools.
To get a brief glimpse at Python, you can just run python
in a terminal and you're dumped into a live python environment. You can play around with code examples to your heart's content.
For bigger things (or when you get fed up of the interactive python console's limitations) you can write Python scripts very easily. Write a file in Python syntax, and then just run python filename
and it'll run. Simples.
If you really want to learn Python, I seriously recommend two things:
-
http://learnpythonthehardway.org/book/ - It sounds tough but it's not. The setup section for Linux has a really practical way of getting
gedit
up to speed too, so don't miss that out. -
The O'Reilly Python Pocket Reference - This probably only applies if you have experience with other programming languages. I was forever trying to work out how dicts and tuples differed from other languages' arrays and the pocket reference is great for looking it up quickly. Better than Google in my experience. Again, if you don't know what an array is, this might not be much help for you.
Once you advance to projects where you might need best practices, that's the time to start looking at virtualenv
(mostly server deployment stuff) or frameworks or anything like that.
Most importantly: have fun.
A note of warning on Python 3.x
A couple of people are suggesting installing (if you need to) and using Python 3. I disagree with this stance. As the asker of the question you're probably completely unaware of the differences between the two or even that there were two versions. That's not your fault - that's just what being a beginner is all about.
Python 3.x has been around for years already but everybody still uses 2.x. I'm serious. Other people may suggest that Py3k is "good enough to learn on" but I posit that at least 90% of all the good tutorials and documentation out there is for Python 2 and trying to follow it in Python 3 will result in tears.
And as you progress and want to pull in external libraries (the really fun bit in Python!) you're going to find that only a tiny slither of the community have migrated over. Inevitably the thing you want to use hasn't been ported and you're left in a pool of your own tears again.
So I know it's 2013 and we're all half-robots floating in space, but take some advice from a cranky Python developer: stick with the old stuff for now. Things for Py3k will improve and probably within the next couple of years it'll be safe to use...
... by which time Python 4 will be released. C'est la vie.
Python3
To just start playing with python just type python
or python3
in a terminal. The first starts python 2, the latter starts python 3. There are two major versions of python (namely 2 and 3) and they differ from each other. In fact, the python developers decided to thoroughly review a number of things in python 3. On a fresh 12.10 install, you normally of both already installed. You'll see something like this (for python 2):
Python 2.7.3 (default, Apr 10 2012, 23:31:26)
Type "copyright", "credits" or "license()" for more information.
>>>
Then you can do things like:
1 + 2
or
a = 1
b = 4
a + b
or write functions
def multiply(a,b):
return a * b
and use them
multiply(a,b)
You can save these functions and calculations in a file. Usually you name them someName.py
. You can then make that file executable by typing chmod 700 someName.py
or you can play with the file in an IDE (see below). If the file is executable you can run python someName.py
to run the file with python 2 or python3 someName.py
to run it with python 3.
Integrated Development Environments
To start learning python, the easiest way is to start with what is called an IDE
, that is an integrated development environment. Don't worry about all the ways in which you can use an IDE. IDE's allow you to save commands in a file and excute them easily.
You can install IDLE (sudo apt-get install idle
), which is the default python IDE. Or you can use geany, reinteract, gedit or whatever.
What an IDE does is really help you to manage your files and your commands.
One of the best places to start is the official python tutorial for python 2 or python 3, I think.
Enjoy.
bpython
If you want to get acquainted to python using the interactive python interpreter, I'd suggest installing bpython
, which is an extension of the stock python interpreter. It offers the following features (taken from its website):
- In-line syntax highlighting.
- Readline-like autocomplete with suggestions displayed as you type.
- Expected parameter list for any Python function.
- "Rewind" function to pop the last line of code from memory and re-evaluate.
- Send the code you've entered off to a pastebin.
- Save the code you've entered to a file.
- Auto-indentation.
- Python 3 support.
Installation:
sudo apt-get install bpython
Run it:
bpython
As for Python 2 vs. 3:
Ubuntu includes version 2.7, which is the last iteration of the 2.xx branch and includes many features of python 3. I'd say stick to python 2 (mostly because of the vast amount of libraries not ported to python 3 yet), but if you run scripts, use the -3
switch to be aware of the problems that could arise if you ran the script on python 3.
E.g.:
python2 -3 script.py
There is a nice website where you can see differences between the major python versions: http://docs.pythonsprints.com/python3_porting/py-porting.html
One more point to note is that default python version on Ubuntu is Python 2.7.x and not Python 3. However you can install Python 3 by running this command:
sudo apt-get install python3
To start it in interactive mode, just run python3
.
You might want to check out coursera.org they have many online courses, including a python one starting soon. kahnacademy also has python lectures. python.org has nice tutorials also... Scott