(Disclaimer: I have never written a tutorial worth anything. This does not try to explain programming concepts, and instead focuses on input/output. First post is basics. Will try to continue into external modules, since those are where all the power lies.)
So they say that Python is awesome.
But let's just give the tour, and you can decide.
This will be fairly thorough, but you absolutely should not try to absorb everything at once. Install Python, get the basics of the command line, and meet me at the next heading, if you don't feel like it's time for all those packages and complex command line features.
Here are some important links:
Python official site: http://python.org
Installer for Python 2.7: https://www.python.org/downloads/release/python-2710/
Googling "Python Tutorial" might also come in handy.
Lots of Python libraries, particularly accessibility-related: http://hg.q-continuum.net/
- Specifically, accessible_output2 is the most straightforward way to use text-to-speech and braille output: http://hg.q-continuum.net/accessible_ou … ve/tip.zip
You might also want Pygame: http://pygame.org/
Or Pyglet: http://pyglet.org
And if you do anything complex enough, you'll want to find Numpy or Scipy, both of which are great if you wan to do low-level audio manipulation.
(People who have spent more time with Python will tell you that Pip is an excellent way to easy-install packages. I haven't bothered, so can't comment.)
Anyway, let's get on to the instructions.
It is advisable to be comfortable using the Command Prompt. You can get away with a few basics easily enough:
- you can find the command prompt in Windows, either by using the built-in search feature, or by typing "cmd" or "command" into the run dialog (windows+r). It's probably in c:\windows\system32\cmd.exe.
- The most common commands:
cd documents
^ changes directory to documents, if possible.
cd ..
^ Moves to the parent directory. SO if you're in c:\my documents, and type cd .., you will move to c:\.
c:, d:, e:, f:, etc
^ change drive. You need to do this explicitly instead of trying to use cd /d.
cd /d c:\users\
^ change directory, given the absolute path.
pushd c:\users\
^ Pushes the current directory onto the stack, then changes to c:\users\. This is useful if you'll want to go back to the previous directory later.
popd
^ Returns to the last directory on the stack.
That's pretty much it. You can use tab and shift+tab to cycle through files and folders, including searching or autocompleting based on what you've already entered.
And any file or directory name with spaces or special characters should be surrounded in quotes. So if you want to move to c:\my documents, you'd type:
cd /d "c:\my documents"
Entering a filename and pressing enter will have windows try to open said file.
Some programs take extra command line arguments. This is relevant here, because a lot of packages use a setup.py script, and you need to cd into the corresponding directory and type:
setup.py install
And it will either install the package, or tell you what other packages you need to hunt down. Luckily, just about everything you'd need will be either at one of the above links, or at http://pypi.python.org/
If you want to mess with environment variables, you can set it so that you can run the python console by just typing "python" at the command line. But I'm too lazy for that, since I just installed python in c:\python27.
Getting started
I'm assuming you have the python console up and running. If not, try typing this at the command line:
c:\python27\python.exe
The important part is running python.exe, from wherever it installed.
If it works, it should display a message with version number and OS information, then leave you at a prompt that looks like this:
>>>
I have no idea how to save console sessions. It's probably something straightforward once you figure out what the session is called internally. But, basically, I treat the console as a place to test and debug things.
Anyway, basics of syntax:
C:\Users\CAE>c:\Python27\python.exe
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello, jys"
Hello, jys
>>> my_number=5
>>> my_number
5
>>> print my_number
5
>>> print "My number is " + my_number + "."
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "With the str function, we see that my number is " + str(my_number) +
"!"
With the str function, we see that my number is 5!
>>> a=my_number+1
>>> a
6
>>> b=a/2
>>> b
3
>>> c=b/2
>>> c
1
>>> c=b/2.0 # explicitly use floating point division.
>>> c
1.5
>>> a%my_number
1
>>> b*c
4.5
>>> type(a)
<type 'int'>
>>> type(b)
<type 'int'>
>>> type(c)
<type 'float'>
>>> if a>b:
... print str(a) + " is greater than " + str(b)
... else :
... print str(a) + " is not greater than " + str(b) + "!"
...
6 is greater than 3
>>> while a>0 :
... a-=1
... print str(a)
...
5
4
3
2
1
0
>>> for i in xrange(10) :
... print i
...
0
1
2
3
4
5
6
7
8
9
>>> my_list=[2, 4, 8, 19]
>>> for i in my_list : print i
...
2
4
8
19
You should be able to type everything after >>> and get the same output.
Python detects blocks like if, else, while, and for, by indentation level. This is not as hard to keep track of as it sounds, though I still feel better adding a comment at the bottom, to indicate what block just closed.
Python comments work in two ways:
single-line comments begin with #
>>> #This is a comment.
And multi-line comments use tripple quotes:
"""
This is a multi-line comment.
These are usually used for documentation, at the start of modules, functions, classes, etc.
"""
Speaking of documentation:
>>> def my_function(a, b) :
... """Prints the sum of a and b. """
... print str(a+b)
...
>>> help(my_function)
Help on function my_function in module __main__:
my_function(a, b)
Prints the sum of a and b.
>>> my_function(2, 2)
4
Anything with halfway decent documentation can be accessed using the help function.
Anything without decent documentation can be accessed, if all you want to see is function definitions and such.
You could type help(int), or help(5), but I don't recommend it.
Ho, a game!
I'm doing this from scratch, and don't remember everything. Watch as the help function saves loads of time:
>>> import random
>>> help(random)
Help on module random:
NAME
random - Random variable generators.
FILE
c:\python27\lib\random.py
DESCRIPTION
integers
--------
uniform within range
sequences
---------
pick random element
pick random sample
generate random permutation
distributions on the real line:
------------------------------
uniform
triangular
normal (Gaussian)
-- More --
>>> help(random.random)
Help on built-in function random:
random(...)
random() -> x in the interval [0, 1).
>>> def guess() :
... """ Generates a random number, and asks the user to guess it."""
... answer=int(10.0*random)+1 # from 1-10
... tries=0
... response=-1
... while response!=answer :
... print "Guess:"
... response=int(raw_input())
... if response<answer : print "Too low"
... elif response>answer : print "Too high"
... else :
... print "Correct!"
... tries-=1
... tries+=1
... print "You completed the game in " + str(tries) + " tries"
...
>>> guess()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in guess
TypeError: unsupported operand type(s) for *: 'float' and 'module'
Awe, crud, it seems I used random() when I should have used random.random(). But don't worry, for you can repeat previous lines with the up arrow key.
(Also, this is why you usually type programs into notepad.)
>>> def guess() :
... """ Generates a random number, and asks the user to guess it."""
... answer=int(10.0*random.random())+1 # from 1-10
... tries=0
... response=-1
... while response!=answer :
... print "Guess:"
... response=int(raw_input())
... if response<answer : print "Too low"
... elif response>answer : print "Too high"
... else :
... print "Correct!"
... tries-=1
... tries+=1
... print "You completed the game in " + str(tries) + " tries"
...
>>> guess()
Guess:
1
Too low
Guess:
7
Too high
Guess:
3
Too low
Guess:
5
Too low
Guess:
6
Correct!
You completed the game in 4 tries
OK, since we're using randomness, now, you shouldn't get the same outputs. Also, I have no idea why I opened by guessing 1. (The correct move is 5.)
Also, you generally shouldn't use print, then raw_input, the way I did here. I am out of practice, as you can plainly read.
You can do this instead:
>>> raw_input("Echo:")
Echo:Moose
'Moose'
Well, that was fun.
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
George... Don't do that.