{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 001_basics\n", "\n", "[Source](https://github.com/iArunava/Python-TheNoTheoryGuide/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The Zen of Python\n", "import this" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Comments\n", "# This is a python tutorial and a single line comment\n", "''' This is a multiline comment\n", " pretty awesome!!\n", " Let me introduce you to jennifer!'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simple imports\n", "import math\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# importing specific functions from modules\n", "# imports just the factorial function from math\n", "from math import factorial\n", "\n", "# imports all the functions from math\n", "from math import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Giving aliases\n", "# The Module name is alaised\n", "import math as m\n", "\n", "# The function name is alaised\n", "from math import factorial as fact" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Calling imported functions\n", "# If you import the module you have to call the functions from the module\n", "import math\n", "print (math.factorial(12))\n", "\n", "# If you import the functions you can call the function as if it is in your program\n", "from random import randrange as rg\n", "print (rg(23, 1000))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Variables\n", "msg = \"Python!\" # String\n", "v2 = 'Python!' # Also String works same\n", "v1 = 2 # Numbers\n", "v3 = 3.564 # Floats / Doubles\n", "v4 = True # Boolean (True / False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# print() \n", "# automatically adds a newline\n", "print (msg)\n", "print (v2)\n", "print (v1)\n", "print (v3)\n", "print (v4)\n", "print (\"Hello Python!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Note: Both \" and ' can be used to make strings. And this flexibility allows for the following:\n", "msg2 = 'Jennifer said, \"I love Python!\"'\n", "msg3 = \"After that Jennifer's Python Interpreter said it back to her!\"\n", "msg4 = 'Of Course she used the command `print(\"I love Jennifer\")`'\n", "\n", "print (msg2)\n", "print (msg3)\n", "print (msg4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# input()\n", "msg = input()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# input() with message\n", "msg = input (\"Provide some input: \")\n", "print (msg)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check for specific input without storing it\n", "if input(\"Enter something: \") == \"something\":\n", " print (\"Something something\")\n", "else: print (\"Not Something\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Python takes every input as a string\n", "# So, if required you can convert to the required type\n", "msg = input(\"Enter a number: \")\n", "print (type(msg))\n", "\n", "msg = int(input (\"Enter a number again, if not a number this will throw an error: \"))\n", "print (type(msg))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Basic Arithmetic operations\n", "# Add\n", "print (3 + 2)\n", "print (3.4565 + 56.232)\n", "print ('------------')\n", "\n", "# Subtract\n", "print (3 - 4)\n", "print (34.56 - 3.78)\n", "print ('------------')\n", "\n", "# Multiply\n", "print (4 * 3)\n", "print (7.56 * 34)\n", "print ('------------')\n", "\n", "# Division\n", "print (5 / 2)\n", "print (5.0 / 2)\n", "print (5 / 2.0)\n", "print (25.0 / 5)\n", "print ('------------')\n", "\n", "# Exponents\n", "print (4 ** 4)\n", "print (5.67 ** 3)\n", "print ('------------')\n", "\n", "# Modulo\n", "print (10%3)\n", "print (10%11)" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }