2

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!

  • 4
    You would be better off looking at [this post](http://stackoverflow.com/questions/81584/what-ide-to-use-for-python) for people who argue over which IDE is better for python, as they may discuss keyboard shotcuts there. – inspectorG4dget Sep 29 '10 at 03:05
  • pydev with eclipse is greate IDE for python development, you just need to configure it correctly...and use iPython too – shahjapan Sep 29 '10 at 05:11

3 Answers3

7

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
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 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 Sep 29 '10 at 03:15
  • 1
    @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 Sep 29 '10 at 03:24
  • 2
    I believe that the question "how do I tell my OS to set up an arbitrary (Python-coded) program to run on a double-click on an Icon" is an OS-specific and non programming question -- the solution will be different between (e.g.) ubuntu, kubuntu, windows, macs, &c, but will **not** be at all different whether you're developing the Python program or just a user of it. So I suggest asking on superuser.com, clearly specifying the exact set of platforms of interest. – Alex Martelli Sep 29 '10 at 03:40
  • 1
    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 Sep 29 '10 at 03:51
1

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
  • 68,820
  • 20
  • 127
  • 125
1

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
  • 75,757
  • 21
  • 156
  • 151