{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Programming Language\n", "- http://openbookproject.net/thinkcs/python/english3e/way_of_the_program.html\n", "- the single most important skill for a computer scientist is problem solving\n", "- Python is a tool that helps computer scientists and programmers solve problems by writing code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python features\n", "- high-level general purpose programming language such as PHP, Perl, Java, JavaScript, C++\n", " - as opposed to low level machine language such as assembly\n", "- interpreted language; needs Python interpreter\n", "- platform independent/portable; python programs can be run in many platforms including Rasberry Pi\n", "- open source, can be freely downloaded and use: http://python.org\n", "- installed using Python package manager such as Anaconda or miniconda: https://www.anaconda.com/download/\n", "- two version Python 2.x and Python 3.x - Notebooks and the text use version 3.x which is the new standard" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Zen of Python" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## where to write Python code\n", "\n", "### python prompt in terminal - interactive mode\n", "- Once Python is installed, open terminal and type Python\n", "- \\>>> You'll see this chevron/python prompt\n", "- \\>>> print('hello world')\n", "\n", "### Jupyter Notebook\n", "- interactive notebook that can have live code, results and texts!\n", "- great way to learn, experiment, and take notes while coding\n", "- there are several kernels that support various programming languages for writing code using Jupyter notebook\n", "\n", "### python script using editor such as VS Code\n", "- open VS Code or your favourite editor\n", "- create a hello.py file\n", "- type print('hello world!') and save the file\n", "- run the program from terminal:\n", "- python hello.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## computer program\n", "- sequence of instructions that specifies how to perform a computation\n", "- some basic/fundamental concepts that make up a compupter program:\n", "\n", "### input\n", "- get data from keyboard, a file, or some device\n", "\n", "### output\n", "- display data/answer on screen, or save it to file or to a device\n", "\n", "### math\n", "- basic mathematical operations such as addition, subtraction, multiplication, etc.\n", "\n", "### conditional execution\n", "- check for certain condititions and execute appropriate sequence of statements\n", "\n", "### repitition\n", "- perform some action repeatedly, usually with some variation every time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## debugging\n", " - finding and getting rid of bugs/errors\n", " - as long as humans write computer codes, there'll be always errors in computer program\n", " - although frustrating at times, it is one of the most intellectually rich, challenging, and interesting part of programming" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## types of errors\n", "\n", "### syntax errors\n", "- program needs to follow Python syntax or grammer; otherwise Python interpreter will not understand and tell programmers about the errors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## run-time errors/exceptions\n", "- errors appear while programming is running;\n", "- can be handled to certain extent\n", "\n", "## semantic errors\n", "- program runs fine but gives wrong answer\n", "- can be identified and removed by doing plenty of testing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## The first program " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world!\n" ] } ], "source": [ "# ----------------------------------------------------------\n", "# hello world program\n", "# by: John Doe\n", "# Jan 1 2017\n", "# Copyright: Anyone may freely copy or modify this program\n", "#----------------------------------------------------------\n", "print('hello world!') # say hello to the beautiful world!" ] }, { "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.0" } }, "nbformat": 4, "nbformat_minor": 2 }