From 55a2b8cb0d07653c10ecaf079e223380ece7b401 Mon Sep 17 00:00:00 2001 From: Mohamed Salman Date: Tue, 29 Oct 2019 23:29:52 +0530 Subject: [PATCH 1/2] salman | added some Python Basics Snippets --- PythonBasics.ipynb | 410 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 410 insertions(+) create mode 100644 PythonBasics.ipynb diff --git a/PythonBasics.ipynb b/PythonBasics.ipynb new file mode 100644 index 0000000..19ea075 --- /dev/null +++ b/PythonBasics.ipynb @@ -0,0 +1,410 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Welcome to Jupyter!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# This repo contains an introduction to [Jupyter](https://jupyter.org) and [IPython](https://ipython.org).\n", + "\n", + "Outline of some basics:\n", + "\n", + "* [Notebook Basics](../examples/Notebook/Notebook%20Basics.ipynb)\n", + "* [IPython - beyond plain python](../examples/IPython%20Kernel/Beyond%20Plain%20Python.ipynb)\n", + "* [Markdown Cells](../examples/Notebook/Working%20With%20Markdown%20Cells.ipynb)\n", + "* [Rich Display System](../examples/IPython%20Kernel/Rich%20Output.ipynb)\n", + "* [Custom Display logic](../examples/IPython%20Kernel/Custom%20Display%20Logic.ipynb)\n", + "* [Running a Secure Public Notebook Server](../examples/Notebook/Running%20the%20Notebook%20Server.ipynb#Securing-the-notebook-server)\n", + "* [How Jupyter works](../examples/Notebook/Multiple%20Languages%2C%20Frontends.ipynb) to run code in different languages." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also get this tutorial and run it on your laptop:\n", + "\n", + " git clone https://github.com/ipython/ipython-in-depth\n", + "\n", + "Install IPython and Jupyter:\n", + "\n", + "with [conda](https://www.anaconda.com/download):\n", + "\n", + " conda install ipython jupyter\n", + "\n", + "with pip:\n", + "\n", + " # first, always upgrade pip!\n", + " pip install --upgrade pip\n", + " pip install --upgrade ipython jupyter\n", + "\n", + "Start the notebook in the tutorial directory:\n", + "\n", + " cd ipython-in-depth\n", + " jupyter notebook" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"It's a string\"" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"It's a string\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "salman\n" + ] + } + ], + "source": [ + "print(\"salman\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"-\" * 20)? (, line 4)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m print \"-\" * 20\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"-\" * 20)?\n" + ] + } + ], + "source": [ + "table = 8\n", + "start = 1\n", + "max = 10\n", + "print \"-\" * 20\n", + "print \"The table of 8\"\n", + "print \"-\" * 20\n", + "i = start\n", + "while i <= max:\n", + " result = i * table\n", + " print i, \" * \", table, \" =\" , result\n", + " i = i + 1\n", + "print \"-\" * 20\n", + "print \"Done counting...\"\n", + "print \"-\" * 20" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--------------------\n", + "The table of 8\n", + "--------------------\n", + "1 * 8 = 8\n", + "2 * 8 = 16\n", + "3 * 8 = 24\n", + "4 * 8 = 32\n", + "5 * 8 = 40\n", + "6 * 8 = 48\n", + "7 * 8 = 56\n", + "8 * 8 = 64\n", + "9 * 8 = 72\n", + "10 * 8 = 80\n", + "--------------------\n", + "Done counting...\n", + "--------------------\n" + ] + } + ], + "source": [ + "table = 8\n", + "start = 1\n", + "max = 10\n", + "print(\"-\" * 20)\n", + "print(\"The table of 8\")\n", + "print(\"-\" * 20)\n", + "i = start\n", + "while i <= max:\n", + " result = i * table\n", + " print(i, \" * \", table, \" =\" , result)\n", + " i = i + 1\n", + "print(\"-\" * 20)\n", + "print(\"Done counting...\")\n", + "print(\"-\" * 20)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'raw_input' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mraw_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter a number between 1 - 10\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"you entered number\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'raw_input' is not defined" + ] + } + ], + "source": [ + "number = int(raw_input(\"Enter a number between 1 - 10\"))\n", + "\n", + "print(\"you entered number\", number)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number between 1 - 10hello\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'hello'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter a number between 1 - 10\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"you entered number\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'hello'" + ] + } + ], + "source": [ + "number = int(input(\"Enter a number between 1 - 10\"))\n", + "\n", + "print(\"you entered number\", number)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number between 1 - 103\n", + "you entered number 3\n" + ] + } + ], + "source": [ + "number = int(input(\"Enter a number between 1 - 10\"))\n", + "\n", + "print(\"you entered number\", number)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid character in identifier (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m file = open(“testfile.txt”,”w”)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n" + ] + } + ], + "source": [ + "file = open(“testfile.txt”,”w”) \n", + " \n", + "file.write(“Hello World”) \n", + "file.write(“This is our new text file”) \n", + "file.write(“and this is another line.”) \n", + "file.write(“Why? Because we can.”) \n", + " \n", + "file.close() " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid character in identifier (, line 7)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m w.setWindowTitle(‘Guru99’)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n" + ] + } + ], + "source": [ + "import sys\n", + "from PyQt5.QtWidgets import QApplication, QWidget\n", + "if __name__ == \"__main__\":\n", + " app = QApplication(sys.argv)\n", + " w = QWidget()\n", + " w.resize(300,300)\n", + " w.setWindowTitle(‘Guru99’)\n", + " w.show()\n", + " sys.exit(app.exec_())" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'calendar' has no attribute 'SUNDAT'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcalendar\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcalendar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTextCalendar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcalendar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSUNDAT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformatmonth\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2025\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: module 'calendar' has no attribute 'SUNDAT'" + ] + } + ], + "source": [ + "import calendar\n", + "\n", + "c = calendar.TextCalendar(calendar.SUNDAT)\n", + "str = c.formatmonth(2025, 1)\n", + "print(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " January 2025\n", + "Su Mo Tu We Th Fr Sa\n", + " 1 2 3 4\n", + " 5 6 7 8 9 10 11\n", + "12 13 14 15 16 17 18\n", + "19 20 21 22 23 24 25\n", + "26 27 28 29 30 31\n", + "\n" + ] + } + ], + "source": [ + "import calendar\n", + "\n", + "c = calendar.TextCalendar(calendar.SUNDAY)\n", + "str = c.formatmonth(2025, 1)\n", + "print(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Python']\n" + ] + } + ], + "source": [ + "import re\n", + "xx = \"Python is a Simple Programming Language\"\n", + "r1 = re.findall(r\"^\\w+\",xx)\n", + "print(r1)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['we', 'are', 'splitting', 'the', 'words']\n" + ] + } + ], + "source": [ + "import re\n", + "print((re.split(r'\\s','we are splitting the words')))" + ] + }, + { + "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.6.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From bcbdbf1a944a42e5018434194f03549bdfbf7a2a Mon Sep 17 00:00:00 2001 From: Mohamed Salman Date: Tue, 29 Oct 2019 23:32:54 +0530 Subject: [PATCH 2/2] salman | removed unnecessary lines --- PythonBasics.ipynb | 50 ---------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/PythonBasics.ipynb b/PythonBasics.ipynb index 19ea075..483b9cb 100644 --- a/PythonBasics.ipynb +++ b/PythonBasics.ipynb @@ -1,55 +1,5 @@ { "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Welcome to Jupyter!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# This repo contains an introduction to [Jupyter](https://jupyter.org) and [IPython](https://ipython.org).\n", - "\n", - "Outline of some basics:\n", - "\n", - "* [Notebook Basics](../examples/Notebook/Notebook%20Basics.ipynb)\n", - "* [IPython - beyond plain python](../examples/IPython%20Kernel/Beyond%20Plain%20Python.ipynb)\n", - "* [Markdown Cells](../examples/Notebook/Working%20With%20Markdown%20Cells.ipynb)\n", - "* [Rich Display System](../examples/IPython%20Kernel/Rich%20Output.ipynb)\n", - "* [Custom Display logic](../examples/IPython%20Kernel/Custom%20Display%20Logic.ipynb)\n", - "* [Running a Secure Public Notebook Server](../examples/Notebook/Running%20the%20Notebook%20Server.ipynb#Securing-the-notebook-server)\n", - "* [How Jupyter works](../examples/Notebook/Multiple%20Languages%2C%20Frontends.ipynb) to run code in different languages." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can also get this tutorial and run it on your laptop:\n", - "\n", - " git clone https://github.com/ipython/ipython-in-depth\n", - "\n", - "Install IPython and Jupyter:\n", - "\n", - "with [conda](https://www.anaconda.com/download):\n", - "\n", - " conda install ipython jupyter\n", - "\n", - "with pip:\n", - "\n", - " # first, always upgrade pip!\n", - " pip install --upgrade pip\n", - " pip install --upgrade ipython jupyter\n", - "\n", - "Start the notebook in the tutorial directory:\n", - "\n", - " cd ipython-in-depth\n", - " jupyter notebook" - ] - }, { "cell_type": "code", "execution_count": 1,