See More

{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WBcrMRiUq_8O", "outputId": "d96aaa99-b7ec-4f8c-ed39-ca37f7095e4f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print(\"Hello World\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "flkA6wFQrZCc", "outputId": "ce7b17fb-a1ef-4936-a6b6-db43dd02f2fb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /|\n", " / |\n", " / |\n", "/___|\n" ] } ], "source": [ "print(\" /|\")\n", "print(\" / |\")\n", "print(\" / |\")\n", "print(\"/___|\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SLj-L_ier2MU", "outputId": "4e42d0d6-de2e-41a3-e238-cb3342a92693" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There once was a man named John, \n", "He was 70 old years old.\n", "He really liked the name JOhn\n", "But didn't like being 36\n" ] } ], "source": [ "character_name = \"John\"\n", "character_age = \"36\"\n", "print(\"There once was a man named \" + character_name + \", \")\n", "print(\"He was 70 old years old.\")\n", "print(\"He really liked the name JOhn\")\n", "print(\"But didn't like being \" + character_age )\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DWP98GnPtA-s", "outputId": "a510221c-cdd3-40b3-d012-281dc2ee810f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Giraffe \n", " Academy\n" ] } ], "source": [ "isMale = False\n", "print(\"Giraffe \\n Academy\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DhMXWD1YtYyi", "outputId": "68c699ed-692c-4acb-f466-100c241f4bfb" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "giraffe academy\n", "True\n", "15\n", "G\n", "f\n", "8\n", "Giraffe academy\n" ] } ], "source": [ "phrase = \"Giraffe Academy\"\n", "print(phrase.lower())\n", "print(phrase.lower().islower())\n", "print(len(phrase))\n", "print(phrase[0])\n", "print(phrase[4])\n", "print(phrase.index(\"Aca\"))\n", "print(phrase.replace(\"Aca\",\"aca\"))" ] }, { "cell_type": "code", "source": [ "print(2.098)\n", "print(3+5)\n", "print(3*4)\n", "print(99*11111)\n", "print(10 % 3)\n", "\n", "my_num = -5\n", "print(my_num)\n", "\n", "print(str(my_num) + \" is my favourite number\") #adding two strings\n", "\n", "#print(my_num + \" is my favourite number\") Produce an error because we can not add number and a string\n", "\n", "print(abs(my_num)) #give the absolute value of my_num\n", "\n", "print(pow(5,6)) # this give the 5^6 \n", "\n", "print(max(3,5)) # gives the maximum of two numbers\n", "\n", "print(min(3,99))\n", "\n", "print(round(3.2)) #round the number \\\n", "\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SObZEVJRw9KD", "outputId": "4bd395d0-faa5-48b4-b149-7594cc8e9283" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2.098\n", "8\n", "12\n", "1099989\n", "1\n", "-5\n", "-5 is my favourite number\n", "5\n", "15625\n", "5\n", "3\n", "3\n" ] } ] }, { "cell_type": "code", "source": [ "from math import *\n", "\n", "my_num = -5\n", "\n", "print(floor(3.444445666)) # gives the lowest value of that number\n", "\n", "print(ceil(3.45)) # gives the hightest value of the given number" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lIb37Q8Gy1gJ", "outputId": "d0edf486-5d00-43a5-ec3b-09e641e0315e" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3\n", "4\n" ] } ] }, { "cell_type": "code", "source": [ "print(sqrt(36)) # gives the square root of a given number" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LIf8aJwEzRNo", "outputId": "e93e057a-54e6-42fe-d0d4-35b57d6393ee" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "6.0\n" ] } ] }, { "cell_type": "code", "source": [ "name = input(\"Enter your name: \")\n", "age = input(\"Enter your age : \")\n", "print(\"Hello \"+ name + \", Your are \"+age+\"!\") #Take the input from user and print it out \n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YMX8MswA0Lax", "outputId": "19e8d73f-48f0-4b7e-e2bc-7d2cc756db70" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter your name: Surender Siwach\n", "Enter your age : 27\n", "Hello Surender Siwach, Your are 27!\n" ] } ] }, { "cell_type": "code", "source": [ "num1 = input(\"Enter the first number : \")\n", "num2 = input(\"Enter the secodn number : \")\n", "result = float(num1) + float(num2)\n", "\n", "print(result)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KFzSoGOmzoZr", "outputId": "405bb3ee-0b4b-4c4d-d047-b8cd147a911b" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the first number : 34.44\n", "Enter the secodn number : 44.55\n", "78.99\n" ] } ] }, { "cell_type": "code", "source": [ "color = input(\"Enter a color : \")\n", "plural_noun = input(\"Enter a Plural Noun : \")\n", "celebrity = input(\"Enter a celebirty : \")\n", "\n", "\n", "\n", "print(\"Roses are \" +color)\n", "print(plural_noun + \" are blue \")\n", "print(\"I love \" + celebrity)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vLxaf4RwQCAv", "outputId": "7021e995-7001-4dec-ba07-7a1c26ee287f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter a color : Red\n", "Enter a Plural Noun : Nasa\n", "Enter a celebirty : Steven Hayde\n", "Roses are Red\n", "Nasa are blue \n", "I love Steven Hayde\n" ] } ] }, { "cell_type": "code", "source": [ "friends = [\"Kevin\", \"Karen\", \"Jim\",\"Bijender\", \"Gandhi\", \"Dubey\", \"Bamal\",\"Sharma\",\"Mahesh\"]\n", "\n", "print(friends[0])\n", "print(friends[1])\n", "print(friends[-2])\n", "print(friends[1:]) #From first to all other elements\n", "print(friends[:-1]) #Not the last element" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RfOzSblzRMgT", "outputId": "afdb4c7e-24ac-40d1-e78e-c79e20ddea3c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Kevin\n", "Karen\n", "Sharma\n", "['Karen', 'Jim', 'Bijender', 'Gandhi', 'Dubey', 'Bamal', 'Sharma', 'Mahesh']\n", "['Kevin', 'Karen', 'Jim', 'Bijender', 'Gandhi', 'Dubey', 'Bamal', 'Sharma']\n" ] } ] }, { "cell_type": "code", "source": [ "lucky_numbers = [4,5,6,7,8,9,10,11,12]\n", "friends = [\"Kevin\", \"Karen\", \"Jim\",\"Bijender\", \"Gandhi\", \"Dubey\", \"Bamal\",\"Sharma\",\"Mahesh\"]\n", "\n", "friends.append(\"Vikas\")\n", "\n", "friends.insert(1,\"Sanjay Kumar\")\n", "print(friends)\n", "\n", "friends.remove(\"Jim\")\n", "friends.pop()\n", "print(friends)\n", "print(friends.index(\"Kevin\"))\n", "\n", "friends.sort()\n", "print(friends)\n", "\n", "\n", "friends.clear()\n", "lucky_numbers.reverse() # To reverse the order of the list\n", "print(lucky_numbers)\n", "\n", "print(friends)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wCvsiSyrShNB", "outputId": "fc1807d1-c9e8-412b-9df1-fcd84f3f59e9" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['Kevin', 'Sanjay Kumar', 'Karen', 'Jim', 'Bijender', 'Gandhi', 'Dubey', 'Bamal', 'Sharma', 'Mahesh', 'Vikas']\n", "['Kevin', 'Sanjay Kumar', 'Karen', 'Bijender', 'Gandhi', 'Dubey', 'Bamal', 'Sharma', 'Mahesh']\n", "0\n", "['Bamal', 'Bijender', 'Dubey', 'Gandhi', 'Karen', 'Kevin', 'Mahesh', 'Sanjay Kumar', 'Sharma']\n", "[12, 11, 10, 9, 8, 7, 6, 5, 4]\n", "[]\n" ] } ] }, { "cell_type": "markdown", "source": [ "Tuples \n" ], "metadata": { "id": "z7YXtzcucwVP" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "pDKlRVuBt24b", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "91e3e26b-7309-4ebf-8844-9005b1d3b618" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(6, 7)\n" ] } ], "source": [ "coordinates = [(4,5),(6,7),(9,100)] # we cannot change the tuple once we define them\n", "print(coordinates[1])" ] }, { "cell_type": "code", "source": [ "def say_hi(name,age):\n", " print(\"Hello \"+ name +\"!\" +\" your age is \"+ age)\n", "print(\"Top\")\n", "say_hi(\"Mike\",\"34\")\n", "print(\"Bottom\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "axYTNLxKdOdI", "outputId": "bdbe1d53-5f4a-459a-ddf8-aa1a4b81fc82" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Top\n", "Hello Mike! your age is 34\n", "Bottom\n" ] } ] }, { "cell_type": "code", "source": [ "def cube(num):\n", " return num*num*num\n", " print(\"code\") #not able to reach here\n", "\n", "result = cube(5)\n", "print(cube(4))\n", "\n", "result" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Cp4QxXVhfjgH", "outputId": "4425f737-d069-4d80-b049-befeeccd5824" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "64\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "125" ] }, "metadata": {}, "execution_count": 97 } ] }, { "cell_type": "code", "source": [ "is_Male = True\n", "is_Tall = False\n", "if is_Male and is_Tall : \n", " print(\"You are a male or tall or both\")\n", "elif is_Male and not (is_Tall):\n", " print(\"You are a short male\") \n", "else :\n", " print(\"You are not a male \")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "l08UHB2we9K8", "outputId": "03e9d763-6428-4790-e382-070254021527" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "You are a short male\n" ] } ] }, { "cell_type": "code", "source": [ "def max_num(num1, num2, num3):\n", " if num1 >= num2 and num1 >num3:\n", " return num1\n", " elif num2 >= num1 and num2 > num3:\n", " return num1\n", " else:\n", " return num3\n", "print(max_num(3,40,48))\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eMceBglBhyD9", "outputId": "372a8883-85c9-4af1-e930-fc397bf267a1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "48\n" ] } ] }, { "cell_type": "code", "source": [ "num1 = float(input(\"Enter first number : \"))\n", "op = input(\"Enter operator : \")\n", "num2 = float(input(\"Enter the second number : \"))\n", "\n", "if op == \"+\":\n", " print(num1+num2)\n", "elif op == \"-\":\n", " print(num1 - num2)\n", "elif op == \"*\":\n", " print(num1*num2)\n", "elif op == \"/\":\n", " print(num1/num2)\n", "else:\n", " print(\"Invalid operator\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uRM8j5lvnd4-", "outputId": "fbbbb0c7-8273-4bf7-86b7-d16abf41f69d" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter first number : 9\n", "Enter operator : -\n", "Enter the second number : 2\n", "7.0\n" ] } ] }, { "cell_type": "markdown", "source": [ "**Dictonaries**" ], "metadata": { "id": "nzMeRrK0q0kv" } }, { "cell_type": "code", "source": [ "monthConversion = {\n", " \"Jan\":\"January\",\n", " \"Feb\":\"Feburary\",\n", " \"Mar\":\"March\",\n", " \"Apr\":\"April\",\n", " \"May\":\"May\",\n", " \"Jun\":\"June\",\n", " \"Jul\":\"Julu\",\n", " \"Aug\":\"August\",\n", " \"Oct\":\"October\",\n", " \"Nov\":\"November\",\n", " \"Dec\":\"December\"\n", " \n", "}\n", "\n", "print(monthConversion[\"Nov\"])\n", "print(monthConversion[\"Dec\"])\n", "print(monthConversion[\"Mar\"])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RrhxGkYpnqAg", "outputId": "44e7290e-ff05-48d0-bb98-da00ead91823" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "November\n", "December\n", "March\n" ] } ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "i56X38Wftl_B", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "723e3463-d791-498f-f80c-3071b16e8e68" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "Done with the number\n" ] } ], "source": [ "i = 1\n", "while i <= 10:\n", " print(i)\n", " i += 1\n", "\n", "print(\"Done with the number\")" ] }, { "cell_type": "code", "source": [ "secret_word = \"giraffe\"\n", "guess = \"\"\n", "guess_count = 0\n", "guess_limit = 3\n", "out_of_guess = False\n", "\n", "while guess != secret_word and not(out_of_guess):\n", " if guess_count < guess_limit:\n", " guess = input(\"Enter guess : \")\n", " guess_count += 1\n", " else:\n", " out_of_guess = True\n", "\n", "if out_of_guess:\n", " print(\"Out of guesses, You lose! \")\n", "else:\n", " print(\"You win!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "npA9-1r5spi8", "outputId": "28587d36-2d34-42cf-a3eb-ea3f9c40dd40" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter guess : asdf\n", "Enter guess : sadf\n", "Enter guess : rahul\n", "Out of guesses, You lose! \n" ] } ] }, { "cell_type": "code", "source": [ "\n", "friends = [\"Jim\",\"Karen\",\"Kevin\"]\n", "for letter in \"Giraffe Academy\":\n", " print(letter)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UfcG-JrEvUEY", "outputId": "e01bb078-8f04-4fd5-b661-71d355a12623" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "G\n", "i\n", "r\n", "a\n", "f\n", "f\n", "e\n", " \n", "A\n", "c\n", "a\n", "d\n", "e\n", "m\n", "y\n" ] } ] }, { "cell_type": "code", "source": [ "friends = [\"Jim\", \"Carey\",\"Aslam\"]\n", "for friend in friends :\n", " print(friend)\n", "\n", "\n", "for index in range(12,23):\n", " print(index)\n", "\n", "for index in range(len(friends)):\n", " print(index)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aA7R7fdRwGhu", "outputId": "3aacd26e-4335-440c-abdd-26c3bee4b3a1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Jim\n", "Carey\n", "Aslam\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n", "20\n", "21\n", "22\n", "0\n", "1\n", "2\n" ] } ] }, { "cell_type": "code", "source": [ "print(2**8)\n", "\n", "def raise_to_power(base_num, pow_num):\n", " result = 1\n", " for index in range (pow_num):\n", " result = result*base_num\n", " return result\n", "\n", "print(raise_to_power(2,9))\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Lv5hWjXlxtDP", "outputId": "55fc461b-d93d-404a-c794-b84b74d88abb" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "256\n", "512\n" ] } ] }, { "cell_type": "code", "source": [ "number_grid = [\n", " [1,2,3],\n", " [4,5,6],\n", " [7,8,9],\n", " [0]\n", "]\n", "print(number_grid[3][0])\n", "\n", "for row in number_grid:\n", " for col in row:\n", " print(col)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "C3_lix8stXs3", "outputId": "ffdb94fe-e14e-43ab-b120-7de925e191c3" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "0\n" ] } ] }, { "cell_type": "code", "source": [ "def translate(phrase):\n", " translation = \"\"\n", " for letter in phrase:\n", " if letter in \"AEIOUaeiou\":\n", " translation = translation + \"g\"\n", " else :\n", " translation = translation + letter\n", " return translation\n", "\n", "print(translate(input(\"Enter a phrase : \")))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rFjIAjim2_QI", "outputId": "1190e6ec-ff90-4606-fc5c-437dd05213d9" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter a phrase : dog\n", "dgg\n" ] } ] }, { "cell_type": "code", "source": [ "#this is a comment which python is not able to read\n", "\n", "\n", "\n", "\n", "print(\"Comments are fun!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BLt0YBuq4BDo", "outputId": "2dd19c51-4510-424b-a1d8-57eee9e0f092" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Comments are fun!\n" ] } ] }, { "cell_type": "code", "source": [ "#try and except statement\n", "try:\n", " value = 10/0\n", " number = int(input(\"Enter a number : \"))\n", " print(number)\n", "except:\n", " print(\"Invalid input\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ovj-wqiv4VqW", "outputId": "0d46e787-3701-4618-9799-3028fd8457ca" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Invalid input\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Reading the file " ], "metadata": { "id": "Qmi1d0Yq7H_G" } }, { "cell_type": "code", "source": [ "#open(\"file_name\",\"r+\") #read and write\n", "open(\"File_name.txt\",\"w\") #write in the file\n", "open(\"file_name\",\"a\") #append the data in the file\n", "employee_file = open(\"file_name\",\"r\") #read the file\n", "\n", "#employee_file.close()\n", "\n", "print(employee_file.readable())\n", "\n", "\n", "print(employee_file.readline())\n", "\n", "print(employee_file.read())\n", "\n", "print(employee_file.readline())\n", "\n", "employee_file.close()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Kg3rEink5N3F", "outputId": "32b894ea-85f0-483d-c068-ad1a6467c5ce" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "True\n", "\n", "\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Appending in a file\n" ], "metadata": { "id": "QxdDklR97Ybc" } }, { "cell_type": "code", "source": [ "employee_file = open(\"employee.txt\",\"w\") #appending to a file\n", "\n", "employee_file.write(\"Tony - Driver\")\n", "\n", "employee_file.write(\"\\nSachin - ITI \")\n", "\n", "employee_file.close()\n" ], "metadata": { "id": "BIdcUBm26UmY" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "Pd-lgOI6ACoA" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import useful_tools\n", "print(useful_tools.roll_dice(10))\n", "\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RJdGulIr8T6k", "outputId": "eaa11afd-4681-4245-e154-e937f92c567e" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "6\n" ] } ] }, { "cell_type": "code", "source": [ "student = open(\"Student.py\",\"w\")\n", "student.close()" ], "metadata": { "id": "hi99lM7B9ZmG" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "from Student import Student\n", "\n", "student1 = Student(\"Jim\",\"Bussiness\",3.1,False)\n", "student2 = Student(\"Carry\",\"Rem\", 3.4, True)\n", "print(student1.name)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "NyR3ZVTtgIpP", "outputId": "bba870b6-4709-4c19-eb84-8794ac86dbac" }, "execution_count": 15, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\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;32mfrom\u001b[0m \u001b[0mStudent\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mStudent\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[0mstudent1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mStudent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Jim\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Bussiness\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3.1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;32mFalse\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[0mstudent2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mStudent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Carry\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Rem\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3.4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\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[0mstudent1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: Student() takes no arguments" ] } ] }, { "cell_type": "code", "source": [ "que = open(\"question_prompts.py\",\"w\")\n", "que.close()" ], "metadata": { "id": "zKM5Egcj66zZ" }, "execution_count": 16, "outputs": [] }, { "cell_type": "code", "source": [ "que = open(\"Question.py\",\"w\")\n", "\n", "que.close()" ], "metadata": { "id": "CwBaJZGw8sWn" }, "execution_count": 17, "outputs": [] }, { "cell_type": "code", "source": [ "from Question import Question\n", "\n", "question_prompts = [\n", " \"What colors are apples? \\n(a) Red /Green \\n (b) Purple \\n (c) Orange \\n \\n\",\n", " \"What colors are bananas? \\n(a) Teal \\n (b) Magenta \\n (c) Yellow \\n \\n\",\n", " \"What colors are strawberries? \\n(a) Yellow \\n (b) Red \\n (c) Orange \\n \\n\"\n", "]\n", "\n", "questions = [\n", " Question(question_prompts[0],\"a\"),\n", " Question(question_prompts[1],\"c\"),\n", " Question(question_prompts[2],\"b\"),\n", "]\n", "\n", "def run_test(questions):\n", " score = 0\n", " for question in questions:\n", " answer = input(question.prompt)\n", " if answer == question.answer:\n", " score += 1\n", " print(\"You got \" + str(score)+\" out of \" + str(len(questions)) + \" question right. \")\n", "\n", "run_test(questions)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HM1X81XD8rsl", "outputId": "b3f3b4d0-d6c0-4f07-f8fd-db5b66b26358" }, "execution_count": 24, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "What colors are apples? \n", "(a) Red /Green \n", " (b) Purple \n", " (c) Orange \n", " \n", "a\n", "What colors are bananas? \n", "(a) Teal \n", " (b) Magenta \n", " (c) Yellow \n", " \n", "b\n", "What colors are strawberries? \n", "(a) Yellow \n", " (b) Red \n", " (c) Orange \n", " \n", "c\n", "You got 1 out of 3 question right. \n" ] } ] }, { "cell_type": "code", "source": [ "import numpy as np\n", "class Student:\n", "\n", " def __init__(self, name, major, gpa):\n", " self.name = name\n", " self.major = major\n", " self.gpa = gpa\n", "\n", " def on_honor_roll(self):\n", " if self.gpa >= 3.5:\n", " return True\n", " else:\n", " return False\n", "\n", "\n", "\n", "student1 = Student(\"Oscar\",\"Accounting\",\"3.1\")\n", "print(student1.on_honor_roll())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 356 }, "id": "mARdoLzsC9F4", "outputId": "45e5006d-a683-4aec-b237-091c21c1ff05" }, "execution_count": 38, "outputs": [ { "output_type": "error", "ename": "AttributeError", "evalue": "ignored", "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 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mstudent1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mStudent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Oscar\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Accounting\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"3.1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstudent1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_honor_roll\u001b[0m\u001b[0;34m(\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[0;32m\u001b[0m in \u001b[0;36mon_honor_roll\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mon_honor_roll\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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[0;32m---> 10\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgpa\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m3.5\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 11\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32melse\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: 'Student' object has no attribute 'float'" ] } ] }, { "cell_type": "code", "source": [ "class Chef: \n", "\n", " def make_chicken(self):\n", " print(\"The chef makes a chicken\")\n", "\n", " def make_salad(self):\n", " print(\"The chef makes a salad\")\n", " \n", " def make_special_dish(self):\n", " print(\"The chef make a orange chicken\")\n", "\n", " def make_friend_rice(self):\n", " print(\"The chef makes fried rice\")\n", "\n", "class ChineseChef:\n", " def make_fried_chicken(self):\n", " print(\"The chef makes friend chicken\")\n", "\n", "myChef = Chef()\n", "myChef.make_special_dish()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UxObmHuKIG1D", "outputId": "f8953ecc-6c75-46e3-992d-405573f5e620" }, "execution_count": 45, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The chef make a orange chicken\n" ] } ] }, { "cell_type": "code", "source": [ "class ChineseChef(Chef):\n", " def make_fried_rice(self):\n", " print(\"The chef makes fried rice.\")\n", "\n", "ChineseChef.make_fried_rice()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "2AaDuRDmLlQN", "outputId": "0c696397-75ad-4eab-dc49-93d323135e99" }, "execution_count": 50, "outputs": [ { "output_type": "error", "ename": "NameError", "evalue": "ignored", "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[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The chef makes fried rice.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mChineseChef\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmake_fried_rice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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 'self' is not defined" ] } ] }, { "cell_type": "code", "source": [ "" ], "metadata": { "id": "hdMyXavvFw_-" }, "execution_count": null, "outputs": [] } ], "metadata": { "colab": { "name": "Python_tutorial.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }