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
#3
Quote: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.

Yes, in other languages or in general this is called a Hashtable.
A Hashtable don't save the order of elements.
In PHP this is an implementation detail aswell in Python. Since
Python 3.6 the implementation of dicts has been changed and the order
is now preserved, but developer should not relay on this implementation detail
until it's official in the language. If you want to preserve the order, you can use
collections.OrderedDict.

Example:

import collections
items = [('First', 1), ('Second', 2), ('third', 3)]
ordered_dict = collections.OrderedDict(items)
for key, value in ordered_dict.items():
    print('Key:', key, 'Value:', value)
Don't use a dict as "constructor" for OrderedDict.

Quote:I mean which data structure would I use, sets, lists, dictionaries, counters, tuples?
  • tuple: immutable, holds a sequence of elements
  • list: mutable, holds a sequence of elements, you can add new elements to the list
  • dict: mutable, fast hashtable lookup, no order
  • set: mutable, same as dict, but without values. It's from set theory in math.

Then you've some additional mixed types, which are based on the atomic types.
For example: collections.OrderedDict is a mix of dict and list.

All data types, which are containers, can used for iteration. You can iterate over: tuple, list, dict, set, OrderedDict.
If you iterate over a dict or a set, there is no guarantee for order.

Data types, which are hash table based (dict, set), can hold only objects as keys, which are hashable. A list is not hashable, because it's mutable.


Indexing: If you want to get indexes in a loop, enumerate is the pythonic way. The additional call don't cost much. I think it's implemented in C as a generator.

Printing obejcts: You can print all objects without type conversion. If you have already a str object, just print it. If you call the built in function str on an object, which is already a str, you'll get the same object back. Behind the scenes the print function call the __str__ method of an object. If this method does not exist, it calls __repr__ which stands for representation. General you want to use string interpolation. Look here: https://pyformat.info/

Quote:
a = [val * 2 for val in a]
This gives you a list back.
If you want to print the whole object, just use print(a).
Then you'll get the representation of the list, which can be evaluated in Python.
If you want an output line by line, you can use a for loop:

for element in a:
    print(element)
or you can use the print function inside the list comprehension, but this is not used often:

a = [print(val * 2) for val in a]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Learning python specific syntax after using other scripting languages for years - by DeaD_EyE - Dec-11-2017, 11:53 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,682 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