{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This page demonstrates some basic Python concepts and essentials. See the Python Tutorial and Reference for more exhaustive resources.\n", "
This page provides a brief introduction to:
\n", "You will get a TypeError
. What is wrong?
my_name is a string and my_age is a number. Adding in this context does not make any sense.
\n", "We can determine an object’s type with the type()
function.
The function isinstance()
is typically more useful than comparing variable types as it allows handling subclasses:
Warning
\n", "In older versions of Python (prior to 3.0), the /
operator when used on integers performed integer division; i.e. 3/2
returned 1
, but 3/2.0
returned 1.5
. Beginning with Python 3.0, the /
operator returns a float if integers do not divide evenly; i.e. 3/2
returns 1.5
. Integer division is still available using the //
operator, i.e. 3 // 2
evaluates to 1.
If the second line in the previous example was replaced with list_b = list_a, then what would happen?
\n", "In that case, list_b is the same list as list_a (as opposed to a copy), so when list_b was reversed so is list_a (since list_b is list_a).
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can sort a list and get a new list using the sorted function. e.g." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(['soma', 'basal', 'apical', 'axon', 'obliques'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we have sorted by alphabetical order. If our list had only numbers, it would by default sort by numerical order:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted([6, 1, 165, 1.51, 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we wanted to sort by another attribute, we can specify a function that returns that attribute value as the optional key keyword argument. For example, to sort our list of neuron parts by the length of the name (returned by the function len):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(['soma', 'basal', 'apical', 'axon', 'obliques'], key=len)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists can contain arbitrary data types, but if you find yourself doing this, you should probably consider making classes or dictionaries (described below).
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "confusing_list = ['abc', 1.0, 2, \"another string\"]\n", "print(confusing_list)\n", "print(confusing_list[3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is sometimes convenient to assign the elements of a list with a known length to an equivalent number of variables:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "first, second, third = [42, 35, 25]\n", "print(second)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The range function can be used to create a list-like object (prior to Python 3.0, it generated lists; beginning with 3.0, it generates a more memory efficient structure) of evenly spaced integers; a list can be produced from this using the list function. With one argument, range produces integers from 0 to the argument, with two integer arguments, it produces integers between the two values, and with three arguments, the third specifies the interval between the first two. The ending value is not included." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(10))) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(0, 10))) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(3, 10))) # [3, 4, 5, 6, 7, 8, 9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(0, 10, 2))) # [0, 2, 4, 6, 8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(0, -10))) # []" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(0, -10, -1))) # [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(0, -10, -2))) # [0, -2, -4, -6, -8]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For non-integer ranges, use numpy.arange from the numpy module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TypeError
."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because you cannot modify an element in a tuple, or add or remove individual elements of it, it can operate in Python more efficiently than a list. A tuple can even serve as a key to a dictionary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pickling
”, which attempts to save a Python object to a file for later restoration with the load command."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"contacts = [joe, bob] # Make a list of contacts\n",
"\n",
"with open('contacts.p', 'wb') as pickle_file: # Make a new file\n",
" pickle.dump(contacts, pickle_file) # Write contact list\n",
"\n",
"with open('contacts.p', 'rb') as pickle_file: # Open the file for reading\n",
" contacts2 = pickle.load(pickle_file) # Load the pickled contents\n",
"\n",
"for elem in contacts2:\n",
" elem.print_info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next part of this tutorial introduces basic NEURON commands."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}