Python Forum
Learning python specific syntax after using other scripting languages for years
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning python specific syntax after using other scripting languages for years
#1
Hello, I'm learning AI and if you're doing AI you need python it seems. I've long time used PHP, Javascript and occasionally bash for my job as a fullstack web developer. I'll first admit, I'm not liking Python, but perhaps part of that is because I don't really get the "why" of some of what it does. So maybe you friendly people would be willing to help me out. I'll be making comparisons to PHP a lot because that is what I am most familiar with and would like to achieve in Python.

First datastructures. In PHP you have an array. That array can hold anything and is manipulated by the same utility methods (which are functions not methods of some object). The closest I can find to PHP arrays in Python is a dictionary, but PHP arrays also maintain order and I guess dictionaries do not.

My first "Why" question is why is a scripting language, which are not strongly typed by design and one of the biggest selling points of a scripting language over a compiled language, having so many data structures as objects? You have tuples, lists, dictionaries, sets, counters, and probably more. What is the difference between a list or a set? Why do I have to memorize and understand this to stuff things into a loopable container and do stuff with it? It seems to overcomplicate things and make it hard to use.

My second "Why" question is why is there no standard looping? For any given of those data structures there seems to be 3-4 ways to loop over them, and people pick up different preferences on looping and it makes both reading and writing code harder because there isn't just one easy way to do it, there's 3-4 more difficult ways to do it. Example in PHP there is the for loop and the foreach loop. Most the time the foreach loop does anything you'd need with an array. It gives you the key, the value, and it can be told to do the value by reference instead of using a copy. Literally everything you'd need or want. With Python however you have to use enumerable, or type the name of the thing you're looping over and provide the index to access the value, or use range, or use some weird thing in brackets to do shortcuts on math that is hard to both understand and read. Why have 4 ways to do something, quality is better than quantity here you shouldn't have to wrap something in a function just to loop over it like this, so why?

My third "Why" question is why does it not convert things to be printable without converting types? If you try to print a string and an integer it fails with an error. This is making typing again, an issue, and now you need to convert the number to a string to print the two together or separate them with a , in the print statement rather than concatenating like you do in pretty much every other language I know of.


Here are some examples to go with my text to make it easier to understand what I'm getting at:
PHP example:
$a = array('one'=>2,3=>4,5=>'six');
foreach($a as $k=>$v) echo "$k:$v\n";
I don't even know how to do that in python or even if there's such a data construct that would handle it. I know the looping is going to be a pain and so is the printing :(

I mean which data structure would I use, sets, lists, dictionaries, counters, tuples?

And which of the looping methods do I use after that?

a = {'one':2,3:4,5:'six'} #doesn't this fail for some reason?
for item in a: #can't get index
    print(str(item) + "\n") #have to convert item to string, if it works?
for k,v in enumerate(a): #gets index but has to call a function...
    print(str(k) + ":" + str(v) + "\n") #have to convert to string?
for k in range(len(a)): #gets index but value has to be accessed and now you're calling two functions...
    print(str(k) + ":" + str(a[k]) + "\n") #have to convert to string?
And last we have this syntax for making shorthand loops, this would multiply each value by two and reassign it to the container so doesn't achieve the same things as above but maybe there's some way to make it print from this syntax I'm not sure...
a = [val * 2 for val in a]
I don't think it works with anything but lists but I don't know for certain. All I know is it seems very unintuitive to read and looks way out of place to me.

So, hope there's some nice people here that can explain why python is this way (even if it's just admitting it's silly it would help me feel a bit better about it). To be honest, everyone talks about how great and easy python is and should be taught to beginners but I find it very much complicates simple things for no apparent reason by reintroducing typing and making you have to call functions to even iterate over things :/
Reply


Messages In This Thread
Learning python specific syntax after using other scripting languages for years - by Arcs - Dec-11-2017, 11:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting days to years in loop while computing values across grid cells Lightning1800 2 2,728 May-15-2018, 08:44 PM
Last Post: Lightning1800

Forum Jump:

User Panel Messages

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