You are not logged in.

#1 2009-03-08 03:45:49

dellwood
Member
Registered: 2009-03-02
Posts: 7

[SOLVED] Python Problem

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 smile

EDIT: I'm using python 2.6 and IDLE, if that helps

Last edited by dellwood (2009-03-11 23:20:37)

Offline

#2 2009-03-08 03:57:31

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [SOLVED] Python Problem

*edit*
nevermind... see Dusty's reply below

Last edited by Xyne (2009-03-08 17:34:22)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2009-03-08 04:05:52

countercraft
Member
From: Montréal, CA
Registered: 2008-03-30
Posts: 31

Re: [SOLVED] Python Problem

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

#4 2009-03-08 06:34:18

dellwood
Member
Registered: 2009-03-02
Posts: 7

Re: [SOLVED] Python Problem

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 smile

Offline

#5 2009-03-08 07:48:54

drewbug01
Member
From: chicago
Registered: 2007-04-06
Posts: 85
Website

Re: [SOLVED] Python Problem

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

#6 2009-03-08 15:51:12

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [SOLVED] Python Problem

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

#7 2009-03-08 15:54:13

dellwood
Member
Registered: 2009-03-02
Posts: 7

Re: [SOLVED] Python Problem

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 smile

Offline

#8 2009-03-08 15:57:39

buttons
Member
From: NJ, USA
Registered: 2007-08-04
Posts: 620

Re: [SOLVED] Python Problem

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

#9 2009-03-09 02:31:23

drewbug01
Member
From: chicago
Registered: 2007-04-06
Posts: 85
Website

Re: [SOLVED] Python Problem

dellwood wrote:

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 smile

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

#10 2009-03-11 23:20:15

dellwood
Member
Registered: 2009-03-02
Posts: 7

Re: [SOLVED] Python Problem

Thanks! That's exactly what I needed.

-dellwood smile

Offline

Board footer

Powered by FluxBB