{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Tutorial - I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is not a comprehensive Python tutorial but instead is intended to highlight the parts of the language that will be most important for us. You can download python form the [python.org](https://docs.python.org/3/library/index.html). I recommend installing Anaconda distribution, which already includes most of the libraries that are need to do data science. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comment" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This type of comment is used to document the purpose of functions and classes.'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This is a one-line Python comment - code blocks are so useful!\n", "\"\"\"This type of comment is used to document the purpose of functions and classes.\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Declaration/Initialization " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Remember values, not variables, have data types.\n", "# A variable can be reassigned to contain a different data type.\n", "answer = 42\n", "answer = \"The answer is 42.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Whitespace Formatting \n", "\n", "Many languages use curly braces to delmit blocks of code. Python uses indentation(space and tab). This makes python code very readable, but it also means that you have to be very carefull with you formating:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Note that there is no bracket and semicolon ';'\n", "# for i in [1,2,3,4,5]:\n", "# print(i+i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmetic \n", "Python 2.7 uses integer division by default, that is not what we want most of the time. Python 3.x uses normal division." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.5\n", "2\n" ] } ], "source": [ "# normal division\n", "print(5/2)\n", "# integer division\n", "print(5//2) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings\n", "\n", "Strings can be delimited by single or double quotation marks (but the quotes have to match). Python used backslashes to encode special characters. You can use triple quotes to create multiline strings." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "single = 'string' \n", "double = \"string\"\n", "triple = \"\"\"string\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loop " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n", "d\n" ] } ], "source": [ "# loop using 'in' keyword\n", "for item in ('a','b','c','d'):\n", " print(item)\n", "\n", "# traditional loop\n", "start, end, calculate = 0 , 10 , 0\n", "while (start < end):\n", " calculate += calculate\n", " start += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditionals" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Like Seriously !\n" ] } ], "source": [ "# conditional if - else ladder \n", "if False:\n", " print(\"Wrong Condition !\")\n", "elif False:\n", " print(\"Again Wrong Condition !\")\n", "else:\n", " print(\"Like Seriously !\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Types" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "boolean = True\n", "number = 1.1\n", "string = \"Strings can be declared with single or double quotes.\"\n", "list = [\"Lists can have\", 1, 2, 3, 4, \"or more types together!\"]\n", "tuple = (\"Tuples\", \"can have\", \"more than\", 2, \"elements!\")\n", "dictionary = {'one': 1, 'two': 2, 'three': 3}\n", "variable_with_zero_data = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function\n", "A function is a rule for taking zero or more imputs and returning a corresponding output. To define function in python we use def. There is one special type of function in python known as lanbda function. More about lambda function [here](http://www.secnetix.de/olli/Python/lambda_functions.hawk)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# function declaration/defination\n", "def function_square(x):\n", " return(x*x)\n", "\n", "# function calling\n", "function_square(3)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# lambda function declaration/defination\n", "function_lambda = lambda x: x**2\n", "\n", "# function calling\n", "function_lambda(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exceptions\n", "\n", "When something goes wrong,Python raises an exception. Unhandled, these will cause your program to crash. Exception can be handled using **try** and **except**." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Divide by Zero\n" ] } ], "source": [ "try:\n", " print(0/0)\n", "except ZeroDivisionError:\n", " print(\"Divide by Zero\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modules\n", "Certain features of python are note loaded by default. These include both features included as part of the language as well as third-party features that yoy can download yourself. In order to use these features, you'll need to **import** the modules that contain them." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import re # import the whole module\n", "import re as regex # import the module as an alias\n", "import matplotlib.pyplot as plt # import only some part of the module\n", "from collections import defaultdict, Counter # import the specific feature or value from the module" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# never never do this\n", "from re import * # never do this" ] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }