0% found this document useful (0 votes)
34 views18 pages

Xs 1 Python 1 Basics - Ipynb

Uploaded by

bebolina09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views18 pages

Xs 1 Python 1 Basics - Ipynb

Uploaded by

bebolina09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

{

"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"Python Basics"
],
"metadata": {
"id": "gTjIYGhhYLXk"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3HjP9q31YGHk",
"outputId": "f26e8158-2715-4e38-b691-222ecbefee07"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Hello, World!\n"
]
}
],
"source": [
"# It was created by Guido van Rossum, and released in 1991\n",
"print(\"Hello, World!\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"source": [
"# Indentation"
],
"metadata": {
"id": "p_3Iq1ugZKWa"
}
},
{
"cell_type": "code",
"source": [
"if 5 > 2:\n",
" print(\"Five is greater than two!\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZQiLZGmkZHLg",
"outputId": "99c763e2-98de-4e05-bd7a-a53b4e6c0353"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Five is greater than two!\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Variable creation / Type"
],
"metadata": {
"id": "fwDD6bzXZUkF"
}
},
{
"cell_type": "code",
"source": [
"_x = 5\n",
"_X = \"Hello\" # case-sensitive\n",
"print(_x)\n",
"print(_X)\n",
"print(type(_x),type(_X))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PWGWo8FjZXi9",
"outputId": "01d27b03-8618-4017-a66d-b0f22b463e52"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"5\n",
"Hello\n",
"<class 'int'> <class 'str'>\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Type Casting\n"
],
"metadata": {
"id": "ih4pg0eZZbVi"
}
},
{
"cell_type": "code",
"source": [
"x = str(10) # x will be '10'\n",
"y = int(10) # y will be 10\n",
"z = float(10) # z will be 10.0\n",
"print(x, end=\" \")\n",
"print(y, z, sep=\",\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Bl46qmeNZmgx",
"outputId": "b1d7b4d7-555b-48df-87e8-1c1cd8c893a1"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"10 10,10.0\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Variable: Global vs Local\n"
],
"metadata": {
"id": "lREg3DWWaAN-"
}
},
{
"cell_type": "code",
"source": [
"x = \"awesome\"\n",
"def myfunc():\n",
" x = \"fantastic\"\n",
" print(\"Python is \" + x)\n",
"myfunc()\n",
"print(\"Python is \" + x)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ERgVqB16aCGP",
"outputId": "f90c8dae-76d8-4f49-e05b-d48edbd211c1"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Python is fantastic\n",
"Python is awesome\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Variable assignments\n"
],
"metadata": {
"id": "nS5g_4EbaDfe"
}
},
{
"cell_type": "code",
"source": [
"Fruits = [\"Apple\", \"Banana\", \"Cherry\"]\n",
"x, y, z = Fruits\n",
"#x, y, z = \"Orange\", \"Banana\", \"Cherry\"\n",
"#x=y=z = \"Orange\"\n",
"print(f\"x={x}, y={y}, z={z}\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cvU_GcU-aLw_",
"outputId": "836a3118-8861-4988-9d33-da911e32ab9e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"x=Apple, y=Banana, z=Cherry\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Python Numbers\n"
],
"metadata": {
"id": "oZz1IhriaNIj"
}
},
{
"cell_type": "code",
"source": [
"x = 1 # int\n",
"y = 2.8 # float\n",
"z = 1j # complex\n",
"a = float(x) #convert from int to float:\n",
"b = int(y) #convert from float to int:\n",
"c = complex(x) #convert from int to complex:\n",
"print(f\"a={a}, b={b}, c={c}\")\n",
"import random\n",
"print(random.randrange(1, 100))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-N7h0r6baUAX",
"outputId": "dca65e25-596f-45fa-cc10-cbf2894c237e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"a=1.0, b=2, c=(1+0j)\n",
"8\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"#Strings\n",
"\n"
],
"metadata": {
"id": "p4TB7debaXA2"
}
},
{
"cell_type": "code",
"source": [
"a = \"Hello, World!\"\n",
"print(a)\n",
"print(a[1])\n",
"print(len(a))\n",
"\n",
"# characters by using the slice syntax\n",
"print(a[2:5])\n",
"print(a[2:])\n",
"print(a[:5])\n",
"print(a[-5:-2])\n",
"print(a.upper()) # uppercase\n",
"print(a.strip()) # removes any space at beginning,\n",
"print(a.replace(\"H\", \"J\"))\n",
"print(a.split(\",\")) # returns ['Hello', ' World!’]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GrubIEXiaWHT",
"outputId": "72495c46-6607-4dbb-f56b-ed7a82981cbb"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Hello, World!\n",
"e\n",
"13\n",
"llo\n",
"llo, World!\n",
"Hello\n",
"orl\n",
"HELLO, WORLD!\n",
"Hello, World!\n",
"Jello, World!\n",
"['Hello', ' World!']\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Convert into titlecase"
],
"metadata": {
"id": "IsmYpF7pfQsW"
}
},
{
"cell_type": "code",
"source": [
"string1 = \"Welcome to Techvein!\"\n",
"\n",
"x = string1.title()\n",
"\n",
"print(x)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6JJtBxuNdcv9",
"outputId": "aa2572f5-8c19-41d4-bcac-110241cf3d41"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Welcome To Techvein!\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "1PhWAC6ZaYSR"
}
},
{
"cell_type": "code",
"source": [
"# Operators\n",
"for x in range(10,1,-1):\n",
" print(x)\n",
"\n",
"for x in \"banana\":\n",
" print(x, end=\"\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8H2JLSvTaY3i",
"outputId": "a2b61716-9db5-4a8e-cd8b-accdb23faea0"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"banana"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"#Collection Data Types"
],
"metadata": {
"id": "Tz00zQunbs_B"
}
},
{
"cell_type": "code",
"source": [
"list_1 = [\"apple\", \"banana\", \"apple\", 34, True, 40, False, 34]\n",
"tuple_1 = (\"apple\", \"banana\", \"apple\", 34, True, 40, False, 34)\n",
"set_1 = {\"apple\", \"banana\", \"apple\", 34, True, 40, False, 34}\n",
"\n",
"# order and repeated values\n",
"print(list_1)\n",
"print(tuple_1)\n",
"print(set_1) #No order & no duplicates\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "n_ehc1Ueb5wB",
"outputId": "b8e07d14-0038-4b55-c689-f66661db435d"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['apple', 'banana', 'apple', 34, True, 40, False, 34]\n",
"('apple', 'banana', 'apple', 34, True, 40, False, 34)\n",
"{False, True, 34, 'apple', 40, 'banana'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "R-mZka_eJfXe"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Access by index\n",
"print(list_1[0])\n",
"print(tuple_1[0])\n",
"#print(set_1[0]) #TypeError: 'set' object is not subscriptable"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "r7Ds3PG4cAdr",
"outputId": "7ed95953-7d21-41d2-8139-3b41661dd8b3"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"apple\n",
"apple\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Mutability"
],
"metadata": {
"id": "bP3k_SxMgsXR"
}
},
{
"cell_type": "code",
"source": [
"list_1 = [\"apple\", \"banana\", \"apple\", 34, True, 40, False, 34]\n",
"tuple_1 = (\"apple\", \"banana\", \"apple\", 34, True, 40, False, 34)\n",
"set_1 = {\"apple\", \"banana\", \"apple\", 34, True, 40, False, 34}\n",
"\n",
"# Mutability\n",
"set_1.add(\"blackcurrant\")\n",
"set_1.remove(\"banana\")\n",
"print(set_1)\n",
"\n",
"list_1.append(\"orange\")\n",
"list_1.remove(\"apple\")\n",
"list_1[1] = \"blackcurrant\"\n",
"print(list_1)\n",
"\n",
"#tuple_1[1] = \"blackcurrant\" #TypeError: 'tuple' object does not support
item assignment\n",
"#set_1[1] = \"blackcurrant\" #TypeError: 'set' object does not support
item assignment\n",
"#print(tuple_1.count(34))\n",
"#print(tuple_1.index(34))\n",
"\n",
"#list_1.reverse()\n",
"#print(\"Reversed list =\", list_1)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tDmqjSayfj-Z",
"outputId": "c1a6a158-a95a-4d31-ed6a-1317b9a722b2"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{False, True, 34, 'apple', 'blackcurrant', 40}\n",
"['banana', 'blackcurrant', 34, True, 40, False, 34, 'orange']\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Examples of List Methods in Python\n",
"1. append() - Adds an element at the end of the list\n",
"2. insert() - Inserts an element at a specified index\n",
"3. extend() - Adds elements from another list\n",
"4. remove() - Removes the first occurrence of a specified value\n",
"5. pop() - Removes and returns the last item (or by index)\n",
"6. index() - Returns the index of the first occurrence of a value\n",
"7. count() - Returns the number of times a value appears\n",
"8. sort() - Sorts the list in ascending order\n",
"9. reverse() - Reverses the elements of the list\n",
"10. clear() - Removes all elements from the list"
],
"metadata": {
"id": "maMhJxiB8VT0"
}
},
{
"cell_type": "code",
"source": [
"\n",
"# Create a list and do all the list method\n",
"my_list = [10, 20, 30, 40, 50]\n",
"\n",
"# 1. append() - Adds an element at the end of the list\n",
"my_list.append(60)\n",
"print(\"After append:\", my_list) # [10, 20, 30, 40, 50, 60]\n",
"\n",
"# 2. insert() - Inserts an element at a specified index\n",
"my_list.insert(2, 25)\n",
"print(\"After insert:\", my_list) # [10, 20, 25, 30, 40, 50, 60]\n",
"\n",
"# 3. extend() - Adds elements from another list\n",
"my_list.extend([70, 80])\n",
"print(\"After extend:\", my_list) # [10, 20, 25, 30, 40, 50, 60, 70, 80]\
n",
"\n",
"# 4. remove() - Removes the first occurrence of a specified value\n",
"my_list.remove(30)\n",
"print(\"After remove:\", my_list) # [10, 20, 25, 40, 50, 60, 70, 80]\n",
"\n",
"# 5. pop() - Removes and returns the last item (or by index)\n",
"last_item = my_list.pop()\n",
"print(\"Popped item:\", last_item)\n",
"print(\"After pop:\", my_list)\n",
"\n",
"# 6. index() - Returns the index of the first occurrence of a value\n",
"index_40 = my_list.index(40)\n",
"print(\"Index of 40:\", index_40)\n",
"\n",
"# 7. count() - Returns the number of times a value appears\n",
"my_list.append(20)\n",
"count_20 = my_list.count(20)\n",
"print(\"Count of 20:\", count_20)\n",
"\n",
"# 8. sort() - Sorts the list in ascending order\n",
"my_list.sort()\n",
"print(\"After sort:\", my_list)\n",
"\n",
"# 9. reverse() - Reverses the elements of the list\n",
"my_list.reverse()\n",
"print(\"After reverse:\", my_list)\n",
"\n",
"# 10. clear() - Removes all elements from the list\n",
"my_list.clear()\n",
"print(\"After clear:\", my_list) # []"
],
"metadata": {
"id": "jb-vAH4r8Vs1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Dictionary"
],
"metadata": {
"id": "7AweRJFahqKb"
}
},
{
"cell_type": "code",
"source": [
"dict = {\"brand\": \"St. Xavier's\",\"model\": \"Panihati\",\"estd_year\":
1967,\"affiliation\": \"CISCE\"}\n",
"print(dict.get(\"brand\"))\n",
"dict[\"age\"] = 19 #Modify\n",
"dict[\"job\"] = \"Data Engineer\" #Add\n",
"print(dict)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7OFNJGOGfv0f",
"outputId": "4b1f69d0-8d10-42ca-db80-57e52cc57b7a"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"St. Xavier's\n",
"{'brand': \"St. Xavier's\", 'model': 'Panihati', 'estd_year': 1967,
'affiliation': 'CISCE', 'age': 19, 'job': 'Data Engineer'}\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Control Flow Statements"
],
"metadata": {
"id": "UBV3H6NCho5x"
}
},
{
"cell_type": "code",
"source": [
"# Break example\n",
"for num in range(1, 10):\n",
" if num == 5:\n",
" break # Exit the loop when num equals 5\n",
" print(num)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "V9yDOsO1hoV7",
"outputId": "8ab6f9a2-47ac-4f23-a76a-908a3f234838"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"4\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Continue example\n",
"for num in range(1, 10):\n",
" if num == 5:\n",
" continue # Skip the iteration when num equals 5\n",
" print(num)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "d_Gzccd-h-Ru",
"outputId": "10a499b5-5b59-4f8e-c539-0ee870f06030"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Pass example\n",
"for num in range(1, 5):\n",
" if num == 3:\n",
" pass # Do nothing when num equals 3\n",
" else:\n",
" print(num)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yaUPbBlih-rf",
"outputId": "23ba3314-bd99-48e5-bccb-53c2fc9d5465"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"2\n",
"4\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Functions"
],
"metadata": {
"id": "qZnXy5_RiazN"
}
},
{
"cell_type": "code",
"source": [
"def greet(name):\n",
" print(f\"Hello, {name}!\")\n",
"\n",
"greet('Class 9')\n",
"\n",
"\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PSq6dxNAibSI",
"outputId": "75f75d9a-a51f-4ee6-de16-ce37be4a7577"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Hello, Class 9!\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def add(a,b):\n",
" # print(a+b)\n",
" return a+b\n",
"add(10,5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7mKaUwJfLyi8",
"outputId": "8d976612-0924-4db1-92e5-df5169854d03"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"15"
]
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "code",
"source": [
"def greet(name, msg):\n",
" print(f\"{msg}, {name}\")\n",
"\n",
"greet(name=\"Class 9\",msg=\"Welcome\") # Keyword arguments\n",
"greet( \"Class 9\", \"Welcome\") # positional arguments\n",
"#greet()\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 211
},
"id": "80eXsuVajfm3",
"outputId": "9ac22a97-318b-46b8-94e3-bcd7122ee5a5"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Welcome, Class 9\n",
"Welcome, Class 9\n"
]
},
{
"output_type": "error",
"ename": "TypeError",
"evalue": "greet() missing 2 required positional arguments: 'name' and
'msg'",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipython-input-4-1769888825.py\u001b[0m in \
u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \
u001b[0mgreet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m=\
u001b[0m\u001b[0;34m\"Class 9\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mmsg\
u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Welcome\"\u001b[0m\u001b[0;34m)\
u001b[0m \u001b[0;31m# Keyword arguments\u001b[0m\u001b[0;34m\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mgreet\
u001b[0m\u001b[0;34m(\u001b[0m \u001b[0;34m\"Class 9\"\u001b[0m\u001b[0;34m,\
u001b[0m \u001b[0;34m\"Welcome\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#
positional arguments\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\
u001b[0;32m----> 6\u001b[0;31m \u001b[0mgreet\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;31mTypeError\u001b[0m: greet() missing 2 required positional
arguments: 'name' and 'msg'"
]
}
]
},
{
"cell_type": "code",
"source": [
"def greet(name=\"Guest\"):\n",
" print(f\"Hello, {name}!\")\n",
"greet(\"Alice\") # Positional\n",
"greet() # Default\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "i34x58KMjhQe",
"outputId": "5b9b0388-f295-46f2-c781-84fb99fc3a71"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Hello, Alice!\n",
"Hello, Guest!\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Arbitrary Arguments: *args; number of arguments is unknown, List\n",
"def my_f(*kids):\n",
" print(\"The youngest child is \" + kids[2])\n",
"\n",
"my_f(\"Amar\", \"Akbar\", \"Anthony\")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hZBaJzH2jl6I",
"outputId": "e1aea195-2735-41b6-9b10-ac3267d4139c"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The youngest child is Anthony\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Arbitrary Keyword Arguments: *kargs; number of arguments is unknown,
dictionary\n",
"def my_f(**kid):\n",
" print(\"Your last name is \" + kid[\"k2\"])\n",
"\n",
"my_f(k1 = \"Saptarshi\", k2 = \"Jana\")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1UZXhZRFjnem",
"outputId": "654cab51-68a0-4db1-f2f5-7c9a14b2ebd7"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Your last name is Jana\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Write a program using a function to calculate the simple interest using
the formula “Simple Interest = PRT/100”.\n",
"\n"
],
"metadata": {
"id": "GEx6E3sU_hrS"
}
},
{
"cell_type": "code",
"source": [
"def calculate_simple_interest(principal, int_rate, time):\n",
" return (principal * int_rate * time) / 100\n",
"\n",
"# Call the function\n",
"print(calculate_simple_interest(1000, 5, 2))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-h-7Is3aT_Io",
"outputId": "a31f6165-7d33-41d8-b67d-a590ebdc4dd2"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"100.0\n"
]
}
]
}
]
}

You might also like