You are not logged in.
Pages: 1
Hi,
I was trying out python today, reading though some tutorials and such, and I came across a problem.
I coded a simple program that outputs text and variables. It reads:
#!/usr/bin/python
# Filename: expression.py
length = 5
breadth = 2
area = length * breadth
print('Area is', area)
print('Perimeter is', 2 * (length + breadth))
and I get this output:
('Area is', 10)
('Perimeter is', 14)
With the parentheses and all. I re-checked the code and even pasted the original code from the tutorial into the file, but still no luck. When I take off the parentheses, it reads:
Area is 10
Perimeter is 14
which is exactly how it is supposed to look...am I not supposed to put parentheses? --Any help is greatly appreciated.
-dellwood
EDIT: I'm using python 2.6 and IDLE, if that helps
Last edited by dellwood (2009-03-11 23:20:37)
Offline
*edit*
nevermind... see Dusty's reply below
Last edited by Xyne (2009-03-08 17:34:22)
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
Xyne is right. But if you want to use print as function in Python 2.6, you need to add this line at the top of your program:
from __future__ import print_function
Offline
Thanks -- I have tried updating to 3 (pacman -S python3), but when I check the python version (python -V), it still says I am using 2.6. Is there a way to fix this (sorry if this is off topic for this forum)?
-dellwood
Offline
How are you compiling/running it? In Arch, you'll need to explicitly specify python3 on the command line when you run the program.
Easiest way is just to show ya...
[andrew@phb ~]$ python -V
Python 2.6.1
[andrew@phb ~]$ python3 -V
Python 3.0.1
[andrew@phb ~]$
The packages have not been switched yet, so until then, the "python" command still points to python2. You'll just need to run your programs with python3 if thats what you want.
Hope that helps!
-- Drew
Offline
I use parenthesis in python26 without actually importing the printn function all the time; the trick is to also not use the comma operator, which turns it into tuple. Then they just act like a grouping operator and get ignored when its output.
I do this to get in the habit -- I write print a lot, and I have been forcing myself to switch to print( for when I finally get to deal with dependencies that work on 3.0.
Try either of these instead:
print('Area is' + str(area))
print('Perimeter is' + str(2 * (length + breadth)))
print('Area is %d' % area)
print('Perimeter is %d' % (2 * (length + breadth)))
That latter format is preferred as its more efficient. %d means integer; google for python print format for more info.
Dusty
Offline
Thanks for the responses. Drew, are you sayin just
python3 -V
before running IDLE or do I need to configure something in IDLE for it run python 3?
-dellwood
Offline
EDIT: This is in python26, which *does* accept print as a function without any imports, as dusty mentioned.
Damn, dusty beat me to it.
I'll add that ipython really hates print without parenthesis (when you go to repeat the command using 'rep' or the up key, you'll get an indentation error), so this is a great habit to get into.
It makes quite a bit of sense if you think about it. Under any other circumstance, writing a, b is equivalent to (a, b). It was only print which changed this behaviour.
Another way to get what you want is to use join...
' '.join(['string', 'separated', 'by', 'commas'])
Which puts the spaces at the commas as you'd expect. The downside is everything in the list needs to be a string.
print(' '.join(['Area is', str(area)]))
Last edited by buttons (2009-03-08 15:58:51)
Cthulhu For President!
Offline
Thanks for the responses. Drew, are you sayin just
python3 -V
before running IDLE or do I need to configure something in IDLE for it run python 3?
-dellwood
Well, not exactly. What I was showing was that if you just run something with the python command, it still defaults to the new release. Python3 must be explicitly specified with the python3 command. And, if you want to run IDLE using python3, just do
[user@computer ~]$ idle3
Hope that helps.
Offline
Thanks! That's exactly what I needed.
-dellwood
Offline
Pages: 1