Setting Variables in Python

I just completed the Rounding Numbers piece of homework, and tried to bug test it a little bit. I found that if you assign 'x' as a fraction (e.g. 9/5) it will incorrectly get rounded down as it assumes an integer, so the code will produce the answer 1, instead of the correct 2.

If you input the number as '9./5.' it corrects this, and I know that you can use float() AFTER you've assigned the variable to let the program know it will have decimal points, but I was wondering if there were a way to set the variable to have decimal points when you create the variable, so Python doesn't have to guess what type it is, and the user doesn't have to input the number with a '.'.

Thanks for your help.

asked 23 Feb '12, 13:33

Paul%20Wilson's gravatar image

Paul Wilson
441110
accept rate: 100%

retagged 14 Jan, 19:37

Retagbot's gravatar image

Retagbot ♦
1518171


3 Answers:

No I don't think it's possible, though you can ask this over at StackOverflow if you want.

Python doesn't bind a variable to any particular type.
You could assign an variable x an integer, and in the next statement assign a string to x.
IIRC, Python doesn't reserve a memory address for a variable. What it does is assign a memory location to the constant values. That is if you assign the value 7 to a variable x, it will assign a memory location to 7 & x will point to it. When x is changed, the value in the memory address x was point to doesn't change, but x points to another memory address.

Here, I think this example will make it more clear. # id(x) returns the memory address of the variable x

>>> a = 7
>>> print id(7)
25784840
>>> print id(a)
25784840
>>> a = a+7
>>> print id(a)
25784672
>>> print id(7)
25784840
>>> a = float(a)
>>> print id(a)
25798144

Hope this helps

link

answered 23 Feb '12, 15:30

elssar's gravatar image

elssar
19.3k2563155

Wow, that's exactly the answer I was looking for, and more. Thanks for clearing that up!

Would you see that as a flaw of Python or is it more of a non-issue?

P.S. I just noticed there are nested comment threads and the delete button doesn't seem to be working on my other reply.

(23 Feb '12, 17:53) Paul Wilson Paul%20Wilson's gravatar image

I would call it a feature.
When taking input from the user, you'll either take a string & then parse it or if you're just taking numbers, you can typecast them to float before using them in calculations, so your concerns about problems with user input are a non-issue really.
a = float(raw_input())
or a, b = raw_input()
y = float(a)/b

(24 Feb '12, 21:58) elssar elssar's gravatar image

You can set the var as x = 0.0 and then update it somewhere in the code... Would this have the effect you want? Not sure what you want to do...

link

answered 23 Feb '12, 13:40

Joel%20Montes%20de%20Oca's gravatar image

Joel Montes ...
1.6k51118

I want to be able to set x to have decimal places immediately. I don't want to have the input the number with a decimal point.

For example, if in the Rounding Numbers homework I had the user input a number, and he chose "9/5". If the code used that exact entry, it assumes the division of 9/5 is an integer, and so with the code I produced it would output 1 as the rounded number.

I have very little coding knowledge, but I believe in C# and other languages you can set variables to be types straight away, as with int, float, var, etc.. I want to be able to do that without having to set x first, then convert it using float(x).

I hope that makes more sense.

link

answered 23 Feb '12, 14:09

Paul%20Wilson's gravatar image

Paul Wilson
441110

edited 23 Feb '12, 14:09

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,348
×114

Asked: 23 Feb '12, 13:33

Seen: 515 times

Last updated: 24 Feb '12, 21:58