[Tutor] online interactive Py resource?

2018-11-28 Thread Mats Wichmann


I'm going to end up tutoring someone (family member) learning Python in
the new year.  Look forward to it. It will be a remote arrangement.
Haven't trained anyone in the whole picture (as opposed to bits and bobs
of possibly helpful advice on demand, like here) since I wrote and
delivered a training course for a commercial training company, the last
instance of which was pre-historic in tech terms (2002 in calendar
terms). That those were very much hands-on, in-person.

It occurs to me it would be cool to work interactively in a distributed
Internet editing environment that could run Python code.  Are there
such?  There are distributed editing environments, like Etherpad, Gobby,
(cough) Google Docs.  There are online interactive Python environments,
such as the ones from the nice folks at PythonAnywhere.

is there something that combines the two?  I'm not completely sure
PythonAnywhere doesn't have that capability... but I'm looking for some
wisdom and experiences (and maybe best practices) from the other tutors
around here.

cheers,

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


Re: [Tutor] user overloaded

2018-11-28 Thread Steven D'Aprano
On Wed, Nov 28, 2018 at 12:56:32PM -0500, Avi Gross wrote:

> try ...
> except ValueError, e:
> ...
> 
> You now need to do this format:
> 
> try ...
> except ValueError as e:
> ...
> 
> Why the change?

Because the first form was a trap and a bug magnet:

try:
block
except TypeError, ValueError, KeyError:
print "surprise! this will not catch key errors!"



[...]
> Back to your topic. "=" as a symbol has been used over the years in many
> ways.  In normal mathematics, it is mostly seen as a comparison operator as
> in tan(x) = sin(x)/cos(x)

That's not a comparison, that's an identity. The tangent function is (or 
at least can be) defined as sine divided by cosine.


> Many programming languages used it instead to sort of mean MAKE EQUALS.

That's usually called "assignment" or "binding".


> x = x + 5
> 
> is an example where it makes no mathematical sense. How can a number be
> equal to itself when 5 is added? 

There is no solution there in the normal real number system, but there 
are forms of arithmetic where it does, e.g. "clock arithmetic". If your 
clock only has values 0 through 4, then adding 5 to any x will give you 
x back again.

Although normally we write "congruent" rather than "equals" for clock 
arithmetic: 

x ≡ x + 5 (mod 5)


In any case, just because there is no solution doesn't mean that the 
question makes no sense:

x = x*5

makes perfect sense and has solution 0. Likewise:

x = x**2 + k

makes perfect sense too, and can have zero, one or two solutions 
depending on the value of k.



> The meaning clearly is something like x
> gets assigned a value by evaluating the current value of x and adding 5 to
> that. When done, the old value of x is gone and it has a new value.
> 
> For this reason, some languages like PASCAL used := so the above became 
> 
> x := x + 5
> 
> Yes, same symbol we are discussing and NOT what Python intends.

Actually, it is precisely what Python intends, except for two minor 
differences:

1. in Pascal, := assignment is a statement, not an expression, 
   while in Python it will be an expresion;

2. Python will discourage people from using the := expression
   form as a de facto statement, where = will do.


[...]
> And there are forms of equality that some languages need to distinguish.
> What does it mean if two (unordered) sets are equal? Must they be two
> reference to the same set or is it shorthand for them being strict subsets
> of each other in both directions? You can come up with additional
> abstractions like whether a list is a shallow or deep copy of another and
> how it effects your ideas about them being equal. You may want a way to say
> things are approximately equal. In math, something like ~= is used. Python
> has an operator called "is" that can be a variant on equality.

The "is" operator is in no way, shape or form an equality test. It is an 
*identity* test, which returns True if and only if the two operands are 
the same object.

Equality does not imply identity:

py> x = []
py> (x == []) and (x is not [])
True


nor does identity imply equality:

py> NAN = float(NAN)
py> (NAN is NAN) and (NAN != NAN)
True

Anyone using "is" when then test for equality is programming in a state 
of sin.


> Consider the concept of OR. Some languages spell it out when used in a
> Boolean context. 
> 
> a or b
> 
> But python does something new with this. In some contexts the above
> effectively becomes an IF.

This is called a "short-circuiting boolean operator", and it was old, 
old old when Python started using it 25+ years ago. I believe Lisp was 
the first to introduce them, in the 1950s.

https://en.wikipedia.org/wiki/Short-circuit_evaluation



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


[Tutor] user overloaded

2018-11-28 Thread Avi Gross
OOPS,

Sorry Steve. Yes, I am on multiple mailing lists and the tutor list by
default replies to the sender and you need to edit it to reply to the group.
I will be careful.

Your other point is broader. There are lots of operations (not just ion
python) that are easy to misuse. Sometimes the same symbol is used in
multiple ways, such as how parentheses can mean a tuple even when you wanted
to use them for ordering. There was a recent discussion about how the try
statement changed after 2.x so instead of the part that used to say:

try ...
except ValueError, e:
...

You now need to do this format:

try ...
except ValueError as e:
...

Why the change?

I have not looked to see, but note the earlier version is sort of a tuple
without parentheses which Python allows in many contexts. As the keyword
"as" is now used in other places like import statements so why not make
things more consistent. But to others who just see a CHANGE and wonder why,
many things seem mysterious. Yes, read the manual but that is not something
new users seem to want to do, let alone know how to find what to read and
slog through lots of pages.

Back to your topic. "=" as a symbol has been used over the years in many
ways.  In normal mathematics, it is mostly seen as a comparison operator as
in tan(x) = sin(x)/cos(x)

Many programming languages used it instead to sort of mean MAKE EQUALS.

x = x + 5

is an example where it makes no mathematical sense. How can a number be
equal to itself when 5 is added? The meaning clearly is something like x
gets assigned a value by evaluating the current value of x and adding 5 to
that. When done, the old value of x is gone and it has a new value.

For this reason, some languages like PASCAL used := so the above became 

x := x + 5

Yes, same symbol we are discussing and NOT what Python intends.

Languages like R also decided to not use a naked "=" sign and encouraged
arrow notation like:

x <- x + 5
and even
x + x -> x

And yes, for global variables there are --> and <-- and worse.

But they still accept a naked "=" as well and use that alone in function
definitions and calls for setting values by name. Yes, it can confuse people
when an assignment is "<-" and a comparison is "<=" .

And there are forms of equality that some languages need to distinguish.
What does it mean if two (unordered) sets are equal? Must they be two
reference to the same set or is it shorthand for them being strict subsets
of each other in both directions? You can come up with additional
abstractions like whether a list is a shallow or deep copy of another and
how it effects your ideas about them being equal. You may want a way to say
things are approximately equal. In math, something like ~= is used. Python
has an operator called "is" that can be a variant on equality. But it can be
subtle when python reuses an immutable object and they seem to be the same
even when you try to make them different:

>>> a = "supercalifragilisticexpialidocious" * 20
>>> b  = "supercalifragilisticexpialidocious" * 10 +
"supercalifragilisticexpialidocious" * 10
>>> a == b
True
>>> a is b
True

Weird. Try that with mutables:

>>> a = [ 'hello' ]
 
>>> b = [ 'hello' ]
 
>>> a == b
 
True
>>> a is b
 
False

For people new to computer science or a particular programming language,
there can be too much and users get overloaded by all the overloading.

Adding yet another variant to have a side-effect within an expression using
":=" will extend some of the power of python but at a cost for some, and
especially new users.

Consider the concept of OR. Some languages spell it out when used in a
Boolean context. 

a or b

But python does something new with this. In some contexts the above
effectively becomes an IF.

>>> a = 5
 
>>> b = 10
 
>>> x = a or b
 
>>> x
 
5
>>> a = 0
 
>>> x = a or b
 
>>> x
 
10


So because python has  a creative definition of what is True and What is
False, the above is sort of this code:

if ( a EVALUATES TO TRUE)
  x = a
else 
  x = WHATEVER b EVALUATES to

Since I chose integers, a is true whenever it is not precisely zero. For
lists and many other objects, they are True if not empty.

That is not the same meaning of "or" that many languages use. Add other
meanings like a "bitwise or", "exclusive or" or the union of two sets and
you can see people get confused especially when the same or similar symbols
are used. Many languages have a symbol like "|" and another like "||" for
example. 

Arguably python allows users to invent their own overloading in classes so
that using some version of "or" between two objects means whatever you want
it to mean. I recently (for fun) created a sort of auto-increment (as in the
C/C++ symbol ++) so that if you had a counter called x, then adding ANYTHING
to x using the += notation just incremented it by 1.

X = odd_object(6) # X initialized to 6
X += 1  # X is now 7
X += 12 # 

Re: [Tutor] I need help with my project

2018-11-28 Thread Avi Gross
I suggest starting at the beginning when asking a question to people who
have no way of knowing what you have not told them.
Your sentence is completely in middle or even near the end of something that
has to be larger:

" If the user selected a sandwich, french fries, and a beverage, reduce the
total cost of the order by $1.00."

It sounds like you are being asked to emulate a place you can buy food. So I
suspect you need to display a menu, ask the user to enter info, keep a
running total and so on.

So how do you think you would store the order as well as whatever you need
to know about past sales or the non-discounted cost of the current order?
How do you see if the current order has all the parts needed for the
discount. Do you need to apply the discount just once or multiple times if
they ordered food for lots of people? 

Sure, we could write some code for you, but perhaps you need to think it
through first and maybe if you get stuck, ask if someone can tell you what
is wrong with the code.

Given what you sent, the answer is easy.

RECOGNIZE they placed an order that meets the criterion:
SUBTRACT 1.00 from their order.

But that is not written in Python for obvious reasons.


-Original Message-
From: Tutor  On Behalf Of
Treyton Hendrix
Sent: Tuesday, November 27, 2018 7:30 PM
To: tutor@python.org
Subject: [Tutor] I need help with my project

If the user selected a sandwich, french fries, and a beverage, reduce the
total cost of the order by $1.00.

This is what I have to do and I don't know where to start.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] I need help with my project

2018-11-28 Thread Alan Gauld via Tutor
On 28/11/2018 00:30, Treyton Hendrix wrote:
> If the user selected a sandwich, french fries, and a beverage, reduce the
> total cost of the order by $1.00.
> 
> This is what I have to do and I don't know where to start.

Neither do we because we don't know what you are talking about.
There is no context.

How does a user select these items? Is it a web page? a GUI? A CLI?
An in-store sensor that detects items being removed from the fridge?
(Maybe under the control of a Raspbery Pi or Arduino?)

And how do you know the original prices? Are they hard coded?
read from a data file or database? Input by the user? Or maybe the
fridge sensor reads bar codes attached to the actual items?

How do you calculate the total? Is it stored in a variable?
Is it stored as a floating point value (you should never do
that for money!) or as an integer number of pennies/cents?

If the latter

total -= 100

Is the obvious answer to your question.

Is it even in code or do you just write it down on paper?
Or maybe you are using an electronic calculator?

I could go on but hopefully you see how little information
you have given us to work from.?

If it is in code show us what you hae so far and we can
help you add this feature. Without any clue what you are
doing we can't really help very much.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] I need help with my project

2018-11-28 Thread Bob Gailer
On Nov 28, 2018 3:43 AM, "Treyton Hendrix" <2hendri...@stu.bps-ok.org>
wrote:
>
> If the user selected a sandwich, french fries, and a beverage, reduce the
> total cost of the order by $1.00.
>
> This is what I have to do and I don't know where to start.

You start by learning how to ask effective questions. Example:

I am taking python 101 at Fubar University. I have been given the following
assignment:

Here you tell us the entire assignment not just one sentence from it.

Then you show us whatever attempt you have made to solve the problem. Have
you written any Python program? Show us the program. Tell us where you are
stuck.

We really like to help but we do not have any crystal balls to look into.
Help us understand your situation fully.

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


Re: [Tutor] I need help with my project

2018-11-28 Thread Sibylle Koczian

Am 28.11.2018 um 01:30 schrieb Treyton Hendrix:

If the user selected a sandwich, french fries, and a beverage, reduce the
total cost of the order by $1.00.

This is what I have to do and I don't know where to start.


Well, you are lucky. I just had my first mind-reading lesson today, you 
are my first victim and I'll tell you what you didn't say about your 
problem:


You know prices for sandwiches (one sort or more, with different 
prices?), french fries and beverages. If the user selects one or two of 
them, he pays the sum of the prices. If he selects one of each, he pays 
the sum minus $1.00.


How would you do this by hand?

If my mind-reading isn't yet perfect, please fill the gaps.


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


Re: [Tutor] PEP 572 -- Assignment Expressions

2018-11-28 Thread Steven D'Aprano
On Tue, Nov 27, 2018 at 08:26:04PM -0500, Avi Gross wrote:
> Ivo,

You've replied to the wrong mailing list. Ivo wrote to Python-List, not 
Tutor.

But now that you've raised the issue...


[Ivo asked]
>Why:
>  if (match := pattern.search(data)) is not None:
># Do something with match
> 
>What is wrong with:
>  if match = pattern.search(data) is not None:
># Do something with match
> 
>Assignment expression or assignment statement, it's an assignment, right?
> It is very clear to everyone that it's an assignment! Can't it all just be a
> "="?

It seems that Ivo didn't read the PEP because that is discussed:

https://www.python.org/dev/peps/pep-0572/#id32

Despite Ivo's statement that it is "clear to everyone", the problem is 
that whenever you see an assignment x=y in an assignment, there's no 
obvious way of telling whether they meant assignment or it was a 
mistyped equality test x==y. So it is not clear at all.

In C, this confusion between = and == is a frequent source of serious 
bugs, including one attempt at inserting a back-door into the Linux 
kernel:

https://freedom-to-tinker.com/2013/10/09/the-linux-backdoor-attempt-of-2003/

https://lwn.net/Articles/57135/


Requiring the colon x:=y makes it less likely to mess up the assignment 
or comparison.



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


Re: [Tutor] PEP 572 -- Assignment Expressions

2018-11-28 Thread Avi Gross
Ivo,

One thing you could have done is explore with simpler code to see if you can
deduce what is happening.

 If you opened your Python interpreter and tried to see what happens with a
simplified variant like this, what do you get?

if (match = 5) is not None: pass

That might answer your question on why a := might introduce new
functionality.

Something similar would work fine in a language like C/C++. Heck, it is used
often as a concise side effect.

Both languages have an == to be used in expressions but with a meaning of
COMPARE, not set equal to.

I guess someone thought that situations like matching a regular expression
would benefit for the side effect. There are workarounds, of course. As an
example, in SOME cases, the value of the last expression can be found
momentarily in the special variable consisting of a single underscore. But
the expression above probably would evaluate to True, ...

Ovi


-Original Message-
From: Python-list  On
Behalf Of Ivo Shipkaliev
Sent: Tuesday, November 27, 2018 5:48 AM
To: python-l...@python.org
Subject: PEP 572 -- Assignment Expressions

   Hello.
   Maybe it's too late for a discussion, but I just couldn't resist.
   I just found out about this new ":=" operator. I need to ask:

   What is the need for this additional ":" to the "="?
   Why:
 if (match := pattern.search(data)) is not None:
   # Do something with match

   What is wrong with:
 if match = pattern.search(data) is not None:
   # Do something with match

   Assignment expression or assignment statement, it's an assignment, right?
It is very clear to everyone that it's an assignment! Can't it all just be a
"="?
   Thank you very much!


   Kind Regards
   Ivo Shipkaliev
--
https://mail.python.org/mailman/listinfo/python-list

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


[Tutor] I need help with my project

2018-11-28 Thread Treyton Hendrix
If the user selected a sandwich, french fries, and a beverage, reduce the
total cost of the order by $1.00.

This is what I have to do and I don't know where to start.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor