{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JAAuyh3mxg14", "outputId": "fda3bfc6-17b5-4b75-bf69-202af8e27998" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pakistan zinda bad\n", "Pakistan zinda bad\n" ] } ], "source": [ "data = \"Pakistan zinda bad\"\n", "\n", "print(data)\n", "print(data)" ] }, { "cell_type": "code", "source": [ "data = \"Pakistan zinda bad 123 !@#$\"\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AYjpliV_yQj2", "outputId": "3a885d9d-7656-4654-dff6-66e13fd31559" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pakistan zinda bad\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "data = 'Pakistan zinda bad 123 !@#$'\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DoKS_3IBy_MI", "outputId": "dd5b95ff-bdbb-446c-d0c1-73cb3edd2662" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pakistan zinda bad 123 !@#$\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "data = \"Pakistan zinda bad 123 !@#$\" hello world \"\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "xnz82JKnzs3z", "outputId": "747c994c-057b-452b-b666-e92b494a71be" }, "execution_count": 6, "outputs": [ { "output_type": "error", "ename": "SyntaxError", "evalue": "ignored", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m data = \"Pakistan zinda bad 123 !@#$\" hello world \"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" ] } ] }, { "cell_type": "code", "source": [ "data = 'Pakistan zinda bad 123 !@#$ \" Hello world '\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "r0MIT3u1z8pn", "outputId": "19094d7b-622c-4b2e-b453-5115a97b47f5" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pakistan zinda bad 123 !@#$ \" Hello world \n", "\n" ] } ] }, { "cell_type": "code", "source": [ "data = \"Pakistan zinda bad 123 !@#$ ' Hello world \"\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vo0FMFYl0E3s", "outputId": "84015dd9-8c91-4430-bf93-04b87b58223f" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pakistan zinda bad 123 !@#$ ' Hello world \n", "\n" ] } ] }, { "cell_type": "code", "source": [ "data = 'Pakistan zinda bad' 123 !@#$ \" Hello world '\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "5hHlptfL0KAT", "outputId": "d8b73c68-18ca-4b8b-87e4-25aea1708514" }, "execution_count": 9, "outputs": [ { "output_type": "error", "ename": "SyntaxError", "evalue": "ignored", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m data = 'Pakistan zinda bad' 123 !@#$ \" Hello world '\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ] }, { "cell_type": "markdown", "source": [ "convert special character into normal **character**\n", "* place \"\\\\\" before any special character" ], "metadata": { "id": "V14_lFlW0dYV" } }, { "cell_type": "code", "source": [ "data = 'Pakistan zinda bad\\' 123 !@#$ \" Hello world '\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ioePOFRL0R4C", "outputId": "754472f3-2d09-4533-b601-dddd7349f502" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pakistan zinda bad' 123 !@#$ \" Hello world \n", "\n" ] } ] }, { "cell_type": "code", "source": [ "data = 'Pakistan zinda bad \\' 123 !@#$ \" Hello world '\n", "\n", "print(data)\n", "print(type(data))" ], "metadata": { "id": "yr-jI8hU0-4w" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# assign mulitline string" ], "metadata": { "id": "CFYbuTHM1RBY" } }, { "cell_type": "code", "source": [ "abc = \"Line1 pakistan \"\\\n", "\"Line2 Pakistan \"\\\n", "\"Line3 Pakistan \"\n", "\n", "print(abc)\n", "print(type(abc))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aOXazw901M5w", "outputId": "d0f80933-b877-4fbf-eba9-3b7226246e29" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Line1 pakistan Line2 Pakistan Line3 Pakistan \n", "\n" ] } ] }, { "cell_type": "code", "source": [ "abc = \"\"\"\n", "line1 Hello world\n", "line2 Pakistan zinda bad\n", "line3 we love our country\n", "\"\"\"\n", "\n", "abc" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "qJi0riK21NAy", "outputId": "29b713d4-a82b-409c-c0e8-854326d8610e" }, "execution_count": 13, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'\\nline1 Hello world\\nline2 Pakistan zinda bad\\nline3 we love our country\\n'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 13 } ] }, { "cell_type": "code", "source": [ "abc = \"\"\"\n", "line1 Hello world\n", "line2 Pakistan zinda bad\n", "line3 we love our\\tcountry\n", "\"\"\"\n", "\n", "print(abc)\n", "print(type(abc))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aFVT3EcM2T9E", "outputId": "25198af8-fc20-4574-e893-b8ad0e4d1dd8" }, "execution_count": 17, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "line1 Hello world\n", "line2 Pakistan zinda bad\n", "line3 we love our\tcountry\n", "\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "abc = '''\n", "line1 Hello world\n", "line2 Pakistan 'zinda bad\n", "line3 we love our country\n", "'''\n", "\n", "print(abc)\n", "print(type(abc))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-mEUcTGw1NDa", "outputId": "21cd2243-3f43-4338-da1a-f492b56bc75b" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "line1 Hello world\n", "line2 Pakistan 'zinda bad\n", "line3 we love our country\n", "\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Concatination and Text Formation" ], "metadata": { "id": "5bOnPvuR39eW" } }, { "cell_type": "code", "source": [ "\"1\" + 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 178 }, "id": "ZzETVvTT23gJ", "outputId": "8cc5e66f-34a6-4c26-ccb0-ed991c5bbe36" }, "execution_count": 18, "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[0;32m----> 1\u001b[0;31m \u001b[0;34m\"1\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ] }, { "cell_type": "code", "source": [ "\"1\" + \"1\"" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "SBcjFae_4sVk", "outputId": "b008093c-8e44-4814-e0e1-13914bae595f" }, "execution_count": 19, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'11'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 19 } ] }, { "cell_type": "code", "source": [ "1 + 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wZ9dF2LC4xP5", "outputId": "b68ed2cf-ac8d-41ac-c1d7-440e4589009f" }, "execution_count": 20, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": {}, "execution_count": 20 } ] }, { "cell_type": "code", "source": [ "a = input(\"Enter no1:\")\n", "b = input(\"Enter no2\")\n", "\n", "print(type(a), type(b))\n", "print(a + b) # concatination" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IOoyEzmH401D", "outputId": "83e55ab1-267f-4428-fe0b-5244a5e6e314" }, "execution_count": 21, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter no1:7\n", "Enter no28\n", " \n", "78\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Type casting\n", "* int()\n", "* str()\n", "* float()\n", "* list()\n", "* tuple()\n", "* dict()\n", "* set()\n" ], "metadata": { "id": "7FVEoNpk5s-s" } }, { "cell_type": "code", "source": [], "metadata": { "id": "6uprV4ft8hNC" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "a = \"7\" # string\n", "print( a, type(a))\n", "\n", "a = int(a)# convert string into int\n", "print( a, type(a))\n", "\n", "a = float(a)# convert string into int\n", "print( a, type(a))\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zivwfeiL5w1A", "outputId": "3322ae89-1c22-4ae5-d1af-de542a825783" }, "execution_count": 27, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "7 \n", "7 \n", "7.0 \n" ] } ] }, { "cell_type": "code", "source": [ "a = input(\"Enter no1:\")\n", "b = input(\"Enter no2\")\n", "a = int(a)\n", "b = int(b)\n", "print(type(a), type(b))\n", "print(a + b) # addition operation" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uaaWLgJ15m0y", "outputId": "773d64a0-0112-4dfc-8cdf-3859c48ddb68" }, "execution_count": 28, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter no1:1\n", "Enter no21\n", " \n", "2\n" ] } ] }, { "cell_type": "code", "source": [ "a = int(input(\"Enter no1:\"))\n", "b = int(input(\"Enter no2\"))\n", "\n", "print(type(a), type(b))\n", "print(a + b)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UzBaeTZg5flt", "outputId": "87e676c3-7e5d-41f9-ac2a-9a031cb60866" }, "execution_count": 29, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter no1:1\n", "Enter no21\n", " \n", "2\n" ] } ] }, { "cell_type": "code", "source": [ "int(\"1\") + 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4wIIWhTB9B8x", "outputId": "3963a548-7bba-4b69-9ba2-8203c91db908" }, "execution_count": 30, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": {}, "execution_count": 30 } ] }, { "cell_type": "code", "source": [ "\"1\" + str(1)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "pLdwwHUl-us2", "outputId": "66da5809-73d2-4142-cde4-7df873d71469" }, "execution_count": 31, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'11'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 31 } ] }, { "cell_type": "code", "source": [ "roll_no = 200\n", "name = \"Muhammad Qasim\"\n", "fname = \"Muhammad Aslam\"\n", "contact_no = \"03152968211\"\n", "\n", "card = \"NED University \\nStudent roll no:\" + str(roll_no)+\\\n", "\"\\nStudent Name: \" + name + \"\\nFather's Name: \" + fname +\\\n", "\"\\nContact no: \"\\\n", "+ contact_no\n", "\n", "print(card)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RnpOTLaB-xZB", "outputId": "3e6ac426-2b75-4480-ea88-c92ea7134edb" }, "execution_count": 48, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NED University \n", "Student roll no:200\n", "Student Name: Muhammad Qasim\n", "Father's Name: Muhammad Aslam\n", "Contact no: 03152968211\n" ] } ] }, { "cell_type": "code", "source": [ "roll_no = 200\n", "name = \"Muhammad Qasim\"\n", "fname = \"Muhammad Aslam\"\n", "contact_no = \"03152968211\"\n", "\n", "card = \"\"\"NED University\n", "Student roll no:{}\n", "Student Name: {}\n", "Father's Name: {}\n", "Contact no: {}\"\"\".format(roll_no,name,fname,contact_no)\n", "\n", "print(card)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "c8phL8Pw-9y9", "outputId": "843d1d63-dd08-4b7f-f231-4c37997c587a" }, "execution_count": 57, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NED University \n", "Student roll no:200\n", "Student Name: Muhammad Qasim\n", "Father's Name: Muhammad Aslam\n", "Contact no: 03152968211\n" ] } ] }, { "cell_type": "code", "source": [ "roll_no = 200\n", "name = \"Muhammad Qasim\"\n", "fname = \"Muhammad Aslam\"\n", "contact_no = \"03152968211\"\n", "\n", "card = \"\"\"NED University\n", "Student roll no:{}\n", "Student Name: {}\n", "Father's Name: {}\n", "Contact no: {}\"\"\".format(roll_no,name,fname,contact_no)\n", "# 0 1 2 3\n", "\n", "print(card)" ], "metadata": { "id": "12k6-Yu4CKXd" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "roll_no = 200\n", "name = \"Asif Khan\"\n", "fname = \"Muhammad Aslam\"\n", "contact_no = \"03152968211\"\n", "\n", "card = \"\"\"NED University\n", "Student roll no:{2}\n", "Student Name: {0}\n", "Father's Name: {1}\n", "Contact no: {3}\"\"\".format(name,fname,roll_no,contact_no)\n", "# 0 1 2 3\n", "\n", "print(card)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Cn4E1hiBEr_S", "outputId": "e63a915f-f308-4ab4-eef1-0e405694e07c" }, "execution_count": 61, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NED University \n", "Student roll no:200\n", "Student Name: Asif Khan\n", "Father's Name: Muhammad Aslam\n", "Contact no: 03152968211\n" ] } ] }, { "cell_type": "code", "source": [ "roll_no = 200\n", "name = \"Asif Khan\"\n", "fname = \"Muhammad Aslam\"\n", "contact_no = \"03152968211\"\n", "\n", "card = f\"\"\"NED University\n", "Student roll no:\\t{roll_no}\n", "Student Name:\\t {name}\n", "Father's Name: {fname}\n", "Contact no: {contact_no}\n", "2+2 = {2+2}\"\"\"\n", "\n", "print(card)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZL3eCpt9EwoM", "outputId": "2e4d1228-2b2c-405c-dbf7-55dcd828afb9" }, "execution_count": 70, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "NED University \n", "Student roll no:\t200\n", "Student Name:\t Asif Khan\n", "Father's Name: Muhammad Aslam\n", "Contact no: 03152968211 \n", "2+2 = 4\n" ] } ] }, { "cell_type": "code", "source": [ "roll_no = input(\"roll no\")\n", "name = input(\"Name: \")\n", "fname = input(\"Father's Name\")\n", "contact_no = input(\"contact no: \")\n", "print('=====================')\n", "\n", "card = f\"\"\"NED University\n", "Student roll no:{roll_no}\n", "Student Name: {name}\n", "Father's Name: {fname}\n", "Contact no: {contact_no}\"\"\"\n", "\n", "print(card)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "elHHtEjOGAA5", "outputId": "7859a6f5-b7b9-436a-adea-c82c3cd49253" }, "execution_count": 68, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "roll no444\n", "Name: Iqbal\n", "Father's NameHussain\n", "contact no: 11234\n", "=====================\n", "NED University \n", "Student roll no:444\n", "Student Name: Iqbal\n", "Father's Name: Hussain\n", "Contact no: 11234\n" ] } ] }, { "cell_type": "code", "source": [ "%s string\n", "%d number" ], "metadata": { "id": "9GDNmUWaG9pj" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "roll_no = 111\n", "name = \"Muhammad qasim\"\n", "\"Student name %s \\n roll no: %d\" % (name,roll_no)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "5p8aqgdvJRIz", "outputId": "9f9d22e7-1aa7-409e-c3e0-5df53cebbae4" }, "execution_count": 71, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Student name Muhammad qasim \\n roll no: 111'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 71 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "1RttExOUJmCD" }, "execution_count": null, "outputs": [] } ] }