Python Forum
Newbie have thoughts about logic
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie have thoughts about logic
#1
I have just started learning python and being a logical person and seeing this language with fresh new eyes I get curious about the logic in some of the rules. For instance,
for x in "banana":
  print(x)
I find this not logical. Instead I would use the word loop, like
loop x in "banana": 
 print(x)
or why not even
loop letters in "banana" vertically
The word "loop" would be much easier to remember as it´s an english word that means what I want to do with x. The word "for" doesn't have that meaning in the english language forcing the newbie to memorize its meaning among so many other things to memorize.
My point is - why not make python more logical and easy to learn and perhaps also use by adapting it more to the english language, whenever that's possible?
Yoriz write Sep-01-2021, 10:24 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
SamHobbs likes this post
Reply
#2
The for loop has been around for a long time.
For loop

if you use more descriptive variable names it reads a bit more like English
for letter in "banana":
  print(letter)
Larz60+ likes this post
Reply
#3
Consider this:

- for-loop means 'for each' and 'for each letter in word' is very logical in my mind
- for-loop is not the only loop in Python. There is also while-loop. Introducing 'loop' will therefore also introduce ambiguity (Python Zen: "In the face of ambiguity, refuse the temptation to guess")

Programming languages are artificial languages ("a language whose rules are explicitly established prior to its use") contrary to natural language ("a language whose rules are based on current usage without being specifically prescribed"). Do you see problem here? 'explicit rules' vs 'current usage'?

Also, if you learn any language you can't 'demand' or 'request' changes what are logical to you. My native language doesn't have gender-specific pronouns ('he', 'she') and it would be very logical for me to get rid of those words in english. We also don't have articles of noun ('a', 'the') and I still don't get why on earth these are necessary. However, english-native people don't understand nor recognize me and my problem(s) Smile.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
It could also be argued that you are not looping through the letters in banana. The intent is to "step" through the letters only once, Not loop through them several times.
Reply
#5
I'd bet for loops are older than you are. Dartmouth Basic that I programmed in with punch cards, originally released 1964. Fortran 1957.

The point being that the for loop is an established language construct. Using another word would confuse far more people than it would help.
Pedroski55 and Lou like this post
Reply
#6
Python is dynamically typed language. It has basically two types of loop
  • For Loop
  • While loop

students = ['Peter', 'Camren', 'Andres', 'Jo', 'Geoff']
for name in students:
print(name, len(name))

For loop can be used with list.,strings & tuples etc

Can refer this article on QuizCure Different types of loops in python
[/url] for more examples for loops in python.
Reply
#7
Thanks for all replies!
I question python because I believe it can be improved in terms of ease of learning and use, but being a newbie I'm probably wrong most of the time.

Here's another one:
txt = "The best things in life are free!"
print("expensive" not in txt)
Could it not be written like

txt = "The best things in life are free!"
check if("expensive" is not in txt)
Why use the word "print" when you mean "Check if"? Print means something else in English.
Yoriz write Sep-12-2021, 06:05 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#8
(Sep-02-2021, 04:15 AM)perfringo Wrote: Consider this:

- for-loop means 'for each' and 'for each letter in word' is very logical in my mind
- for-loop is not the only loop in Python. There is also while-loop. Introducing 'loop' will therefore also introduce ambiguity (Python Zen: "In the face of ambiguity, refuse the temptation to guess")

Programming languages are artificial languages ("a language whose rules are explicitly established prior to its use") contrary to natural language ("a language whose rules are based on current usage without being specifically prescribed"). Do you see problem here? 'explicit rules' vs 'current usage'?

Also, if you learn any language you can't 'demand' or 'request' changes what are logical to you. My native language doesn't have gender-specific pronouns ('he', 'she') and it would be very logical for me to get rid of those words in english. We also don't have articles of noun ('a', 'the') and I still don't get why on earth these are necessary. However, english-native people don't understand nor recognize me and my problem(s) Smile.
Thank you for taking the time to answer.
You wrote: "- for-loop means 'for each' and 'for each letter in word' is very logical in my mind"
if for-loop means for each - why not simply use the for each expression in python instead of the for-loop expression? Then people didn't have to learn that for-loop really means for each. Maybe what I want is a computer that has learned my English instead of me having to learn python :)
Reply
#9
https://docs.python.org/3/library/functions.html#print Wrote:print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Print objects to the text stream file ...
Just because you are using print to "check if" doesn't change what print is doing.

If you really feel the need to make a new variable point to print you can.
txt = "The best things in life are free!"
check_if = print
check_if("expensive" not in txt)
Output:
True
Reply
#10
(Sep-01-2021, 10:33 PM)Yoriz Wrote: if you use more descriptive variable names it reads a bit more like English
for letter in "banana":  print(letter)
Yes the sentence for each letter in "banana" print the letter sounds valid to me.

(Sep-12-2021, 06:01 PM)sdd Wrote: Why use the word "print" when you mean "Check if"? Print means something else in English.
(This is a variation of what Yoriz said.) In the sample you provided, it is printing an expression and the expression is doing a check. The word print is misleading but I definitely do not considering it to be doing Check if; the checking is done by the expression, not the print function.

(Sep-12-2021, 06:11 PM)sdd Wrote: why not simply use the for each expression in python instead of the for-loop expression?
Many other languages often use inappropriate terms. Python is improved over the C language that would say:

char banana[] = "banana";
int x;
for (x = 0; x < strlen(banana); ++x)
printf("%c\n", banana[x]);

If you want a language that is defined to be understandable to non-developers then look at COBOL; COBOL loops are described in COBOL - Loop Statements.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,467 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  How to timestamp a Python program, your thoughts? Orthoducks 2 4,662 Dec-02-2016, 12:06 AM
Last Post: Orthoducks

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020