{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1)Numbers:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 - \n", "2.6 - \n", "(2+8j) - \n" ] } ], "source": [ "#integer\n", "x = 2\n", "print(x,\"-\",type(x))\n", "#float\n", "x=2.6\n", "print(x,\"-\",type(x))\n", "#complex\n", "x=2+8j\n", "print(x,\"-\",type(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2)Strings :" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "VIT - \n" ] } ], "source": [ "x=\"VIT\"\n", "print(x,\"-\",type(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3)Lists:\n", "\n", "A collection of elements in []. \n", "\n", "Lists are mutable and flexible,i.e they can be edited." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['AI', 'ML', 'DL'] - \n" ] } ], "source": [ "x = [\"AI\", \"ML\", \"DL\"]\n", "print(x,\"-\",type(x))" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AI\n", "ML\n", "DL\n" ] } ], "source": [ "# Traversing a list\n", "for i in x:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['AI', 'ML']\n", "['ML']\n" ] } ], "source": [ "# pop element from the list\n", "# It is based on LIFO principle: Last In first Out\n", "x.pop()\n", "print(x)\n", "\n", "del x[0];\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ML', 'DL']\n", "['ML', 'DL', 'ML', 'DL']\n" ] } ], "source": [ "# inserting an element in the list at some index\n", "x.insert(3,\"DL\")\n", "print(x)\n", "# Repetition of list\n", "y = x*2\n", "# del x\n", "# print(x)\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ML', 'DL', 'ML', 'DL']\n", "['DL', 'DL', 'ML', 'ML']\n", "['ML', 'ML', 'DL', 'DL']\n", "['ML', 'ML', 'DL']\n" ] } ], "source": [ "# built-in functions\n", "print(y)\n", "y.sort()\n", "print(y)\n", "y.reverse()\n", "print(y)\n", "y.remove(\"DL\")\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4)Tuples : \n", "\n", "Tuples are enclosed in ( )\n", "\n", "They are immutable. We can not edit or change them, once created." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('AI', 'ML', 'DL') - \n", "AI is the fisrt element in tuple ('AI', 'ML', 'DL')\n", "('AI', 'ML') are the fisrt two elements in tuple ('AI', 'ML', 'DL')\n", "DL is the last element in tuple ('AI', 'ML', 'DL')\n", "('ML', 'DL') are the last two elements in tuple ('AI', 'ML', 'DL')\n", "('AI', 'ML', 'DL', 'NLP', 'EDA')\n" ] } ], "source": [ "x = (\"AI\", \"ML\", \"DL\")\n", "print(x,\"-\",type(x))\n", "print(x[0],\" is the fisrt element in tuple\",x)\n", "print(x[0:2],\" are the fisrt two elements in tuple\",x)\n", "print(x[-1],\" is the last element in tuple\",x)\n", "print(x[-2:],\" are the last two elements in tuple\",x)\n", "x1 = (\"NLP\", \"EDA\")\n", "print(x + x1)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "AI\n", "ML\n", "DL\n" ] } ], "source": [ "# Membership\n", "print(\"Ai\" in x)\n", "# Traversing a tuple\n", "for i in x: print(i)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "ML\n", "AI\n", "\n", "\n" ] } ], "source": [ "print(len(x))\n", "print(max(x))\n", "print(min(x))\n", "x = [\"AI\", \"ML\", \"DL\"]\n", "print(type(x))\n", "y = tuple(x)\n", "print(type(y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5)Dictionary\n", "\n", "Unordered nature.\n", "\n", "{key:value} - key is immutable, value is mutable." ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Name': 'Rohan', 'Age': 8, 'Class': 'Four'} - \n" ] } ], "source": [ "x = {'Name': 'Rohan', 'Age': 8, 'Class': 'Four'}\n", "print(x,\"-\",type(x))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Rohan'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x['Name']" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['Rohan', 8, 'Four'])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.values()" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['Name', 'Age', 'Class'])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.keys()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Name': 'Rohan', 'Age': 8, 'Class': 'Four', 'School': 'VIT'}\n" ] } ], "source": [ "x['School'] = \"VIT\";\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Name': 'Rohan', 'Age': 8, 'Class': 'Four', 'School': 'VIT'}\n" ] } ], "source": [ "len(x)\n", "y = x.copy()\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Age': 8, 'Class': 'Four', 'School': 'VIT'}\n", "{}\n" ] }, { "ename": "NameError", "evalue": "name 'y' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [65]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(y)\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m y; \u001b[38;5;66;03m# delete entire dictionary\u001b[39;00m\n\u001b[1;32m----> 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43my\u001b[49m)\n", "\u001b[1;31mNameError\u001b[0m: name 'y' is not defined" ] } ], "source": [ "del y['Name']; # remove entry with key 'Name'\n", "print(y)\n", "y.clear(); # remove all entries in dict\n", "print(y)\n", "del y; # delete entire dictionary\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'c', 'b', 'd']\n" ] } ], "source": [ "# For deleting duplicates from a list, Dictionary is used\n", "\n", "x=['a','c','b','a','d','b']\n", "x= list(dict.fromkeys(x)) # creates a dictionary from x as keys and then coverts that dictionary to list\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Order of operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PEMDAS - \n", "\n", "P - Paranthesis -- 1\n", "\n", "E - Exponentiation -- 2\n", "\n", "M - Multiplication--3\n", "\n", "D - division--3\n", "\n", "A- Addition--4\n", "\n", "S - Subtraction--4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1) Arithmetic: + , - , * , /, % , // , ** " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.5\n", "2\n", "1\n" ] } ], "source": [ "print(5/2) # Divison ans\n", "print(5//2) # Quotient\n", "print(5%2) #reminder" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2) Relational : >,<,>=,<=,==,!=" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3) Logical: and,or,not" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Membership" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "l = [\"1\",\"2\",\"3\"]\n", "print('3' in l)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"VIT\"\n", "\"I\" in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions :" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1) Type conversions \n", "\n", "Converting one datatype to other" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('1', '2', '3')\n" ] } ], "source": [ "l = [\"1\",\"2\",\"3\"]\n", "new_l = tuple(l)\n", "print(new_l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2) Math functions\n", "\n", "Perform mathematical operations - sqrt(),sin(),cos(),floor(),ceil(),log(),pow(x,y)\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.0\n" ] } ], "source": [ "import math as m\n", "a=16\n", "sq = m.sqrt(a)\n", "print(sq)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Floor prints the largest integer which is smaller than the number: 7\n", "Ceil prints the smallest integer which is larger than the number: 8\n" ] } ], "source": [ "x = 7.8\n", "print(\"Floor prints the largest integer which is smaller than the number: \",m.floor(x) )\n", "print(\"Ceil prints the smallest integer which is larger than the number: \",m.ceil(x) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3) User Defined Functions" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vaishnavi\n", "vaishnavi\n" ] } ], "source": [ "# User defined function to print your name\n", "def abc(name):\n", " print(name)\n", "\n", "name = input() # input from user\n", "abc(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings operations:" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "it \n", "iannehC tiV\n", "vit chennai\n", "VIT CHENNAI\n", "9\n", "Vit Chennai\n", "False\n", "Vit Chenn#i\n", "False\n", "vIT cHENNAI\n", "['Vit Chenn', 'i']\n" ] } ], "source": [ "s = \"Vit Chennai\"\n", "# print elements from 1 to 3 index\n", "print(s[1:4])\n", "# print elements in reverse order\n", "print(s[::-1])\n", "# convert all to lower case\n", "print(s.lower())\n", "#convert all to upper case\n", "print(s.upper())\n", "#find a letter\n", "print(s.find('a'))\n", "#removes space from end and beginning\n", "s=s.strip() \n", "print(s)\n", "# are all the elements alphabets?\n", "print(s.isalpha())\n", "#replace an element woth other\n", "print(s.replace('a','#'))\n", "#are all elements digits?\n", "print(s.isdigit())\n", "#Convert lower to upper and upper to lower case\n", "print(s.swapcase())\n", "#split the string from an element\n", "s=s.replace('a','#')\n", "print(s.split('#'))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# File handing" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file_name.txt\n", "True\n" ] } ], "source": [ "# open a file \n", "f = open(\"file_name.txt\",'w') # this opens a file for writing if it alraedy exists or else create a new file\n", "f.write(\"This is a new file created. \")\n", "print(f.name)\n", "f.close()\n", "print(f.closed) # true if f is closed" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "# open a file only for reading\n", "f = open(\"file_name.txt\",\"r\") #for this a file should exist \n", "f.close()" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "#append a file\n", "f=open(\"file_name.txt\",'a')\n", "f.write(\"this is in continuation\")\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a new file created.\n", " this is in continuation\n", "51\n" ] } ], "source": [ "#some functions\n", "f = open(\"file_name.txt\",\"r\") \n", "print(f.read(27)) # first 10 characters\n", "print(f.readline()) # read first line\n", "print(f.tell()) # position of cursor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map() function\n", "\n", "Apply on iterables\n", "\n", "Returns a new list\n", "\n", "Syntax: map(function,iterables)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "# code without using map()\n", "def dub(a):\n", " return a*2\n", "\n", "x=dub(5)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8, 10]\n" ] } ], "source": [ "# code using map()\n", "l=[1,2,3,4,5]\n", "x = map(dub,l)\n", "print(list(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lambda ()\n", "\n", "Reduce code size" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# square \n", "sqr = lambda a:a*a\n", "sqr(5)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solve linear equation\n", "z=lambda x,y: 5*x+3*y\n", "z(2,4)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8]" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# lambda with map\n", "l=[1,2,3,4]\n", "list(map(lambda x:x*2,l))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## filter()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Syntax : filter(function,iterables)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 5]\n" ] } ], "source": [ "def sq(x):\n", " if x>3:\n", " return x*x\n", "\n", "l=[1,2,3,4,5]\n", "sqr = filter(sq,l)\n", "print(list(sqr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## reduce()\n", "\n", "Carries out series calculation and returns a single answer" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "import functools\n", "from functools import reduce\n", "\n", "def add(a,b):\n", " return a+b\n", "\n", "l=[1,2,3,4]\n", "x= reduce(add,l) # 1+2=3 , 3+3=6, 6+4=10\n", "print(x)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }