{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Numbers\n", "\n", "There’s nothing magical about numbers in Python, and we’ve already discovered \n", "how we perform operations on them." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "(2 * (1 + 3) - 5) / 0.5" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "11 % 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python also lets you manipulate complex numbers, using `j` to represent the \n", "complex term." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "a = 1 + 4j\n", "b = 4 - 1j\n", "a - b" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Complex numbers are objects, of course, and have some useful functions and \n", "properties attached to them." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "a_conj = a.conjugate()\n", "print(a_conj)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "a_img = a.imag" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "a_real = a.real\n", "None" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Somewhat confusingly, computing the magnitude of a complex number can be done \n", "with the `abs` method, which is available globally." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "abs(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "(a.real**2 + a.imag**2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "(a.real**2 + a.imag**2) ** 0.5" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This also demonstrates the `**` operator, which for real numbers corresponds to \n", "exponentiation.\n", "\n", "Each type of number can be created _literally_, like we’ve been doing, by just \n", "typing the number into your shell or source code, and by using the correspond \n", "methods." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "int()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "float()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "complex()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "" }, "tags": [] }, "source": [] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython" }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python" } }, "nbformat": 4, "nbformat_minor": 4 }