views:

118

answers:

3

After we write a code in Matlab we can use ctrl+A+ctrl+I and ctrl+A+ctrl+J to format our code (comments, loops alignment etc). Is there something similar or any helpful keyboard shortcuts in Python?

Also, just like we can use upward arrow to copy our previous command window history in Matlab, is it possible or some keyboard shortcut to do that in Python?

Thanks!

+6  A: 

Python is a programming language, not an integrated development environment (IDE), therefore it has no "keyboard shortcuts" or the like. Each given development environment may offer different facilities or the like. You appear to consider GNU Readline (typically used in the simple text-mode interpreter environment that many Python executables bundle) as "part of Python" -- but that's a misconception; readline is a perfectly general library for interactive input in command-line environments, and Python only one of the many programs using it. Another environment usually bundled with Python is IDLE, a GUI one, and of course the editing facilities are completely and drastically different. There are many third-party environments such as "Wing IDE" and each offers a drastically different set of editing features and facilities from all the others.

To summarize: your question makes no more sense about Python per se than it would about (say) C, or Java, or any other programming languages. Don't let (usually proprietary) programming languages that come with bundled IDEs confuse you on this subject!

Alex Martelli
@Alex: Thanks. If I write a python code in notepad++, then how do I run it? I checked the python add-on in notepadd++, yet when I run with F5 it doesnt run the code. So right now I have to write the program in python's own editor where I can run the code on pressing F5.
Harpreet
The simple solution is to use the bundled "IDLE GUI" that you should have in your Python folder. Then, if you are writing a script, F5 will run it.
Rafe Kettler
@Rafe: How to run when writing code in some other editors, like notepad++? Also, if how can I open my .py file (the python's editor where I wrote my code)? Because if I double click that it just shows the executable and doesn't opens the file. To see the content of my code I have to right click and open it in wordpad.
Harpreet
@Harpreet: I'm fairly certain that the only program that will really integrate into Python is the IDLE. If you're on Windows (which is what I suspect), if you want to view the code you right click and select "Edit with IDLE." Notepad++ is great for C and others, but for Python IDLE is the best (it wouldn't be included with Python if it weren't).
Rafe Kettler
@Rafe: Yeah that works, however I still wanted if I could open the IDLE with just one left click :)
Harpreet
"with just one left click " -->sorry, I mean double click.
Harpreet
Alex Martelli
You can run Python programs from Notepad++. Go to the Run menu and and choose Run (or just press F5). If you're on Windows (which it sounds like), then you enter 'C:\Python26\python.exe "$(FULL_CURRENT_PATH)"'. That's for Python 2.6 and you would change it for other versions. You do need the double quotation marks around the second part of that. Then click on Save, enter a name for this (like Run Python) and select a key combination. I use F6. Press OK and you can now run the currently open Python script by pressing F6 or selecting "Run Python" from the Run menu.
Justin Peel
@Justin: works fine. Thanks!
Harpreet
+1  A: 

If you use emacs, then

  1. you can press tab anywhere on a line and it will properly indent that line relative to the preceding line (making the assumption that the current block is continuing).

  2. you can mark selections of text and press C-c < and C-c > to move blocks of text left and right.

These are the two that I actually use with any regularity. I'm sure that anything that any other editor can do, emacs can do too ;)

on the whole, formatting python code is difficult for a program to do because the indentation drastically affects semantics.

consider

for i, item in enumerate(lst):
    if i % 2:
        sum += i * int(item)
    return sum

and

for i, item in enumerate(lst):
    if i % 2:
        sum += i * int(item)
return sum

Do you really want your editor deciding which one you mean?

aaronasterling
+1  A: 

If you use the Python IDLE (comes with Python on Windows, readily available on Linux and Unix flavors), most of the formatting work is done for you. For instance, IDLE automatically indents loops and any other code block after a :. This is far better than writing Python scripts in a standard text editor like gedit, emacs, vim, or Notepad, especially since you can simply press F5 to run the script.

As for previous commands, the biggest disadvantage to the Python shell is that you cannot press the up arrow to get the last command. However, if you use the non-GUI shell (in the Windows command prompt or a Unix terminal, the command is python), you can use the shell's command recall to get the last command.

Rafe Kettler