Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# This is Markdown"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "# This is code"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "hello universe!\n"
- ]
- }
- ],
- "source": [
- "print (\"hello universe!\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Print Statement"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of 5 is <class 'str'>\n"
- ]
- }
- ],
- "source": [
- "A='5'\n",
- "print('type of',A,'is',type(A))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Data types"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of 5 is <class 'int'>\n"
- ]
- }
- ],
- "source": [
- "A=5\n",
- "print('type of',A,'is',type(A))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of 5.0 is <class 'float'>\n"
- ]
- }
- ],
- "source": [
- "A=5.0\n",
- "print('type of',A,'is',type(A))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of True is <class 'bool'>\n"
- ]
- }
- ],
- "source": [
- "A=True\n",
- "print('type of',A,'is',type(A))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of (100+2j) is <class 'complex'>\n"
- ]
- }
- ],
- "source": [
- "A=100+2j\n",
- "print('type of',A,'is',type(A))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of [1, 2, 3, 4, 5] is <class 'list'>\n",
- "type of 2 is <class 'int'>\n"
- ]
- }
- ],
- "source": [
- "A=[1,2,3,4,5]\n",
- "print('type of',A,'is',type(A))\n",
- "print('type of',A[1],'is',type(A[1]))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of {1: 'orange', 2: 'apple', 3: 'mango', 4: 'banana'} is <class 'dict'>\n",
- "type of orange is <class 'str'>\n"
- ]
- }
- ],
- "source": [
- "Dict={1:'orange',2:'apple',3:'mango',4:'banana'}\n",
- "print('type of',Dict,'is',type(Dict))\n",
- "print('type of',Dict[1],'is',type(Dict[1]))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "type of (1, 2, 3, 4) is <class 'tuple'>\n",
- "type of 2 is <class 'int'>\n"
- ]
- }
- ],
- "source": [
- "Tup=(1,2,3,4)\n",
- "print('type of',Tup,'is',type(Tup))\n",
- "print('type of',Tup[1],'is',type(Tup[1]))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## List Operations"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 77, 3, 4, 5]\n"
- ]
- }
- ],
- "source": [
- "#Update list value\n",
- "A[1]=77\n",
- "print(A)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 77, 3, 5]\n"
- ]
- }
- ],
- "source": [
- "# delete list elements\n",
- "\n",
- "del A[3]\n",
- "print(A)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 77]\n"
- ]
- }
- ],
- "source": [
- "# to print upto first 2 elements. Here, the 0, 1 position elements are printed.\n",
- "print(A[0:2])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[77, 3, 5]\n"
- ]
- }
- ],
- "source": [
- "# Here starting from position 1 remaining elements are printed.\n",
- "print(A[1:])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 77, 3]\n"
- ]
- }
- ],
- "source": [
- "#Here upto 2nd index elements are printed. \n",
- "print(A[:3])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 77, 3, 5]\n"
- ]
- }
- ],
- "source": [
- "# All the elements are printed it is equal to print(A) but more understandable that we are going to print a list.\n",
- "print(A[:])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['Thor', 'ironman', 'Captain America', 'Hulk', 'Ant man', 'Captain Marvel']\n"
- ]
- }
- ],
- "source": [
- "# Addition of lists\n",
- "A=['Thor', 'ironman', 'Captain America', 'Hulk']\n",
- "B=['Ant man','Captain Marvel']\n",
- "C = A + B\n",
- "\n",
- "print (C)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 2, 1, 2, 2]\n"
- ]
- }
- ],
- "source": [
- "# Multiplication of lists\n",
- "\n",
- "A = [1,2,2]\n",
- "\n",
- "B = A*2\n",
- "\n",
- "print(B)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "minimum is 1\n",
- "maximum is 2\n",
- "length is 6\n"
- ]
- }
- ],
- "source": [
- "# List functions\n",
- "\n",
- "print('minimum is',min(B))\n",
- "print('maximum is',max(B))\n",
- "print('length is',len(B))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after appending [1, 2, 2, 1, 2, 2, ['a', 'b']]\n"
- ]
- }
- ],
- "source": [
- "B.append(['a','b'])\n",
- "print('after appending',B)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after extending [1, 2, 2, 1, 2, 2, ['a', 'b'], 'b', 'a', 'c']\n"
- ]
- }
- ],
- "source": [
- "B.extend(['b','a','c'])\n",
- "print('after extending',B)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "count of 2 in B list 4\n"
- ]
- }
- ],
- "source": [
- "print('count of 2 in B list',B.count(2))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after insert [1, 2, 2, 'hit', 1, 2, 2, ['a', 'b'], 'b', 'a', 'c']\n"
- ]
- }
- ],
- "source": [
- "B.insert(3,'hit')\n",
- "print('after insert',B)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after sort in ascending order [1, 1, 2, 2, 2, 3, 4, 5, 7]\n",
- "after sort in descending order [7, 5, 4, 3, 2, 2, 2, 1, 1]\n"
- ]
- }
- ],
- "source": [
- "B=[2,3,1,2,2,1,4,5,7]\n",
- "B.sort()\n",
- "print('after sort in ascending order',B)\n",
- "B.sort(reverse=True)\n",
- "print('after sort in descending order',B)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after converting into list ['C', 'S', 'K']\n"
- ]
- }
- ],
- "source": [
- "# Convert into list\n",
- "\n",
- "B = \"CSK\"\n",
- "C = list(B)\n",
- "\n",
- "print('after converting into list',C)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Tuple Operations"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "tuple3: ('Python', 'PHP', 'HTML', 'C', '1', '2', '3', '4')\n",
- "tuple4: ('Python', 'PHP', '3', '4')\n"
- ]
- }
- ],
- "source": [
- "# You can not modify tuple values. But you can perform an operation store into another tuple.\n",
- "tuple1 = ('Python','PHP','HTML','C')\n",
- "tuple2= ('1','2','3','4')\n",
- "tuple3= tuple1+tuple2\n",
- "print(\"tuple3: \", tuple3)\n",
- "tuple4 = tuple1[0:2]+tuple2[2:4]\n",
- "print (\"tuple4: \", tuple4) \n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Dictionary Operations"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "first name\n",
- "last name\n",
- "33\n"
- ]
- }
- ],
- "source": [
- "a = {1:\"first name\",2:\"last name\", \"age\":33}\n",
- "\n",
- "#print value having key=1\n",
- "print(a[1])\n",
- "#print value having key=2\n",
- "print(a[2])\n",
- "#print value having key=\"age\"\n",
- "print(a[\"age\"])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after adding: {1: 'first name', 2: 'last name', 'age': 33, 'name': 'Sachin'}\n"
- ]
- }
- ],
- "source": [
- "# Adding a key/value to a dictionary\n",
- "\n",
- "a['name']=\"Sachin\"\n",
- "\n",
- "print('after adding:',a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after update: {1: 'first name', 2: 'last name', 'age': 33, 'name': 'Kohli'}\n"
- ]
- }
- ],
- "source": [
- "# Update a value using key\n",
- "\n",
- "a['name']='Kohli'\n",
- "\n",
- "print('after update:',a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "after deleting a[\"age\"]: {1: 'first name', 2: 'last name', 'name': 'Kohli'}\n"
- ]
- },
- {
- "ename": "NameError",
- "evalue": "name 'a' 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<ipython-input-45-c9e1be683ef7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'after deleting a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined"
- ]
- }
- ],
- "source": [
- "# delete operations\n",
- "\n",
- "del a['age']\n",
- "\n",
- "print('after deleting a[\"age\"]:', a)\n",
- "\n",
- "del a\n",
- "\n",
- "print('after deleting a',a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 46,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{1: 'first name', 2: 'last name', 'age': 33}\n"
- ]
- }
- ],
- "source": [
- "# copying a dictionary\n",
- "a = {1:\"first name\",2:\"last name\", \"age\":33}\n",
- "\n",
- "b = a.copy()\n",
- "\n",
- "print(b)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{1: 'date', 2: 'last name', 'age': 33} , {1: 'first name', 2: 'last name', 'age': 33}\n"
- ]
- }
- ],
- "source": [
- "a[1]='date'\n",
- "print(a,',',b)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{1: 'date', 2: 'last name', 'age': 33, 'place': 'Chennai'}\n"
- ]
- }
- ],
- "source": [
- "c = a\n",
- "\n",
- "c['place']='Chennai'\n",
- "\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Branching and looping"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "true\n"
- ]
- }
- ],
- "source": [
- "a = [1,2,3,4,5]\n",
- "if 1 in a:\n",
- " print('true')\n",
- " "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "false\n"
- ]
- }
- ],
- "source": [
- "if 6 in a:\n",
- " print('true')\n",
- "else:\n",
- " print('false')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 is there\n"
- ]
- }
- ],
- "source": [
- "if 8 in a:\n",
- " print('8 is there')\n",
- "elif 1 in a:\n",
- " print('1 is there')\n",
- "else:\n",
- " print('nothing')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## For loop"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "2\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "for i in range(0,5,2):\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "2\n",
- "3\n",
- "4\n",
- "5\n"
- ]
- }
- ],
- "source": [
- "for each in a:\n",
- " print(each)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 62,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "for j in range(5):\n",
- " print(j)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## While loop"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 66,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "3\n",
- "6\n",
- "10\n",
- "15\n",
- "21\n",
- "28\n",
- "36\n",
- "45\n"
- ]
- }
- ],
- "source": [
- "sum = 0\n",
- "count = 1\n",
- "while (count < 10):\n",
- " sum = sum + count\n",
- " print (sum)\n",
- " count= count + 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 67,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "3\n",
- "6\n",
- "10\n",
- "15\n"
- ]
- }
- ],
- "source": [
- "sum = 0\n",
- "count = 1\n",
- "while (count < 10):\n",
- " sum = sum + count\n",
- " print (sum)\n",
- " if sum == 15:\n",
- " break\n",
- " count= count + 1"
- ]
- },
- {
- "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.8"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement