Skip to content

Instantly share code, notes, and snippets.

@flyfir248
Created September 24, 2023 08:40
Show Gist options
  • Save flyfir248/3583a3a537032b6206a842638ddb81c7 to your computer and use it in GitHub Desktop.
Save flyfir248/3583a3a537032b6206a842638ddb81c7 to your computer and use it in GitHub Desktop.
Day4_17_09_2023.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/flyfir248/3583a3a537032b6206a842638ddb81c7/day4_17_09_2023.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"id": "822e5faa",
"metadata": {
"id": "822e5faa"
},
"source": [
"### Agenda:\n",
"- Recap\n",
"- Strings\n",
" - Indexing\n",
" - Slicing\n",
" - Negative indexing\n",
" - String functions\n",
"- Typecasting\n",
"- Lists"
]
},
{
"cell_type": "markdown",
"id": "8145b335",
"metadata": {
"id": "8145b335"
},
"source": [
"### Recap\n",
"- Assignment operators\n",
"- Logical operators\n",
"- Bitwise operators\n",
"- Membership operators\n",
"- String definetion\n",
"- String indexing\n",
"- String slicing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77201567",
"metadata": {
"id": "77201567"
},
"outputs": [],
"source": [
"# '' and \"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9de60cb",
"metadata": {
"id": "d9de60cb"
},
"outputs": [],
"source": [
"st1 = 'Python is easy to understand'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e757c40",
"metadata": {
"id": "5e757c40"
},
"outputs": [],
"source": [
"st2 = \"Python is 'easy' to understand\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "463742d7",
"metadata": {
"id": "463742d7"
},
"outputs": [],
"source": [
"st3 = 'In english there is a proverb , \"Work is worship\".'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea26ba63",
"metadata": {
"id": "ea26ba63",
"outputId": "8862ad2a-f9d1-4b05-ec5c-bf1bd5054797"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In english there is a proverb , \"Work is worship\".\n"
]
}
],
"source": [
"print(st3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "937ce711",
"metadata": {
"id": "937ce711"
},
"outputs": [],
"source": [
"st4 = \"In english there is a proverb , \\\"Work is worship\\\".\"\n",
"# Double quotes has a special meaning --- It tells the begining of string\n",
"# and end of string\n",
"# To remove that special meaning and use double quotes\n",
"# like any other character in string, use \\\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "793e798b",
"metadata": {
"id": "793e798b",
"outputId": "df557b45-5d23-4642-c71e-0e0e83187c4e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In english there is a proverb , 'Work is worship'.\n"
]
}
],
"source": [
"st4 = 'In english there is a proverb , \\'Work is worship\\'.'\n",
"print(st4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8208e31",
"metadata": {
"id": "c8208e31"
},
"outputs": [],
"source": [
"# Counting length starts from 1.\n",
"# Postitive Indexing start from 0. Negative indexing start from -1.\n",
"# ., space everything is counted as a character."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61f0c5dc",
"metadata": {
"id": "61f0c5dc"
},
"outputs": [],
"source": [
"# Negative indexing\n",
"st = \"Double quotes has a special meaning. It tells the begining of string \\\n",
"and end of string. To remove that special meaning and use double quotes \\\n",
"like any other character in string, use \""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "370fff63",
"metadata": {
"id": "370fff63",
"outputId": "e1c97e26-3eeb-4bfa-e877-efa1fb7a06bc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Double quotes has a special meaning. It tells the begining of string and end of string. To remove that special meaning and use double quotes like any other character in string, use \n"
]
}
],
"source": [
"print(st)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "447fbfb3",
"metadata": {
"id": "447fbfb3",
"outputId": "3cbd8cb8-f4d7-4357-9508-a429536309dd"
},
"outputs": [
{
"data": {
"text/plain": [
"'s has a special meaning. It tells the begining of string and end of string. To remove that special meaning and use double quotes like any other character in string'"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[12:-6]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ead74e2",
"metadata": {
"id": "9ead74e2"
},
"outputs": [],
"source": [
"st5 = 'Python is easy to learn'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ebb9e218",
"metadata": {
"id": "ebb9e218",
"outputId": "f6b6d6dd-c6d4-43f0-bec8-ac82e0ef87bc"
},
"outputs": [
{
"data": {
"text/plain": [
"'to'"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st5[-8:-6]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "261ada2c",
"metadata": {
"id": "261ada2c"
},
"outputs": [],
"source": [
"# Extract word \"than\" using\n",
"# only positive indexing,\n",
"# only negative indexing,\n",
"# RHS positive index , LHS negative index\n",
"# RHS negative index , LHS positive index\n",
"st6 = 'Its better to understand Python through coding than theoretically'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcf5c26a",
"metadata": {
"id": "fcf5c26a",
"outputId": "15a3bf54-e922-45b4-acfa-b12c59873f4c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"than\n",
"than\n",
"than\n",
"than\n"
]
}
],
"source": [
"print(st6[-18:51])\n",
"print(st6[47:-14])\n",
"print(st6[47:51])\n",
"print(st6[-18:-14])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87c9f4d7",
"metadata": {
"id": "87c9f4d7",
"outputId": "ac760345-bfe7-40f0-95a3-4342e85fc5b0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"than \n",
"than\n",
"than\n"
]
}
],
"source": [
"print(st6[47:52])\n",
"print(st6[-18:-14])\n",
"print(st6[47:-14])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d276eba",
"metadata": {
"id": "8d276eba"
},
"outputs": [],
"source": [
"st = \"Prog'ram'ming\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64209de0",
"metadata": {
"id": "64209de0",
"outputId": "c672dbd6-c549-4bb7-fef9-1656d1efea65"
},
"outputs": [
{
"data": {
"text/plain": [
"\"mar'g\""
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[7:-11:-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "265870f5",
"metadata": {
"id": "265870f5",
"outputId": "acc211ac-c3da-427c-ec90-2d5fcf769bf6"
},
"outputs": [
{
"data": {
"text/plain": [
"'ro'"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[4:1:-2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c954956d",
"metadata": {
"id": "c954956d",
"outputId": "5242ed9f-6b2a-4ad5-fdd8-d8e3a69fc459"
},
"outputs": [
{
"data": {
"text/plain": [
"'rogr'"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[1:5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "483b0c8d",
"metadata": {
"id": "483b0c8d",
"outputId": "21c378df-8d0c-4d07-a2e5-c74efa1ecb43"
},
"outputs": [
{
"data": {
"text/plain": [
"''"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[5:1:1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0530e097",
"metadata": {
"id": "0530e097",
"outputId": "a1315138-f117-4b8a-bbbe-48b0ad092295"
},
"outputs": [
{
"data": {
"text/plain": [
"'argo'"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[5:1:-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a222fe6",
"metadata": {
"id": "5a222fe6"
},
"outputs": [],
"source": [
"st = \"Programming\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a25a8e24",
"metadata": {
"id": "a25a8e24",
"outputId": "3f23eacb-a0d9-41f0-e974-0856e8952e21"
},
"outputs": [
{
"data": {
"text/plain": [
"'gnimmargorP'"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st[::-1] #### Reversing a string without using any functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b769dae6",
"metadata": {
"id": "b769dae6"
},
"outputs": [],
"source": [
"### Find weather a string is pallindrome\n",
"st1 = 'dad' # True\n",
"st2 = 'madam' # True\n",
"st3 = 'malayalam' # True\n",
"st4 = 'hannah' # True\n",
"st5 = 'papa' # False\n",
"st6 = '121' # True\n",
"st7 = '1441' # True\n",
"st8 = '4566' # False\n",
"st9 = '22222222' # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23b74556",
"metadata": {
"id": "23b74556",
"outputId": "41e37134-84aa-4289-a261-6e4be767c137"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st1==st1[::-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1821a417",
"metadata": {
"id": "1821a417",
"outputId": "b061cf52-010d-4a1e-d919-177ea3ef80b9"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st5==st5[::-1]"
]
},
{
"cell_type": "markdown",
"id": "4204f7d7",
"metadata": {
"id": "4204f7d7"
},
"source": [
"### String fuctions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e49912f",
"metadata": {
"id": "5e49912f"
},
"outputs": [],
"source": [
"st = 'Its better to understand Python through coding than theoretically'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37a80246",
"metadata": {
"id": "37a80246",
"outputId": "31aa0b15-d8a9-4589-a769-c4fb88dfb2f6"
},
"outputs": [
{
"data": {
"text/plain": [
"47"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st.find('than') # Starting Index/Position at which than is present in the string"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20cb4fa3",
"metadata": {
"id": "20cb4fa3",
"outputId": "5c3f7872-6189-4016-cc52-31fe4505a85d"
},
"outputs": [
{
"data": {
"text/plain": [
"65"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(st)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f17bd50",
"metadata": {
"id": "7f17bd50",
"outputId": "76e8d331-56d6-4452-d6c9-1f26f0fce90d"
},
"outputs": [
{
"data": {
"text/plain": [
"-18"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st.find('than') - len(st)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e06a5ce8",
"metadata": {
"id": "e06a5ce8"
},
"outputs": [],
"source": [
"st2 = 'Python is very simple'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a6d7082",
"metadata": {
"id": "7a6d7082",
"outputId": "141d9c61-75fc-44ee-93e5-059205466291"
},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st2.find('is')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f74fd7df",
"metadata": {
"id": "f74fd7df",
"outputId": "28e7cd04-23da-4e3b-ea3d-58c4bc364d48"
},
"outputs": [
{
"data": {
"text/plain": [
"'python is very simple'"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st2.lower()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a784a87",
"metadata": {
"id": "5a784a87",
"outputId": "f57ee5f2-4d05-438e-beb9-1f352dc9de0f"
},
"outputs": [
{
"data": {
"text/plain": [
"'PYTHON IS VERY SIMPLE'"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st2.upper()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "915ad997",
"metadata": {
"id": "915ad997"
},
"outputs": [],
"source": [
"st1 = 'Hi. '"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "476696ef",
"metadata": {
"id": "476696ef"
},
"outputs": [],
"source": [
"st2 = 'How are you ?'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e380be01",
"metadata": {
"id": "e380be01"
},
"outputs": [],
"source": [
"st3 = st1+st2 ### Concatenate multiple strings"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dac63dd",
"metadata": {
"id": "8dac63dd",
"outputId": "8b985864-54f7-4b81-cc15-2977b7cba239"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi. Python is very simple\n"
]
}
],
"source": [
"print(st3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29bae95d",
"metadata": {
"id": "29bae95d",
"outputId": "4d3edc24-b2fc-4d84-c829-13ecd19ceb5c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi. Hi. Hi. \n"
]
}
],
"source": [
"print(st1*3) ### String multiplication is nothing but repetition"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0d9b18d",
"metadata": {
"id": "b0d9b18d"
},
"outputs": [],
"source": [
"st2 = 'Python is very simple'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3562122",
"metadata": {
"id": "e3562122",
"outputId": "ae789145-d11a-4baa-aca2-3b262bf071a2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python \n"
]
}
],
"source": [
"st3 = st2[:7]\n",
"print(st3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96632368",
"metadata": {
"id": "96632368",
"outputId": "bb246e7b-1b9f-4083-dd65-3891bec585df"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"is very simple\n"
]
}
],
"source": [
"st4 = st2[7:]\n",
"print(st4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "230997fb",
"metadata": {
"id": "230997fb",
"outputId": "2ce4861e-eda5-4fbc-d4be-e3135cca77dc"
},
"outputs": [
{
"data": {
"text/plain": [
"'PYTHON is very simple'"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st3.upper()+st4"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f51c699b",
"metadata": {
"id": "f51c699b",
"outputId": "09c78462-2ae6-40a2-f1b3-8b7e2a974559"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on method_descriptor:\n",
"\n",
"upper(self, /)\n",
" Return a copy of the string converted to uppercase.\n",
"\n"
]
}
],
"source": [
"### If you want to explore other function Use\n",
"help(str.upper)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc2fbc32",
"metadata": {
"id": "dc2fbc32",
"outputId": "010e9adb-7acc-4176-fe82-3a0b6d2c2aa9"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python is simple\n"
]
}
],
"source": [
"st3 = 'python is simple'\n",
"print(st3.capitalize())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34854a51",
"metadata": {
"id": "34854a51",
"outputId": "2a7f0c77-2413-4074-a865-f1469c5edb17"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python is simple\n"
]
}
],
"source": [
"print(st3[0].upper()+st3[1:])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f04783c6",
"metadata": {
"id": "f04783c6"
},
"outputs": [],
"source": [
"st5 = 'Python is simple to code. Python Python is easy to understand'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05c1717c",
"metadata": {
"id": "05c1717c",
"outputId": "eb748ba5-210e-4335-b5ac-bf23fe2a1c50"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"print(st5.find('Python',0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b49cf27",
"metadata": {
"id": "1b49cf27",
"outputId": "34d7f7ce-638a-4f6b-a540-44bd193cfa5e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"26\n"
]
}
],
"source": [
"print(st5.find('Python',1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa143977",
"metadata": {
"id": "aa143977",
"outputId": "a2ece6ee-c457-41af-90e8-dbf5e4fd7fce"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"33\n"
]
}
],
"source": [
"print(st5.find('Python',27))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d753fca0",
"metadata": {
"id": "d753fca0",
"outputId": "bc21d25f-f35b-4d4d-d2f3-f88239369639"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on method_descriptor:\n",
"\n",
"upper(self, /)\n",
" Return a copy of the string converted to uppercase.\n",
"\n"
]
}
],
"source": [
"help(str.upper)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "392b462b",
"metadata": {
"id": "392b462b",
"outputId": "96e5b538-edce-46f7-ffda-b8188c5b75dc"
},
"outputs": [
{
"data": {
"text/plain": [
"'H'"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str.upper('h')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2925a1ed",
"metadata": {
"id": "2925a1ed"
},
"outputs": [],
"source": [
"st = 'h'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dadeb2da",
"metadata": {
"id": "dadeb2da",
"outputId": "33d1567a-b3a4-46ba-a725-374d000d3ede"
},
"outputs": [
{
"ename": "TypeError",
"evalue": "str.upper() takes no arguments (1 given)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [184]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mst\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mupper\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mh\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[1;31mTypeError\u001b[0m: str.upper() takes no arguments (1 given)"
]
}
],
"source": [
"st.upper('h')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f88e3d8c",
"metadata": {
"id": "f88e3d8c",
"outputId": "70711462-c82e-4a4f-882c-57aadc17e276"
},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 187,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'abcdef'.find('c',4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71fbbef4",
"metadata": {
"id": "71fbbef4",
"outputId": "9e300784-f321-4ed4-c77c-6e9bf48a9086"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on method_descriptor:\n",
"\n",
"find(...)\n",
" S.find(sub[, start[, end]]) -> int\n",
" \n",
" Return the lowest index in S where substring sub is found,\n",
" such that sub is contained within S[start:end]. Optional\n",
" arguments start and end are interpreted as in slice notation.\n",
" \n",
" Return -1 on failure.\n",
"\n"
]
}
],
"source": [
"help(str.find)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cd4dac8",
"metadata": {
"id": "3cd4dac8",
"outputId": "36f5b77a-381b-48bf-dc03-bf8e082fb948"
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 190,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st5 = 'Python is simple to code. Python Python is easy to understand'\n",
"st5.count('Python')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "693ecee2",
"metadata": {
"id": "693ecee2",
"outputId": "9382096b-2c8f-4754-91db-e0451d49250d"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 191,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st5.count('is')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b277355",
"metadata": {
"id": "6b277355",
"outputId": "48c590e8-e021-4c43-aa8b-02436398de6d"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"st5.count('to')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e706eff",
"metadata": {
"id": "1e706eff",
"outputId": "d9a5c19a-c68b-4be8-b522-f65ef34d1518"
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 254,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Python'.count('ho',-5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4147052a",
"metadata": {
"id": "4147052a",
"outputId": "76c24c8b-7eb5-49ec-d771-2a0d8850bd75"
},
"outputs": [
{
"data": {
"text/plain": [
"'ohty'"
]
},
"execution_count": 255,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Python'[-2:0:-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b6d66f0",
"metadata": {
"id": "9b6d66f0",
"outputId": "aedef9b9-4dac-4999-be8c-6d9b1f1b0a3e"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 256,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Day1_09092023.ipynb'.endswith('ipynb')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8aba02ea",
"metadata": {
"id": "8aba02ea",
"outputId": "c2db2348-cebc-4740-dc07-8f0b32071f39"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Day1_09092023.csv'.endswith('ipynb')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f225180",
"metadata": {
"id": "4f225180",
"outputId": "8df9d967-8d0a-46f5-918b-5d0a87e3e62a"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 208,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Apple'.endswith('e')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aecfb047",
"metadata": {
"id": "aecfb047",
"outputId": "25d09e62-fbf6-4982-9692-efc9193ae974"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Apple'.endswith('le')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6cd9ebf0",
"metadata": {
"id": "6cd9ebf0",
"outputId": "fb1c081f-8cdd-4a6e-dc7a-74be0a18843d"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 251,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Apple'.endswith('pe')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa0d22bb",
"metadata": {
"id": "fa0d22bb",
"outputId": "a697cb96-d457-4c4e-fe9a-b0fad8684f2e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on method_descriptor:\n",
"\n",
"find(...)\n",
" S.find(sub[, start[, end]]) -> int\n",
" \n",
" Return the lowest index in S where substring sub is found,\n",
" such that sub is contained within S[start:end]. Optional\n",
" arguments start and end are interpreted as in slice notation.\n",
" \n",
" Return -1 on failure.\n",
"\n"
]
}
],
"source": [
"help(str.find)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20e7ba09",
"metadata": {
"id": "20e7ba09",
"outputId": "027dee8f-3f54-4584-ebc2-a4d0af5b9475"
},
"outputs": [
{
"ename": "ValueError",
"evalue": "substring not found",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [267]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mProgramming\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mz\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[1;31mValueError\u001b[0m: substring not found"
]
}
],
"source": [
"'Programming'.index('z')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b37cf49b",
"metadata": {
"id": "b37cf49b",
"outputId": "41ad8f27-4cd5-4a8d-dbe9-08ac4b5949b0"
},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 268,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Programming'.find('z')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54bf9f85",
"metadata": {
"id": "54bf9f85",
"outputId": "78c47f99-6000-40b8-ffd0-95b778ce3072"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on method_descriptor:\n",
"\n",
"isalnum(self, /)\n",
" Return True if the string is an alpha-numeric string, False otherwise.\n",
" \n",
" A string is alpha-numeric if all characters in the string are alpha-numeric and\n",
" there is at least one character in the string.\n",
"\n"
]
}
],
"source": [
"help(str.isalnum)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4cb32bdf",
"metadata": {
"id": "4cb32bdf",
"outputId": "b9dcbe8c-4075-49a9-810b-636419ade2d0"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'ghdgfdgd556778'.isalnum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c7e265e",
"metadata": {
"id": "9c7e265e",
"outputId": "be963928-4a9d-4e90-96d1-efcba415c886"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 272,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'ghdgfdg'.isalnum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f80e0f4",
"metadata": {
"id": "1f80e0f4",
"outputId": "0148f298-869a-44ef-b213-4af556720605"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 283,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'23456'.isalnum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73437ac9",
"metadata": {
"id": "73437ac9",
"outputId": "e5adfb66-7068-4fc1-9ae6-77797df72b27"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 284,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'234.56'.isalnum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a7ca790",
"metadata": {
"id": "4a7ca790",
"outputId": "4a50b883-1979-48f1-e7db-bf34a3bad99f"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 285,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'234:56'.isalnum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a062fd9d",
"metadata": {
"id": "a062fd9d",
"outputId": "32a627a9-75da-4eae-f9d5-c5cc22e4dc90"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 286,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'234@56'.isalnum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97eb12c3",
"metadata": {
"id": "97eb12c3",
"outputId": "77569f09-e529-40aa-971d-3ffefb8534f4"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 287,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'sdfsdfsdf'.isalpha()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffdb026d",
"metadata": {
"id": "ffdb026d",
"outputId": "9e604554-c195-41df-a6c1-f3043dabef64"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 288,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'345gfgfgf3453'.isalpha()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5bfed20",
"metadata": {
"id": "d5bfed20"
},
"outputs": [],
"source": [
"'dffddd'.isalpha()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1ba8ad5",
"metadata": {
"id": "a1ba8ad5",
"outputId": "ca6c3b9a-2923-4cc8-baac-d32ec85e1bf2"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 292,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'1234.5.6'.isnumeric()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbb5d879",
"metadata": {
"id": "bbb5d879",
"outputId": "5f248ea7-185a-43ad-b6dd-1c052ac57e30"
},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, True)"
]
},
"execution_count": 297,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'1೫२'.isdecimal(),'1೫२'.isdigit(),'1೫२'.isnumeric()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "573fe494",
"metadata": {
"id": "573fe494",
"outputId": "a0222c17-6c72-4e28-945b-aaea7d13c8ba"
},
"outputs": [
{
"data": {
"text/plain": [
"(False, True, True)"
]
},
"execution_count": 298,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'¹⁹'.isdecimal(),'¹⁹'.isdigit(),'¹⁹'.isnumeric()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18f9bca5",
"metadata": {
"id": "18f9bca5",
"outputId": "a4fe174d-7589-4844-a5db-1fb1c830e4e6"
},
"outputs": [
{
"data": {
"text/plain": [
"(False, False, True)"
]
},
"execution_count": 299,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'¼'.isdecimal(),'¼'.isdigit(),'¼'.isnumeric()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d15dc01a",
"metadata": {
"id": "d15dc01a",
"outputId": "782f6225-33be-4815-d277-1d996be358a9"
},
"outputs": [
{
"data": {
"text/plain": [
"(True, False, False)"
]
},
"execution_count": 331,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'76767658878'.isdecimal(),'76767689999¹⁹'.isdecimal(),'76767689999¼'.isdecimal()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "485a8267",
"metadata": {
"id": "485a8267",
"outputId": "4dd18555-fdd2-4159-dbea-29719a736e96"
},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, False)"
]
},
"execution_count": 332,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'76767658878'.isdigit(),'7676789999¹⁹'.isdigit(),'7676768999¼'.isdigit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6119932e",
"metadata": {
"id": "6119932e",
"outputId": "54599b66-8738-409c-bd37-1e30ce5fcb2e"
},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, True)"
]
},
"execution_count": 333,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'76767658878'.isnumeric(),'76767689999¹⁹'.isnumeric(),'76767689999¼'.isnumeric()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f42b1a5d",
"metadata": {
"id": "f42b1a5d",
"outputId": "dbc353c6-0e7c-4632-a554-bcdf6260fd05"
},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 327,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(float('23.6'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78831a0a",
"metadata": {
"id": "78831a0a",
"outputId": "7c36c141-9e1f-44d9-afa1-178406e6b9c7"
},
"outputs": [
{
"data": {
"text/plain": [
"2389"
]
},
"execution_count": 328,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int('2389')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e5a4c36",
"metadata": {
"id": "7e5a4c36",
"outputId": "27cd0ce5-b5a3-4d0d-83a1-0290a48e69d7"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 329,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'2*4'.isnumeric()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd6398f0",
"metadata": {
"id": "bd6398f0",
"outputId": "04a01e21-7c3f-46f1-b36b-c257d175474a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"import math\n",
"print(math.floor(3.6))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a838858",
"metadata": {
"id": "9a838858",
"outputId": "cb3ddaf3-cf51-440a-ce8f-fc0a97d2455b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"print(round(3.6))"
]
},
{
"cell_type": "markdown",
"id": "f453519e",
"metadata": {
"id": "f453519e"
},
"source": [
"### Practice questions"
]
},
{
"cell_type": "markdown",
"id": "c9c34582",
"metadata": {
"id": "c9c34582"
},
"source": [
"- Write a Python program that takes a string as input and uses slicing to reverse the string. For example, if the input is \"Hello, World!\", the program should output \"!dlroW ,olleH\".\n",
"\n",
"- Given a string, create two new strings by slicing the original string. One string should contain characters at even indices, and the other should contain characters at odd indices. For example, if the input string is \"Python\", the output should be two strings: \"Pto\" and \"yhn\".\n",
"\n",
"- Write a Python function that takes a string as input and extracts alternate characters to create a new string. For example, if the input is \"Python\", the output should be \"Pto\". Ensure that your program handles both even and odd-length input strings.\n",
"\n",
"- Write a Python program that extracts and prints the domain (e.g., \"gmail.com\") from an email address. Given an email address as input, the program should slice and extract the domain portion after the \"@\" symbol.\n",
"\n",
"- Create a Python program that takes a string as input and swaps the first and last characters. For example, if the input is \"Python\", the program should output \"nythoP\". Use string slicing to achieve this without loops\n",
"\n",
"- Create a Python program that takes a string as input and reflects it by repeating it backward. For example, if the input is \"Python\", the program should output \"PythonnohtyP\". Use string slicing to achieve this without loops."
]
},
{
"cell_type": "markdown",
"id": "06cbf0c8",
"metadata": {
"id": "06cbf0c8"
},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "56891dc1",
"metadata": {
"id": "56891dc1"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f1ebffc",
"metadata": {
"id": "9f1ebffc"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment