Re: [Tutor] floats

2011-06-07 Thread Alan Gauld


"Michael bridges"  wrote


i want to 10 / 1000 and get 0.01 not 0
if 1000 is made 1000.00 then 0.01 is printed
but that gives 500 / 1000.00 is 0.5 not 0.50

can someone till me how to get a two decimal precision every time?


You are confusing two different things..
The first case is that of integer versus float division.
You solve that by either explicitly making one of the
numbers a float or by converting to float using the foloat() 
operation.


The second issue is the *representation* of the result.
The number of decimal places displayed is a matter
of representation only, the actual value stored will not
change. Thus 0.5 and 0.50 and 0.5000 are all
the same value in memory, it is only how they are
printed that changes and that is controlled by how
you choose to format the display.

Typically you use a format string:

res = 10/float(20)
"%f" % res
"%7.3f" % res
"%5.1f" % res
"%6e" % res
"%6.4g" % res


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2011-06-07 Thread Michael bridges
ok, will attempt to clarify.
i want to out put of two numbers [int or float or anything] to be x.xx not x.x.
i want two numbers after the decimal not one.


--- On Tue, 6/7/11, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: Re: [Tutor] floats
> To: tutor@python.org
> Date: Tuesday, June 7, 2011, 1:16 AM
> 
> "Michael bridges" 
> wrote
> 
> > i want to 10 / 1000 and get 0.01 not 0
> > if 1000 is made 1000.00 then 0.01 is printed
> > but that gives 500 / 1000.00 is 0.5 not 0.50
> > 
> > can someone till me how to get a two decimal precision
> every time?
> 
> You are confusing two different things..
> The first case is that of integer versus float division.
> You solve that by either explicitly making one of the
> numbers a float or by converting to float using the
> foloat() operation.
> 
> The second issue is the *representation* of the result.
> The number of decimal places displayed is a matter
> of representation only, the actual value stored will not
> change. Thus 0.5 and 0.50 and 0.5000 are all
> the same value in memory, it is only how they are
> printed that changes and that is controlled by how
> you choose to format the display.
> 
> Typically you use a format string:
> 
> res = 10/float(20)
> "%f" % res
> "%7.3f" % res
> "%5.1f" % res
> "%6e" % res
> "%6.4g" % res
> 
> 
> HTH,
> 
> -- Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2011-06-07 Thread Walter Prins
Hello Michael

On 7 June 2011 21:10, Michael bridges  wrote:

> ok, will attempt to clarify.
> i want to out put of two numbers [int or float or anything] to be x.xx not
> x.x.
> i want two numbers after the decimal not one.
>
> Alan's already given you exactly the correct answer.  Have you tried his
suggestions?  What did you not understand or what problems did you run into?

Here's a little example from an interactive Python interpreter session which
shows you how you can play with these concepts to help clarify your
understanding:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on
win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> res = 10/float(20)
>>> res
0.5
>>> "%f" % res
'0.50'
>>> "%7.3f" % res
'  0.500'
>>> str = "%6e" % res
>>> str
'5.00e-01'
>>> print str
5.00e-01
>>>

Regards,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] if statement

2011-06-07 Thread Matthew Brunt
i'm very new to python (currently going through a python for beginners
book at work to pass the time), and i'm having trouble with an if
statement exercise.  basically, i'm creating a very simple password
program that displays "Access Granted" if the if statement is true.
the problem i'm having is that no matter what i set the password to,
it seems like it's ignoring the if statement (and failing to print
"Access Granted").  the code is copied below.  i'm pretty sure it's my
own programming ignorance, but i would greatly appreciate any
feedback.

# Granted or Denied
# Demonstrates an else clause

print("Welcome to System Security Inc.")
print("-- where security is our middle name\n")

password = input("Enter your password: ")

if password == "a":
   print("Access Granted")

input("\n\nPress the enter key to exit.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if statement

2011-06-07 Thread Andre Engels
On Tue, Jun 7, 2011 at 11:26 PM, Matthew Brunt  wrote:
> i'm very new to python (currently going through a python for beginners
> book at work to pass the time), and i'm having trouble with an if
> statement exercise.  basically, i'm creating a very simple password
> program that displays "Access Granted" if the if statement is true.
> the problem i'm having is that no matter what i set the password to,
> it seems like it's ignoring the if statement (and failing to print
> "Access Granted").  the code is copied below.  i'm pretty sure it's my
> own programming ignorance, but i would greatly appreciate any
> feedback.

Actually, no, this case it's not your ignorance. What is probably
going on is that you are using Python 2, but the book is going with
Python 3. I won't get into the details, but the function that does
what input() does in Python 3, is called raw_input() in Python 2 (and
the Python 2 input() function is advised against using for security
reasons). Probably if you change input to raw_input in your program,
it does do what you want it to do.

> # Granted or Denied
> # Demonstrates an else clause
>
> print("Welcome to System Security Inc.")
> print("-- where security is our middle name\n")
>
> password = input("Enter your password: ")
>
> if password == "a":
>   print("Access Granted")
>
> input("\n\nPress the enter key to exit.")
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Copying a mutable

2011-06-07 Thread Válas Péter
Hi,

let X be a mutable container, such as dict/set/list=bytearray, and Y=X,
When I change X, Y will follow it, having always the same value, although
id(X)!=id(Y). How is that, what is the explanation? Meanwhile the same for
immutable types results a real copy, and so does for simple mutables such as
int.

I suspect it has something to do with pointers, but what is the difference
between mutables and immutables, and why have they different id's if they
are the same?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if statement

2011-06-07 Thread Alan Gauld


"Andre Engels"  wrote 


Actually, no, this case it's not your ignorance. What is probably
going on is that you are using Python 2, but the book is going with
Python 3. 


You can quickly check the version by just typing python at 
an OS command prompt to get into the interactive interpreter(>>>).


The welcome message should tell you the version you 
are running.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying a mutable

2011-06-07 Thread Wayne Werner
Apologies for the top post, my phone doesn't allow editing the message body.

You are slightly confused - ints are not mutable! You can combine or
multiply them, along with several other operations, but they are certainly
not mutable. The easiest way to check is use them as keys in a dict. You
can't do that with lists or other mutable types. Since I don't have computer
access right now I can't look up the docs on id, but hopefully someone else
can enlighten you. But ints are definitely immutable. I suspect you're
confusing the fact that you can easily reassign ints with mutability.

HTH,
Wayne

On Jun 7, 2011 5:50 PM, "Válas Péter"  wrote:

Hi,

let X be a mutable container, such as dict/set/list=bytearray, and Y=X,
When I change X, Y will follow it, having always the same value, although
id(X)!=id(Y). How is that, what is the explanation? Meanwhile the same for
immutable types results a real copy, and so does for simple mutables such as
int.

I suspect it has something to do with pointers, but what is the difference
between mutables and immutables, and why have they different id's if they
are the same?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying a mutable

2011-06-07 Thread Válas Péter
2011. június 8. 1:02 Wayne Werner írta, :

> You are slightly confused - ints are not mutable!
>

All right, I was really wrong in this question (here it is after midnight
:-), but this doesn't make an influence on the main question.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying a mutable

2011-06-07 Thread Walter Prins
Hi,

2011/6/7 Válas Péter 

> Hi,
>
> let X be a mutable container, such as dict/set/list=bytearray, and Y=X,
> When I change X, Y will follow it, having always the same value, although
> id(X)!=id(Y).


That's not correct:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on
win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=[1,2,3]
>>> y=x
>>> id(x)
36115464L
>>> id(y)
36115464L
>>> x.append(4)
>>> id(x)
36115464L
>>> id(y)
36115464L
>>> x
[1, 2, 3, 4]
>>>

As you can see, x and y are references to the same object (e.g. with the
same id.)

Regards

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying a mutable

2011-06-07 Thread Nathan
unsubscribe


在 2011-06-08 07:11:33,"Walter Prins"  写道:
Hi,


2011/6/7 Válas Péter
Hi,

let X be a mutable container, such as dict/set/list=bytearray, and Y=X,
When I change X, Y will follow it, having always the same value, although 
id(X)!=id(Y).

That's not correct:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=[1,2,3]
>>> y=x
>>> id(x)
36115464L
>>> id(y)
36115464L
>>> x.append(4)
>>> id(x)
36115464L
>>> id(y)
36115464L
>>> x
[1, 2, 3, 4]
>>>

As you can see, x and y are references to the same object (e.g. with the same 
id.)

Regards

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying a mutable

2011-06-07 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Válas Péter wrote:

Hi,

let X be a mutable container, such as dict/set/list=bytearray, and Y=X,
When I change X, Y will follow it, having always the same value, although
id(X)!=id(Y). How is that, what is the explanation? Meanwhile the same for
immutable types results a real copy, and so does for simple mutables such as
int.

I suspect it has something to do with pointers, but what is the difference
between mutables and immutables, and why have they different id's if they
are the same?



It would help greatly if you actually posted some code that showed your 
confusion, since there are several errors in your message.  As Wayne 
pointed out, integers are immutable.  There are no methods on them that 
modify them in place.  All you can do is bind a variable to a different 
integer object.


Further, if you set Y=X, the id's will be the same.  Try it, and paste 
the actual test into your message.  Don't just paraphrase.


Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [4, 5,6]
>>> y = x
>>> id(y)
24789144
>>> id(x)
24789144


Now, if you change x by doing an append, for example, they both change: 
 You mutated the object to which they both refer.


>>> x.append(42)
>>> x
[4, 5, 6, 42]
>>> y
[4, 5, 6, 42]
>>>

But if you rebind one of them, then they can diverge:

>>> x = [3]
>>> x
[3]
>>> y
[4, 5, 6, 42]
>>> id(x)
24978568
>>> id(y)
24789144

Now you can notice that x has a different id. It's bound to a different 
object.


Now, if an object is immutable, then the question of what happens when 
you change the object is nonsensical.  Since you can't change it, you 
don't really care if the two names are bound to same object, or just to 
two objects that happen to have the same value.


One more thing.  Binding happens in an assignment, but it also happens 
in a function call, and in some import syntaxes and in for loops, 
generator expressions, and list comprehensions.  Some of that behavior 
can change between different versions of Python, so if you start getting 
into those corners, you'll have to be more specific.


HTH
DaveA



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copying a mutable

2011-06-07 Thread Válas Péter
Walter and Dave, thank you for the useful and detailed answer, now I see it
better. I didn't write code, because once I realized I had spoiled
something, the mistake has no more importance except historical, the correct
solutions have importance.

2011. június 8. 4:19 Dave Angel írta, :

> Now, if an object is immutable, then the question of what happens when you
> change the object is nonsensical.  Since you can't change it, you don't
> really care if the two names are bound to same object, or just to two
> objects that happen to have the same value.
>

Being one of the purposes of Python to be a simple educational language, I
want to make this simple to a beginner who does care. :-)
Here is a piece of code, Python 3.1.2, a small game with a list and a tuple:
>>> li=[3,4]
>>> id(li)
13711200
>>> la=li
>>> id(la)
13711200
>>> li*=4
>>> li
[3, 4, 3, 4, 3, 4, 3, 4]
>>> la
[3, 4, 3, 4, 3, 4, 3, 4]
>>> id(li)
13711200
>>> tup=(3,4)
>>> id(tup)
13697392
>>> top=tup
>>> id(top)
13697392
>>> tup*=4
>>> tup
(3, 4, 3, 4, 3, 4, 3, 4)
>>> top
(3, 4)
>>> id(tup)
12956016
>>> id(top)
13697392
My question is: how would you explain the different behaviour of a list and
a tuple for a beginner? Formally we made the same intervention to these poor
guys, the list and the tuple, they look the same except parenthesis and
square bracket, but they reacted differently. Although tuples are immutable,
the beginner only sees that they have changed in the same way.

This is another point the beginner may be confused: multiple assignments.
Formally we play the same game with an int and a list, and they will again
react in a different way.
>>> n=m=8
>>> m*=8
>>> m
64
>>> n
8
>>> n=m=[2,3]
>>> m*=8
>>> m
[2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3]
>>> n
[2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3]


>
> One more thing.  Binding happens in an assignment, but it also happens in a
> function call, and in some import syntaxes and in for loops, generator
> expressions, and list comprehensions.

Just to precisely understand English words, because this is a foreign
language for me. As far as I understand, assignment means giving a value to
a variable which is the expression used by classical languages that have
variables (such as Pascal or Basic). Python has no variables, since even the
simpliest data is an object, but we still say assignment, because it is
comfortable.
In this sense, if I say, "assignment" is a subset of "binding", is it
correct?

Péter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor