Python: Difference between revisions

189 bytes added ,  20 January 2020
no edit summary
No edit summary
Line 58: Line 58:
====Array Indexing====
====Array Indexing====
[https://docs.scipy.org/doc/numpy/user/basics.indexing.html Scipy Reference]
[https://docs.scipy.org/doc/numpy/user/basics.indexing.html Scipy Reference]
===Lists===
The default data structure in Python is lists.<br>
A lot of functional programming can be done with lists<br>
<syntaxhighlight lang="python">
groceries = ["apple", "orange"]
groceries.reverse()
# ["orange", "apple"]
groceries_str = ",".join(groceries)
# "apple,orange"
groceries_str.split(",")
# ["apple", "orange"]
# Note that functions such as map, enumerate, range return enumerable items
# which you can iterate over in a for loop
# You can also convert these to lists by calling list() if necessary
enumerate(groceries)
# [(0, "apple"), (1, "orange")]
</syntaxhighlight>


===Filesystem===
===Filesystem===
Line 215: Line 189:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
if __name__ == "__main__":
if __name__ == "__main__":
</syntaxhighlight>
==Data Structures==
===Lists===
The default data structure in Python is lists.<br>
A lot of functional programming can be done with lists<br>
<syntaxhighlight lang="python">
groceries = ["apple", "orange"]
groceries.reverse()
# ["orange", "apple"]
groceries_str = ",".join(groceries)
# "apple,orange"
groceries_str.split(",")
# ["apple", "orange"]
# Note that functions such as map, enumerate, range return enumerable items
# which you can iterate over in a for loop
# You can also convert these to lists by calling list() if necessary
enumerate(groceries)
# [(0, "apple"), (1, "orange")]
</syntaxhighlight>
===Dictionaries===
Dictionaries are hashmaps in Python
<syntaxhighlight lang="python">
# Create a dictionary
my_map = {}
# Or
my_map = {1: "a", 2: "b"}
</syntaxhighlight>
</syntaxhighlight>