{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting The Python Basics Right\n", "\n", "Special thanks to PyQuantNews for donating this content. Subscribe [here](https://www.pyquantnews.com/) for free Python resources that will help you get started with Python for Quant Finance.\n", "\n", "## Code Comments\n", "\n", "A comment is a note made by a programmer in the source code of a program. Its purpose is to clarify the source code and make it easier for people to follow along with what is happening. Anything in a comment is generally ignored when the code is actually run, making comments useful for including explanations and reasoning as well as removing specific lines of code that you may be unsure about. Comments in Python are created by using the pound symbol (`# Insert Text Here`). Including a `#` in a line of code will comment out anything that follows it." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# This is a comment\n", "# These lines of code will not change any values\n", "# Anything following the first # is not run as code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may hear text enclosed in triple quotes (`\"\"\" Insert Text Here \"\"\"`) referred to as multi-line comments, but this is not entirely accurate. This is a special type of `string` (a data type we will cover), called a `docstring`, used to explain the purpose of a function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' This is a special string '" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\" This is a special string \"\"\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mInit signature:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m/\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m \n", "str(object='') -> str\n", "str(bytes_or_buffer[, encoding[, errors]]) -> str\n", "\n", "Create a new string object from the given object. If encoding or\n", "errors is specified, then the object must expose a data buffer\n", "that will be decoded using the given encoding and error handler.\n", "Otherwise, returns the result of object.__str__() (if defined)\n", "or repr(object).\n", "encoding defaults to sys.getdefaultencoding().\n", "errors defaults to 'strict'.\n", "\u001b[0;31mType:\u001b[0m type\n", "\u001b[0;31mSubclasses:\u001b[0m StrEnum, DeferredConfigString, FoldedCase, _rstr, _ScriptTarget, _ModuleTarget, LSString, include, Keys, InputMode, ..." ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "str??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make sure you read the comments within each code cell (if they are there). They will provide more real-time explanations of what is going on as you look at each line of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Objects - Basic Types & Variables\n", "\n", "Everything in Python is an **object** and every object in Python has a **type**. Some of the basic types include:\n", "\n", "- **`int`** (integer; a whole number with no decimal place)\n", " - `10`\n", " - `-3`\n", "- **`float`** (float; a number that has a decimal place)\n", " - `7.41`\n", " - `-0.006`\n", "- **`str`** (string; a sequence of characters enclosed in single quotes, double quotes, or triple quotes)\n", " - `'this is a string using single quotes'`\n", " - `\"this is a string using double quotes\"`\n", " - `'''this is a triple quoted string using single quotes'''`\n", " - `\"\"\"this is a triple quoted string using double quotes\"\"\"`\n", "- **`bool`** (boolean; a binary value that is either true or false)\n", " - `True`\n", " - `False`\n", "- **`NoneType`** (a special type representing the absence of a value)\n", " - `None`\n", "\n", "In Python, a **variable** is a name you specify in your code that maps to a particular **object**, object **instance**, or value.\n", "\n", "By defining variables, we can refer to things by names that make sense to us. Names for variables can only contain letters, underscores (`_`), or numbers (no spaces, dashes, or other characters). Variable names must start with a letter or underscore.\n", "\n", "