1624 lines
40 KiB
Text
1624 lines
40 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "l0E95atrLpN8"
|
|
},
|
|
"source": [
|
|
"Prepared By Talha Tariq"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "z6wATfzDt_tU"
|
|
},
|
|
"source": [
|
|
"# Introduction to Data Science Lab 02"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "IeSpe8cot_tV"
|
|
},
|
|
"source": [
|
|
"## Table of Content\n",
|
|
"\n",
|
|
"* Revision and Tasks\n",
|
|
" * Introduction to Jupyter Notebook\n",
|
|
" * Introduction to Python\n",
|
|
" * Print\n",
|
|
" * User Input\n",
|
|
" * Variables and Data Types\n",
|
|
" * Conditional Statements\n",
|
|
" * Loops\n",
|
|
" * Functions\n",
|
|
" * Lists\n",
|
|
"* Dictionaries\n",
|
|
"* Tuples\n",
|
|
"* Set\n",
|
|
"* Lambda Function\n",
|
|
"* Class"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "SusYo9W1t_tW"
|
|
},
|
|
"source": [
|
|
"## Introduction"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "rnD27ifAt_tW"
|
|
},
|
|
"source": [
|
|
"Python is a great general-purpose programming language on its own, but with the help of a few popular libraries (numpy, scipy, matplotlib) it becomes a powerful environment for scientific computing. Python is a high-level, dynamically typed multiparadigm programming language. Python code is often said to be almost like pseudocode, since it allows you to express very powerful ideas in very few lines of code while being very readable."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Rysx_YE4t_tW"
|
|
},
|
|
"source": [
|
|
"## Print statement"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "cYb0pjh1L9eb",
|
|
"outputId": "861f0d33-643d-485d-d972-054d89df24aa"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"This is my first hands-on in Python :)\n",
|
|
"Second lab of IDS\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print('This is my first hands-on in Python :)')\n",
|
|
"print(\"Second lab of IDS\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "MZCOxrCut_tZ",
|
|
"outputId": "567696d1-2463-4d54-8f92-33d34d734eeb"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"This is my first hands-on in Python :)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print('This is my {} hands-on in {} :)'.format('first', 'Python'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Uw5QeapMt_ta"
|
|
},
|
|
"source": [
|
|
"## User Input"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "6sngtzq9t_ta",
|
|
"outputId": "76c54cb1-5b0c-402e-8656-4f507de73b2c"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Enter username:Talha Tariq\n",
|
|
"Username is: Talha Tariq\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"data=input\n",
|
|
"name = input(\"Enter username:\")\n",
|
|
"print(\"Username is: \" + name)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "bIAJ-PnAt_tb"
|
|
},
|
|
"source": [
|
|
"## Variables/Data types"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "SaTYAFdLt_tb"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"x = 1\n",
|
|
"y = 3.5\n",
|
|
"z = 'Programming'\n",
|
|
"w = True"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "gzYBxcYft_tb",
|
|
"outputId": "7eddb61e-1c28-46f0-b875-2ecc14ee4dc8"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1 3.5 Programming True\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(x, y, z, w)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "y-e_R-09t_tb",
|
|
"outputId": "a739700e-66c5-42bd-af8a-034b5bd1fc0a"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'int'> <class 'float'> <class 'str'> <class 'bool'>\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(type(x), type(y), type(z), type(w))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "k46_3Mumt_tc"
|
|
},
|
|
"source": [
|
|
"## Arithmetic Operators"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 86
|
|
},
|
|
"id": "sk_8DFcuL9ey",
|
|
"outputId": "dd60a271-3457-465d-e16a-41acf12a56ab"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2\n",
|
|
"0\n",
|
|
"2\n",
|
|
"0.5\n",
|
|
"9\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(x + 1) # Addition\n",
|
|
"print(x - 1) # Subtraction\n",
|
|
"print(x * 2) # Multiplication\n",
|
|
"print(x / 2) # Division\n",
|
|
"print(3 ** 2) # Exponentiation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 52
|
|
},
|
|
"id": "U4Jl8K0tL9e4",
|
|
"outputId": "07e3db14-3781-42b7-8ba6-042b3f9f72ba"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2\n",
|
|
"4\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"x += 1\n",
|
|
"print(x)\n",
|
|
"\n",
|
|
"x *= 2\n",
|
|
"print(x)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "q0fgNFhpt_tc",
|
|
"outputId": "712e2867-d3cb-4f8d-b0af-586e4c18d50b"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"10.0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import math\n",
|
|
"\n",
|
|
"x=100\n",
|
|
"y=math.sqrt(x)\n",
|
|
"print(y)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "YQgmQfOgL9fI"
|
|
},
|
|
"source": [
|
|
"## Boolean Operators"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "vvXqW0nUt_te",
|
|
"outputId": "f7169d0c-862c-4029-c225-a4dff1db6500"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"False\n",
|
|
"True\n",
|
|
"False\n",
|
|
"True\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"t, f = True, False\n",
|
|
"\n",
|
|
"print(t and f) # Logical AND\n",
|
|
"print(t or f) # Logical OR\n",
|
|
"print(not t) # Logical NOT\n",
|
|
"print(t != f) # Logical XOR"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "UQnQWFEyL9fP"
|
|
},
|
|
"source": [
|
|
"## Strings"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 34
|
|
},
|
|
"id": "AijEDtPFL9fP",
|
|
"outputId": "2a6b0cd7-58f1-43cf-e6b7-bf940d532549"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"hello 5\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"hello = 'hello' # String literals can use single quotes\n",
|
|
"world = \"world\" # or double quotes; it does not matter\n",
|
|
"\n",
|
|
"print(hello, len(hello)) #len=length"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 34
|
|
},
|
|
"id": "saDeaA7hL9fT",
|
|
"outputId": "2837d0ab-9ae5-4053-d087-bfa0af81c344"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"helloworld\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"hw = hello + world # String concatenation\n",
|
|
"print(hw)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 34
|
|
},
|
|
"id": "Nji1_UjYL9fY",
|
|
"outputId": "0149b0ca-425a-4a34-8e24-8dff7080922e"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"hello world 12\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"hw12 = '{} {} {}'.format(hello, world, 12) # string formatting\n",
|
|
"print(hw12)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "bUpl35bIL9fc"
|
|
},
|
|
"source": [
|
|
"String objects have a bunch of useful methods; for example:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 121
|
|
},
|
|
"id": "VOxGatlsL9fd",
|
|
"outputId": "ab009df3-8643-4d3e-f85f-a813b70db9cb"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Hello\n",
|
|
"HELLO\n",
|
|
" hello\n",
|
|
" hello \n",
|
|
"heDDDDo\n",
|
|
"world\n",
|
|
"$=234\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"s = \"hello\"\n",
|
|
"print(s.capitalize()) # Capitalize a string\n",
|
|
"print(s.upper()) # Convert a string to uppercase; prints \"HELLO\"\n",
|
|
"print(s.rjust(7)) # Right-justify a string, padding with spaces\n",
|
|
"print(s.center(7)) # Center a string, padding with spaces\n",
|
|
"print(s.replace('l', 'DD')) # Replace all instances of one substring with another\n",
|
|
"print(' world '.strip()) # Strip leading and trailing whitespace\n",
|
|
"print(\"$=\"+str(234)) # concatenate a string and a number"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "06cayXLtL9fi"
|
|
},
|
|
"source": [
|
|
"You can find a list of all string methods in the [documentation](https://docs.python.org/3.7/library/stdtypes.html#string-methods)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "kAhfuSUct_tj"
|
|
},
|
|
"source": [
|
|
"## Conditionals"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "U6DFNNnqt_tj",
|
|
"outputId": "203d5396-3c5f-4877-ab24-a0dc5a3d1a6f"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"positive\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"my_variable = 5\n",
|
|
"\n",
|
|
"if my_variable < 0:\n",
|
|
" print(\"negative\")\n",
|
|
" print(\" number\")\n",
|
|
"elif my_variable == 0:\n",
|
|
" print(\"null\")\n",
|
|
"else:\n",
|
|
" print(\"positive\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "aSZw6c0Qt_tm"
|
|
},
|
|
"source": [
|
|
"Here < and > are the strict less and greater than operators, while == is the equality operator (not to be confused with =, the variable assignment operator). The operators <= and >= can be used for less (resp. greater) than or equal comparisons."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "UONpMhF4L9f_"
|
|
},
|
|
"source": [
|
|
"## Loops"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "87taihG1t_tt"
|
|
},
|
|
"source": [
|
|
"Loops are a way to execute a block of code multiple times. There are two main types of loops: while loops and for loops."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "0281aIHat_tt",
|
|
"outputId": "059159c8-e55d-4655-bb12-631400df578d"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0\n",
|
|
"1\n",
|
|
"2\n",
|
|
"3\n",
|
|
"4\n",
|
|
"5\n",
|
|
"6\n",
|
|
"7\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"i = 0\n",
|
|
"while i <= len('Program'):\n",
|
|
" print(i)\n",
|
|
" i += 1 # equivalent to i = i + 1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 69
|
|
},
|
|
"id": "4cCOysfWL9gA",
|
|
"outputId": "560e46c7-279c-409a-838c-64bea8d321c4"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2\n",
|
|
"3\n",
|
|
"4\n",
|
|
"5\n",
|
|
"6\n",
|
|
"7\n",
|
|
"8\n",
|
|
"9\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for p in range(2,10):\n",
|
|
" print(p)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "RZaABIklt_tt",
|
|
"outputId": "2a048093-7f16-4736-c975-363f45fbb6c4"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"a\n",
|
|
"a\n",
|
|
"a\n",
|
|
"a\n",
|
|
"a\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for x in \"bananarama\":\n",
|
|
" if x=='a':\n",
|
|
" print(x)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "EelU-1rft_tt"
|
|
},
|
|
"source": [
|
|
"# Exercise\n",
|
|
"## Online Store Shopping Cart\n",
|
|
"Write a Python program to simulate the online store's shopping cart system. The program should use a while loop to provide an interactive shopping experience.\n",
|
|
"\n",
|
|
"\n",
|
|
"### Tasks:\n",
|
|
"* Initialize an empty list called shopping_cart to store items.\n",
|
|
"* Display a welcome message and available products.\n",
|
|
"* Loop should be iterating over AI Response Simulator and Investment decision and keep asking for user input.\n",
|
|
"* Prompt the user to enter the number corresponding to the product they want to add to the cart.\n",
|
|
"* Check if the user entered 'q'. If so, break the loop.\n",
|
|
"* Convert the choice to an integer and validate it within the valid range (1 to 3).\n",
|
|
"* Based on the user's choice, add the selected product to the shopping_cart list.\n",
|
|
"* Display a confirmation message indicating the added product.\n",
|
|
"* Continue the loop to allow the user to add more products or proceed to checkout.\n",
|
|
"* After the loop ends, display the contents of the shopping cart.\n",
|
|
"* Display a goodbye message.\n",
|
|
"* End the program.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"id": "uaWA6X7Mt_tu"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"welcome to the store\n",
|
|
"1 . AI response Simulator\n",
|
|
"2 . Investment decison\n",
|
|
"3 . random thing\n",
|
|
"1 . AI response Simulator\n",
|
|
"2 . Investment decison\n",
|
|
"3 . random thing\n",
|
|
"your shopping cart\n",
|
|
"AI response Simulator\n",
|
|
"goodbye\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Write your code here\n",
|
|
"shopping_cart = []\n",
|
|
"items = ['AI response Simulator', 'Investment decison', 'random thing']\n",
|
|
"\n",
|
|
"print('welcome to the store')\n",
|
|
"loop = True\n",
|
|
"\n",
|
|
"while loop:\n",
|
|
" i = 1\n",
|
|
" for item in items:\n",
|
|
" print(i,'. ',item)\n",
|
|
" i+=1\n",
|
|
" index = input('enter a number to add to shopping cart')\n",
|
|
"\n",
|
|
" if index == 'q':\n",
|
|
" loop = False\n",
|
|
" break\n",
|
|
" index = int(index)\n",
|
|
" if index < 1 or index > 3:\n",
|
|
" print('invalid index, try again')\n",
|
|
" continue\n",
|
|
"\n",
|
|
" shopping_cart.append(items[index-1])\n",
|
|
"\n",
|
|
"print('your shopping cart')\n",
|
|
"for item in shopping_cart:\n",
|
|
" print(item)\n",
|
|
"print('goodbye')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "AXA4jrEOL9hM"
|
|
},
|
|
"source": [
|
|
"## Functions"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "WaRms-QfL9hN"
|
|
},
|
|
"source": [
|
|
"Python functions are defined using the `def` keyword. For example:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "kiMDUr58L9hN"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def sign(x):\n",
|
|
" if x > 0:\n",
|
|
" return 'positive'\n",
|
|
" elif x < 0:\n",
|
|
" return 'negative'\n",
|
|
" else:\n",
|
|
" return 'zero'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "k2uRgLt1t_tu",
|
|
"outputId": "86620e7a-8e9e-40ee-d04a-5df715e94879"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"negative\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(sign(-5))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "U-QJFt8TL9hR"
|
|
},
|
|
"source": [
|
|
"We will often define functions to take optional keyword arguments, like this:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "PfsZ3DazL9hR",
|
|
"outputId": "34fef4c8-2731-4ce6-f3cd-6ab52bcb75af"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Hello, Talha!\n",
|
|
"HELLO, TALHA!\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def hello(name, loud=False):\n",
|
|
" if loud:\n",
|
|
" print('HELLO, {}'.format(name.upper()))\n",
|
|
" else:\n",
|
|
" print('Hello, {}!'.format(name))\n",
|
|
"\n",
|
|
"hello('Talha')\n",
|
|
"hello('Talha!', loud=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "MLoP2sXQt_tv",
|
|
"outputId": "b7338b6d-f9c3-4545-bcd3-6392d4d7eb5b"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"36"
|
|
]
|
|
},
|
|
"execution_count": 94,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"def square(x):\n",
|
|
" return x ** 2\n",
|
|
"\n",
|
|
"def multiply(a, b):\n",
|
|
" return a * b\n",
|
|
"\n",
|
|
"# Functions can be composed.\n",
|
|
"square(multiply(3, 2))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "wj-ueQGzt_tv",
|
|
"outputId": "644cecee-971a-491d-d89c-76e6e55bca5b"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" (25, 625)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def multi_values(x):\n",
|
|
" y=x ** 2\n",
|
|
" z=y ** 2\n",
|
|
"\n",
|
|
" return y,z\n",
|
|
"\n",
|
|
"b=multi_values(5)\n",
|
|
"print(' {}'.format(b))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "apoQnvIqt_tw"
|
|
},
|
|
"source": [
|
|
"## Lists\n",
|
|
"A list is a collection of values enclosed in square brackets []. It can hold elements of different types.\n",
|
|
"\n",
|
|
"### List Characteristics:\n",
|
|
"\n",
|
|
"* Lists are ordered collections.\n",
|
|
"* Lists are mutable (can be changed after creation).\n",
|
|
"* Lists can contain duplicate elements."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "7SPioMZ-t_tw",
|
|
"outputId": "daa550d7-3a8f-4668-8152-08c6d085c54d"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"['apple', 'banana']"
|
|
]
|
|
},
|
|
"execution_count": 139,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"my_list = [1, 2, 3, 'apple', 'banana'] # creating a list\n",
|
|
"\n",
|
|
"my_list #print a list\n",
|
|
"my_list[3] # print values from index 3 to 4 (4 exclusive)\n",
|
|
"my_list[2:5] # print values from index 2 to 5 (5 exclusive)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "d0NX9A9Gt_tw",
|
|
"outputId": "b7eae649-f493-46c1-d353-9c73f267e9a8"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1\n",
|
|
"2\n",
|
|
"3\n",
|
|
"apple\n",
|
|
"banana\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for i in my_list: # iterate list by loop\n",
|
|
" print(i)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "zgmgR91rt_tw",
|
|
"outputId": "248e79f0-b71c-4b21-e64c-3ad06746ca9d"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[1, 4, 9, 16, 25]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"numbers = [1, 2, 3, 4, 5]\n",
|
|
"squared = [x**2 for x in numbers]\n",
|
|
"print(squared) # Output: [1, 4, 9, 16, 25]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "du2lL74Gt_tx",
|
|
"outputId": "84da410a-2478-447b-b332-ddf193044330"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[9, 6, 5, 4, 3, 2, 1, 1]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"numbers = [3, 1, 4, 1, 5, 9, 2, 6]\n",
|
|
"numbers.sort()\n",
|
|
"numbers.reverse()\n",
|
|
"print(numbers)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "dT8ZbYSWt_tx"
|
|
},
|
|
"source": [
|
|
"Further operations are here:\n",
|
|
"\n",
|
|
"* append() : Used for appending and adding elements to the end of the List.\n",
|
|
"\n",
|
|
"* copy() : It returns a shallow copy of a list. \n",
|
|
"\n",
|
|
"* clear() : This method is used for removing all items from the list. \n",
|
|
"\n",
|
|
"* count() : These methods count the elements. \n",
|
|
"\n",
|
|
"* extend() : Adds each element of the iterable to the end of the List. \n",
|
|
"\n",
|
|
"* index() : Returns the lowest index where the element appears. \n",
|
|
"\n",
|
|
"* insert() : Inserts a given element at a given index in a list. \n",
|
|
"\n",
|
|
"* pop() : Removes and returns the last value from the List or the given index value.\n",
|
|
"\n",
|
|
"* remove() : Removes a given object from the List. \n",
|
|
"\n",
|
|
"* reverse() : Reverses objects of the List in place. \n",
|
|
"\n",
|
|
"* sort() : Sort a List in ascending, descending, or user-defined order.\n",
|
|
"\n",
|
|
"* min() : Calculates the minimum of all the elements of the List. \n",
|
|
"\n",
|
|
"* max() : Calculates the maximum of all the elements of the List. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "kjXGGatYIMJI",
|
|
"outputId": "2040b4b6-cb92-4922-a707-c4992346206d"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"array('i', [2, 5, 7, 4, 5, 1, 11, 2, 5])\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Array\n",
|
|
"import array as arr\n",
|
|
"data_array = arr.array(\"i\", [2,5,7,4,5,1,11,2,5])\n",
|
|
"print(data_array)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "hduhQq7mIOGR",
|
|
"outputId": "e38ad26e-f636-4d2f-9917-021202edbcb0"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{1, 2, 4, 5, 7, 11}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Set\n",
|
|
"# Does not allow duplicate values.\n",
|
|
"data_set = {2,5,7,4,5,1,11}\n",
|
|
"print(data_set)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "tJD2HDKCIQI7",
|
|
"outputId": "0337c92b-fad7-483b-ac06-8d6f9989647b"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"('apple', 'banana', 'cherry')\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Tuple\n",
|
|
"# Tuple items are ordered, unchangeable, and allow duplicate values.\n",
|
|
"data_tuple = (\"apple\", \"banana\", \"cherry\")\n",
|
|
"print(data_tuple)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "axQ4OAJ_t_tx"
|
|
},
|
|
"source": [
|
|
"# Dictionaries\n",
|
|
"Dictionaries allow you to associate values with unique keys, enabling efficient data retrieval and manipulation. Dictionaries are unordered collections of key-value pairs. Each key is unique and associated with a value. Dictionaries are created using curly braces {} and key-value pairs separated by colons :."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Yy7_C-0qt_ty"
|
|
},
|
|
"source": [
|
|
"## Creating and Accessing Dictionaries"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "sZaAaG-Zt_tz"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Creating a dictionary\n",
|
|
"student = {\n",
|
|
" \"name\": \"John\",\n",
|
|
" \"age\": 20,\n",
|
|
" \"major\": \"Computer Science\"\n",
|
|
"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "8z9eAbt5t_tz",
|
|
"outputId": "a534f67e-502e-4d02-e037-8a13d52d02a5"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"John\n",
|
|
"20\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(student[\"name\"])\n",
|
|
"print(student[\"age\"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "MJbPDTUnt_tz",
|
|
"outputId": "5318a980-109c-4733-aea0-9852cf541cab"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'name': 'John', 'age': 20, 'major': 'Computer Science'}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(student)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "PJGKBMqmt_t0"
|
|
},
|
|
"source": [
|
|
"### Manipulating Dictionaries\n",
|
|
"#### Modifying Values:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "s-xWE6D5t_t1",
|
|
"outputId": "b7471b2a-55c5-4abf-b40e-3646875ee02a"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"26\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"student[\"age\"] = 26\n",
|
|
"print(student[\"age\"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "zxNZ1sk9t_t1"
|
|
},
|
|
"source": [
|
|
"#### Adding New Key-Value Pairs:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "UsMucR5ut_t1",
|
|
"outputId": "7d4b9a69-e59b-40ef-8c34-7e11b0b9f95d"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'name': 'John', 'age': 26, 'major': 'Computer Science', 'city': 'New York'}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"student[\"city\"] = \"New York\"\n",
|
|
"print(student)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "0ZQlR2Kat_t1"
|
|
},
|
|
"source": [
|
|
"#### Deleting Key-Value Pairs:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "h99OrEmmt_t1",
|
|
"outputId": "00bee6dc-189e-4818-bc27-352c3fd3eea1"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'name': 'John', 'major': 'Computer Science', 'city': 'New York'}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"del student[\"age\"]\n",
|
|
"print(student)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "yAkZik3Ht_t2",
|
|
"outputId": "d8203db3-fe2f-4ccf-bdab-e757e09cc180"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'New York'"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"student.pop(\"city\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "6RgEieyGt_t2",
|
|
"outputId": "400dc240-d1a9-468a-9d56-35f8356ab247"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'name': 'John', 'major': 'Computer Science'}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(student)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "-jI_fO6Mt_t2"
|
|
},
|
|
"source": [
|
|
"#### Dictionary Methods:\n",
|
|
"* keys(): Returns a list of all keys in the dictionary.\n",
|
|
"* values(): Returns a list of all values in the dictionary.\n",
|
|
"* items(): Returns a list of tuples, each containing a key-value pair."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "ouj3J5AVt_t2",
|
|
"outputId": "0fe355ac-4de0-4cb2-bd87-7c8f620a7786"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"dict_keys(['name', 'major'])\n",
|
|
"dict_values(['John', 'Computer Science'])\n",
|
|
"dict_items([('name', 'John'), ('major', 'Computer Science')])\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(student.keys())\n",
|
|
"print(student.values())\n",
|
|
"print(student.items())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "UUjkefO3t_t3",
|
|
"outputId": "a690a8f6-6368-4df4-c084-77a0d6211e5a"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"machanic\n",
|
|
"electrician\n",
|
|
"True\n",
|
|
"23.5\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"tools={\"machanic\":[\"Hammer\", \"Screwdriver\", \"etc\"],\n",
|
|
" \"electrician\": [\"tester\",\"wires\", \"holders\"],\n",
|
|
" True:False,\n",
|
|
" 23.5:\"b\"}\n",
|
|
"for m in tools:\n",
|
|
" print(m)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "7yEkvUo_t_t3",
|
|
"outputId": "0ad044e6-8b81-4044-abe3-94353d48cc17"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"['Hammer', 'Screwdriver', 'etc']\n",
|
|
"['tester', 'wires', 'holders']\n",
|
|
"False\n",
|
|
"b\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for m in tools:\n",
|
|
" print(tools[m])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "c-d8D-Jjt_t4"
|
|
},
|
|
"source": [
|
|
"## English-Spanish Dictionary\n",
|
|
"You are creating a basic English-Spanish dictionary that allows users to look up English words and find their Spanish translations.\n",
|
|
"\n",
|
|
"\n",
|
|
"### Tasks\n",
|
|
"* Create a dictionary where English words are keys and their Spanish translations are values.\n",
|
|
"* Ask the user to input an English word.\n",
|
|
"* Check if the word exists in the dictionary.\n",
|
|
"* If the word exists, print its Spanish translation.\n",
|
|
"* If the word does not exist, inform the user that the translation is not available.\n",
|
|
"* Implement a loop that allows the user to keep looking up words until they choose to exit.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"id": "rsDrUyMPt_t4"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"translation not available\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"dict = {\n",
|
|
" \"Hi\" : \"hola\",\n",
|
|
" \"Water\" : \"Agua\",\n",
|
|
" \"Fire\" : \"Fuego\"\n",
|
|
"}\n",
|
|
"while True:\n",
|
|
" word = input(\"enter a word in english or type q to exit\")\n",
|
|
" if word == 'q':\n",
|
|
" break\n",
|
|
" spanish_word = dict.get(word)\n",
|
|
" if spanish_word is None:\n",
|
|
" print('translation not available')\n",
|
|
" else:\n",
|
|
" print('the word in spanish is', spanish_word)\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "UstEegygDmF_"
|
|
},
|
|
"source": [
|
|
"#Lambda Function\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Tc4btWjCIj8L"
|
|
},
|
|
"source": [
|
|
"A lambda function is a small anonymous function.\n",
|
|
"\n",
|
|
"A lambda function can take any number of arguments, but can only have one expression."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "uswh8PsxInuC",
|
|
"outputId": "0dac0f36-fb14-4a94-ec2c-9e8957727f08"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"15"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# simple function\n",
|
|
"def add(a):\n",
|
|
" return a + 10\n",
|
|
"\n",
|
|
"add(5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "Le20eRKODk2t",
|
|
"outputId": "30f74cdb-90b2-468b-cd9d-8115747f2180"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"30"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"x = lambda a, b : a * b\n",
|
|
"x(5, 6)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "uPl67FMyC0S5"
|
|
},
|
|
"source": [
|
|
"## Online Store Discounts\n",
|
|
"\n",
|
|
"You're building an online store that offers discounts based on membership levels. Write a lambda function that calculates the discounted price for an item based on the user's membership level. The discount percentages are as follows:\n",
|
|
"\n",
|
|
"* Regular: 10% off\n",
|
|
"* Premium: 20% off\n",
|
|
"* VIP: 30% off\n",
|
|
"\n",
|
|
"Use the lambda function to calculate the final price of an item with a given price and membership level."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"id": "_NSwznSJLQPD"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"your actual price 2675\n",
|
|
"your discount 0.1\n",
|
|
"your final price 2407.5\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"membership_level = input('enter your membership level, Regular, Premium or VIP\\n')\n",
|
|
"dict = {\n",
|
|
" \"Regular\" : 0.1,\n",
|
|
" \"Premium\" : 0.2,\n",
|
|
" \"VIP\" : 0.3\n",
|
|
"}\n",
|
|
"price = 2675\n",
|
|
"disc = dict.get(membership_level)\n",
|
|
"getdiscount = lambda price, disc : price - (price*disc)\n",
|
|
"\n",
|
|
"print('your actual price ', price)\n",
|
|
"print('your discount ', disc)\n",
|
|
"print('your final price ', getdiscount(price, disc))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "aIrawkI4J4n4"
|
|
},
|
|
"source": [
|
|
"# Happy Coding :)\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"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.12.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|