{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introduction to Python Statements\n", "\n", "In this lecture we will be doing a quick overview of Python Statements. This lecture will emphasize differences between Python and other languages such as C++. \n", "\n", "There are two reasons we take this approach for learning the context of Python Statements:\n", "\n", " 1.) If you are coming from a different language this will rapidly accelerate your understanding of Python.\n", " 2.) Learning about statements will allow you to be able to read other languages more easily in the future." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "##Python vs Other Languages\n", "\n", "Let's create a simple statement that says:\n", "\"If a is greater than b, assign 2 to a and 4 to b\"\n", "\n", "Take a look at these two if statements (we will learn about building out if statements soon).\n", "\n", "**Version 1 (Other Languages)**\n", "\n", " if (a>b){\n", " a = 2;\n", " b = 4;\n", " }\n", " \n", "**Version 2 (Python)** \n", "\n", " if a>b:\n", " a = 2\n", " b = 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You'll notice that Python is less cluttered and much more readable than the first version. How does Python manage this?\n", "\n", "Let's walk through the main differences:\n", "\n", "Python gets rid of () and {} by incorporating two main factors: a *colon* and *whitespace*. The statement is ended with a colon, and whitespace is used (indentation) to describe what takes place in case of the statement.\n", "\n", "Another major difference is the lack of semicolons in Python. Semicolons are used to denote statement endings in many other languages, but in Python, the end of a line is the same as the end of a statement.\n", "\n", "Lastly, to end this brief overview of differences, let's take a closer look at indentation syntax in Python vs other languages:\n", "\n", "## Indentation\n", "\n", "Here is some pseudo-code to indicate the use of whitespace and indentation in Python:\n", "\n", "**Other Languages**\n", "\n", " if (x)\n", " if(y)\n", " code-statement;\n", " else\n", " another-code-statement;\n", " \n", "**Python**\n", " \n", " if x:\n", " if y:\n", " code-statement\n", " else:\n", " another-code-statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how Python is so heavily driven by code indentation and whitespace. This means that code readability is a core part of the design of the Python language.\n", "\n", "Now let's start diving deeper by coding these sort of statements in Python!" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Time to code!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }