{ "cells": [ { "cell_type": "markdown", "id": "58ed4457-f388-4d13-bcea-189f8e330cb8", "metadata": {}, "source": [ "1.\tWrite a Program to print the student details using Escape sequence characters.(Example:\\n,\\t,\\”)." ] }, { "cell_type": "code", "execution_count": 1, "id": "ac882dfd-ec8f-45c6-8fea-06402e1cbb16", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Student Details:\n", "\n", "Name:\t\t\"John Doe\"\n", "Roll No:\t\"12345\"\n", "Course:\t\t\"B.Tech Computer Science\"\n", "College:\t\"ABC Engineering College\"\n", "Location:\t\"Hyderabad\"\n" ] } ], "source": [ "# Program to print student details using escape sequences\n", "\n", "print(\"Student Details:\\n\")\n", "print(\"Name:\\t\\t\\\"John Doe\\\"\")\n", "print(\"Roll No:\\t\\\"12345\\\"\")\n", "print(\"Course:\\t\\t\\\"B.Tech Computer Science\\\"\")\n", "print(\"College:\\t\\\"ABC Engineering College\\\"\")\n", "print(\"Location:\\t\\\"Hyderabad\\\"\")\n" ] }, { "cell_type": "markdown", "id": "6273f28f-cb38-4c4a-aecb-34cbdfcc8f49", "metadata": {}, "source": [ "2.\t The total number of students in a class are 45 out of which 25 are boys. If 80% of the total students secured grade 'A' out of which 16 are boys, then Develop a Program to calculate the total number of girls getting grade 'A'." ] }, { "cell_type": "code", "execution_count": 2, "id": "891b28c8-01ae-47e0-8571-2e95fc28cab3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total number of girls getting grade 'A': 20\n" ] } ], "source": [ "# Program to calculate the total number of girls getting grade 'A'\n", "\n", "total_students = 45\n", "total_boys = 25\n", "percentage_A = 80\n", "boys_with_A = 16\n", "\n", "# Calculate total students with grade 'A'\n", "students_with_A = (percentage_A / 100) * total_students\n", "\n", "# Calculate girls with grade 'A'\n", "girls_with_A = students_with_A - boys_with_A\n", "\n", "print(\"Total number of girls getting grade 'A':\", int(girls_with_A))\n" ] }, { "cell_type": "markdown", "id": "14e3e0b0-42b4-4870-b014-35c8c197d396", "metadata": {}, "source": [ "3.\t Develop a Program to calculate the sum of the first and the last digit of a 56743" ] }, { "cell_type": "code", "execution_count": 3, "id": "4da75663-be36-47aa-91c5-bc2e181a7407", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sum of the first and last digit of 56743 is: 8\n" ] } ], "source": [ "\n", "\n", "number = 56743\n", "\n", "# Last digit\n", "last_digit = number % 10\n", "\n", "# First digit (find number of digits and divide)\n", "first_digit = number // 10000 # since 56743 has 5 digits\n", "\n", "sum_digits = first_digit + last_digit\n", "\n", "print(\"The sum of the first and last digit of\", number, \"is:\", sum_digits)\n" ] }, { "cell_type": "markdown", "id": "fe5b91a9-d51f-47bb-9a37-16638a7db3f6", "metadata": {}, "source": [ "4.\t Write a program for calculating the bill amount for an item with the following scenarios\n", "\n", "•\tThe quantity of item sold, and price of the item must read from the user and calculate the bill\n", "\n", "•\tAfter that there is a 10% discount on bill amount\n", "\n", "•\tThere is a tax amount of 12% \n", "\n", "•\tFind the total bill after availing the discount and applying the tax\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "db1beb37-627b-4b88-97ba-7fc6034d3947", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter the quantity of the item: 5\n", "Enter the price of the item: 5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Initial Bill Amount: Rs. 25.0\n", "Bill after 10% discount: Rs. 22.5\n", "Tax (12%): Rs. 2.6999999999999997\n", "Total Bill Amount: Rs. 25.2\n" ] } ], "source": [ "\n", "\n", "# Read input from user\n", "quantity = int(input(\"Enter the quantity of the item: \"))\n", "price = float(input(\"Enter the price of the item: \"))\n", "\n", "# Calculate initial bill\n", "bill_amount = quantity * price\n", "print(\"\\nInitial Bill Amount: Rs.\", bill_amount)\n", "\n", "# Apply 10% discount\n", "discount = 0.10 * bill_amount\n", "bill_after_discount = bill_amount - discount\n", "print(\"Bill after 10% discount: Rs.\", bill_after_discount)\n", "\n", "# Apply 12% tax\n", "tax = 0.12 * bill_after_discount\n", "total_bill = bill_after_discount + tax\n", "\n", "# Display final amount\n", "print(\"Tax (12%): Rs.\", tax)\n", "print(\"Total Bill Amount: Rs.\", total_bill)\n" ] }, { "cell_type": "markdown", "id": "3bc4afd5-9d05-4869-8c57-ce4b421b465e", "metadata": {}, "source": [ "5.\t Implement a program to calculate in how many days a work will be completed by three persons A, B and C together. A, B, C take x days, y days and z days respectively to do the job alone. The formula to calculate the number of days if they work together is xyz/(xy + yz + xz) days where x, y, and z are given as input to the program." ] }, { "cell_type": "code", "execution_count": 6, "id": "ebc48165-2b20-48f8-9be3-eeea63af885b", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter the number of days A takes to complete the work alone: 5\n", "Enter the number of days B takes to complete the work alone: 5\n", "Enter the number of days C takes to complete the work alone: 5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1.6666666666666667\n" ] } ], "source": [ "# Read input from the user\n", "x = float(input(\"Enter the number of days A takes to complete the work alone: \"))\n", "y = float(input(\"Enter the number of days B takes to complete the work alone: \"))\n", "z = float(input(\"Enter the number of days C takes to complete the work alone: \"))\n", "\n", "# Apply the formula: (x * y * z) / (x*y + y*z + x*z)\n", "days_together = (x * y * z) / (x * y + y * z + x * z)\n", "\n", "# Display result\n", "print(days_together)\n", "\n" ] }, { "cell_type": "markdown", "id": "c5123c52-7da3-4cd8-8992-96f725f23065", "metadata": {}, "source": [ "6.\t Implement a program to read two complex numbers and perform addition ,subtraction" ] }, { "cell_type": "code", "execution_count": 7, "id": "fd5895ae-1c2b-4366-8f3f-31a7541b0b59", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter the first complex number (e.g., 2+3j): 5+2j\n", "Enter the second complex number (e.g., 4+5j): 9+3j\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "First Complex Number: (5+2j)\n", "Second Complex Number: (9+3j)\n", "Addition: (14+5j)\n", "Subtraction: (-4-1j)\n" ] } ], "source": [ "\n", "# Read two complex numbers from user\n", "c1 = complex(input(\"Enter the first complex number (e.g., 2+3j): \"))\n", "c2 = complex(input(\"Enter the second complex number (e.g., 4+5j): \"))\n", "\n", "# Perform addition and subtraction\n", "sum_c = c1 + c2\n", "diff_c = c1 - c2\n", "\n", "# Display results\n", "print(\"\\nFirst Complex Number: \", c1)\n", "print(\"Second Complex Number:\", c2)\n", "print(\"Addition: \", sum_c)\n", "print(\"Subtraction:\", diff_c)\n" ] }, { "cell_type": "markdown", "id": "9f87f237-898a-4048-ab76-d0163d8afd6d", "metadata": {}, "source": [ "7.\tDevelop a program to demonstrate evolution of following arithmetic expressions?\n", "\n", "•Consider b=4, c=8, d=2,e=4,f=2\n", "\n", "•\ta=b+c/d+e*f\n", "\n", "•\ta=(b+c)/d+e*f\n", "\n", "•\ta=b+c/((d+e)*f)\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "1d0e54e7-94eb-4f0e-a3ef-a5398582a4fa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Expression 1 (a = b + c/d + e*f): 16.0\n", "Expression 2 (a = (b + c)/d + e*f): 14.0\n", "Expression 3 (a = b + c/((d + e) * f)): 4.666666666666667\n" ] } ], "source": [ "# Given values\n", "b = 4\n", "c = 8\n", "d = 2\n", "e = 4\n", "f = 2\n", "\n", "# Expression 1: a = b + c/d + e*f\n", "a1 = b + c/d + e*f\n", "print(\"Expression 1 (a = b + c/d + e*f):\", a1)\n", "\n", "# Expression 2: a = (b + c)/d + e*f\n", "a2 = (b + c)/d + e*f\n", "print(\"Expression 2 (a = (b + c)/d + e*f):\", a2)\n", "\n", "# Expression 3: a = b + c/((d + e) * f)\n", "a3 = b + c/((d + e) * f)\n", "print(\"Expression 3 (a = b + c/((d + e) * f)):\", a3)\n" ] }, { "cell_type": "markdown", "id": "80afcafd-67cc-4661-b623-311393de1d31", "metadata": {}, "source": [ "8.\t Write a Python program that takes two lists as input and concatenates them using the \"+\" operator." ] }, { "cell_type": "code", "execution_count": 9, "id": "4352736b-cd9c-4790-a2d4-f0deb517f5e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6]\n" ] } ], "source": [ "list1=[1,2,3]\n", "list2=[4,5,6]\n", "print(list1+list2)" ] }, { "cell_type": "markdown", "id": "c513a0e5-e030-49c1-af7f-5adcfd513960", "metadata": {}, "source": [ "9.Write a program to enter the marks of a student in four subjects. Then calculate the total and aggregate, and display the grade obtained by the student. If the student scores an aggregate greater than 75%, then the grade is Distinction. If aggregate is >=60 and <75, then the grade is First Division. If aggregate is >=50 and <60, then the grade is Second division. If aggregate is >=40 and <50, then the grade is third division. Else the grade is Fail. " ] }, { "cell_type": "code", "execution_count": 1, "id": "a009e210-fb09-42f1-8599-c366be45628e", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter English Marks 45\n", "Enter Math Marks 45\n", "Enter Science Marks 45\n", "Enter Hindi Marks 45\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "The total marks are 180.0\n", "The aggregate is 45.0\n", "Third division\n" ] } ], "source": [ "Eng=float(input(\"Enter English Marks\"))\n", "Math=float(input(\"Enter Math Marks\"))\n", "Sci=float(input(\"Enter Science Marks\"))\n", "Hindi=float(input(\"Enter Hindi Marks\"))\n", "total=Eng+Math+Sci+Hindi\n", "aggre=(total/4)\n", "print(\"The total marks are\",total)\n", "print(\"The aggregate is\",aggre)\n", "if aggre>=75:\n", " print(\"Distinction\")\n", "elif aggre>=60 and aggre<75:\n", " print(\"First Division\")\n", "elif aggre>=50 and aggre<60:\n", " print(\"Second Division\")\n", "elif aggre>=40 and aggre<50:\n", " print(\"Third division\")\n", "else:\n", " print(\"Fail\")" ] }, { "cell_type": "markdown", "id": "41c745d1-7c6e-479b-9b5a-083ea48957b1", "metadata": {}, "source": [ "10.\t Write a program to calculate roots of a quadratic equation. The programmer has to identify whether the roots are real, equal or imaginary " ] }, { "cell_type": "markdown", "id": "ba8f4788-01d5-4b09-be44-b1b6396ac384", "metadata": {}, "source": [ "The discriminant (d)=(b*b)-(4*a*c)\n", "\n", "If d > 0 → Roots are real and distinct\n", "\n", "If d == 0 → Roots are real and equal\n", "\n", "If d < 0 → Roots are imaginary (complex)" ] }, { "cell_type": "code", "execution_count": 2, "id": "21430ce8-50be-4209-aee5-d2c315733ec7", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter coefficient a: 8\n", "Enter coefficient b: 4\n", "Enter coefficient c: 6\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Roots are imaginary (complex).\n", "Root 1 = -0.25 + 0.82915619758885i\n", "Root 2 = -0.25 - 0.82915619758885i\n" ] } ], "source": [ "import math\n", "\n", "# Input coefficients\n", "a = float(input(\"Enter coefficient a: \"))\n", "b = float(input(\"Enter coefficient b: \"))\n", "c = float(input(\"Enter coefficient c: \"))\n", "\n", "# Calculate discriminant\n", "d = (b ** 2) - (4 * a * c)\n", "\n", "# Determine nature and calculate roots\n", "if d > 0:\n", " root1 = (-b + math.sqrt(d)) / (2 * a)\n", " root2 = (-b - math.sqrt(d)) / (2 * a)\n", " print(\"Roots are real and distinct.\")\n", " print(\"Root 1 =\", root1)\n", " print(\"Root 2 =\", root2)\n", "\n", "elif d == 0:\n", " root = -b / (2 * a)\n", " print(\"Roots are real and equal.\")\n", " print(\"Root 1 = Root 2 =\", root)\n", "\n", "else:\n", " realPart = -b / (2 * a)\n", " imagPart = math.sqrt(-d) / (2 * a)\n", " print(\"Roots are imaginary (complex).\")\n", " print(f\"Root 1 = {realPart} + {imagPart}i\")\n", " print(f\"Root 2 = {realPart} - {imagPart}i\")\n" ] }, { "cell_type": "markdown", "id": "df2090bf-cc22-484d-8a3d-ae25eee00e2e", "metadata": {}, "source": [ "## 11.\t A company decides to give bonus to all its employees on Diwali. A 5% bonus on salary is given to the male workers and 10% bonus on salary to the female workers. Write a program to enter the salary and gender of the employee. If the salary of the employee is less than Rs. 10,000 then the employee gets an extra 2% bonus on salary. Calculate the bonus that must be given to the employee and display the salary that the employee will get." ] }, { "cell_type": "code", "execution_count": null, "id": "39440432-1870-4500-bd49-607beb601d17", "metadata": {}, "outputs": [], "source": [ "# Program to calculate Diwali Bonus for employees\n", "\n", "# Input\n", "salary = float(input(\"Enter employee salary: \"))\n", "gender = input(\"Enter employee gender (M/F): \")\n", "\n", "# Initialize bonus\n", "bonus = 0\n", "\n", "# Bonus based on gender\n", "if gender.upper() == 'M':\n", " bonus = salary * 0.05 # 5% for male\n", "elif gender.upper() == 'F':\n", " bonus = salary * 0.10 # 10% for female\n", "else:\n", " print(\"Invalid gender entered!\")\n", "\n", "# Extra 2% if salary is less than 10,000\n", "if salary < 10000:\n", " bonus += salary * 0.02\n", "\n", "# Final salary after adding bonus\n", "final_salary = salary + bonus\n", "\n", "# Output\n", "print(\"\\n--- Salary Details ---\")\n", "print(\"Basic Salary : Rs.\", salary)\n", "print(\"Bonus Amount : Rs.\", bonus)\n", "print(\"Final Salary Paid : Rs.\", final_salary)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a04257fc-9c3f-406f-8c00-b67dc4c8e667", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4077382e-861d-4152-baec-c1b7f9850aa8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7735c375-6d8c-4db5-a964-6ecf6b9129cf", "metadata": {}, "source": [ "12.\t Demonstrate a program to print the sum of the series 1/12+ 1/22+1/32 +……. +1/n2. where n is taken from the user." ] }, { "cell_type": "code", "execution_count": 30, "id": "e502e72b-2303-43eb-9c2a-5ee2aad5a68c", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a number 5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1.4636111111111112\n" ] } ], "source": [ "n=int(input(\"Enter a number\"))\n", "sum=0\n", "for i in range(1,n+1):\n", " sum=sum+1/(i**2)\n", "print(sum)" ] }, { "cell_type": "code", "execution_count": 7, "id": "a96d3145-dd61-4ba3-8455-27ecf5afc5ae", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4636111111111112" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1/1+1/4+1/9+1/16+1/25" ] }, { "cell_type": "markdown", "id": "d68f910f-3912-45b7-addc-89a2ffd4a4cc", "metadata": {}, "source": [ "13.\tWrite a program to implement the below scenarios\n", "\n", "•\tSum of cubes of numbers from 1 to n using range ().\n", "\n", "•\tDisplay the numbers in descending order using range ().\n", "\n", "•\tSum of squares of even numbers from 1 to n using range ().\n", "\n", "•\tDisplay all leap years from 2000 – 2200 using range ().\n", "\t\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "76842189-db00-4624-9dce-b7046de9f2c1", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a number 5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1\n", "9\n", "36\n", "100\n", "225\n", "225\n" ] } ], "source": [ "## Sum of cubes of numbers from 1 to n using range ().\n", "n=int(input(\"Enter a number\"))\n", "sum=0\n", "for i in range(1,n+1):\n", " sum=sum+(i**3)\n", " print(sum)\n", "print(sum)" ] }, { "cell_type": "code", "execution_count": 10, "id": "f98082f7-4877-4f9f-a97b-ddc4665356ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 " ] } ], "source": [ "## Display the numbers in descending order using range ().\n", "for i in range(50,0,-1):\n", " print(i,end=\" \")" ] }, { "cell_type": "code", "execution_count": 16, "id": "57946246-9c81-41b2-bd2b-9af9d283a640", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a number: 5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "4\n", "20\n", "20\n" ] } ], "source": [ "## Sum of squares of even numbers from 1 to n using range ().\n", "n=int(input(\"Enter a number:\"))\n", "sum=0\n", "for i in range(1,n+1):\n", " if i%2==0:\n", " sum=sum+(i**2)\n", " print(sum)\n", "print(sum)" ] }, { "cell_type": "code", "execution_count": null, "id": "efcdc4b1-0650-4d30-9475-3fdbb69fe882", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 14, "id": "d439276d-715b-4648-9e23-cb07ac79f264", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 2152 2156 2160 2164 2168 2172 2176 2180 2184 2188 2192 2196 " ] } ], "source": [ "## Display all leap years from 2000 – 2200 using range ().\n", "for i in range(2000,2201):\n", " if((i%400==0) or (i%4==0 and i%100!=0)):\n", " print(i,end=\" \")" ] }, { "cell_type": "markdown", "id": "13aa151d-7694-4f2b-beae-8a227eb64d87", "metadata": {}, "source": [ "Write a program to print the below patterns:\n", "\n", "## Pattern 1\n", "\t\t\t\t\n", "1\n", "\n", "2 3\n", "\n", "4 5 6\n", "\n", "7 8 9 10\n", "\n", "11 12 13 14 15\n", "\n", "## Pattern 2\n", "\n", "\n", "1\n", "\n", "2 1\n", "\n", "3 2 1\n", "\n", "4 3 2 1\n", "\n", "5 4 3 2 1\n", "\n", "## Pattern 3\n", "\t\n", "5 4 3 2 1\n", "\n", "4 3 2 1\n", "\n", "3 2 1\n", "\n", "2 1\n", "\n", "1\t\n", "## Pattern 4\n", " * \n", " * *\n", " * * *\n", " * * * *\n", " * * * * *\t \n", "\n", " ## Pattern 5\n", "\n", " \n", " 1\n", " \n", " 1 2 \n", " \n", " 1 2 3\n", "\n", "1 2 3 4 \n", "\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "88bee81e-97b8-4880-a239-94201e232fb0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 \n", "2 3 \n", "4 5 6 \n", "7 8 9 10 \n", "11 12 13 14 15 \n" ] } ], "source": [ "'''Pattern 1\n", "\t\t\t\t\n", "1\n", "\n", "2 3\n", "\n", "4 5 6\n", "\n", "7 8 9 10\n", "\n", "11 12 13 14 15'''\n", "\n", "# outer loop controls the number of rows, and the inner loop controls the number of values printed in each row.\n", "\n", "num=1\n", "for i in range(1,6): # no.of rows\n", " for j in range(0,i):\n", " print(num,end=\" \")\n", " num=num+1\n", " print()\n", "\n" ] }, { "cell_type": "code", "execution_count": 25, "id": "997b5d37-3b02-409b-b3a3-5d746382ec0b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 \n", "2 1 \n", "3 2 1 \n", "4 3 2 1 \n", "5 4 3 2 1 \n" ] } ], "source": [ "''' pattern 2\n", "1\n", "\n", "2 1\n", "\n", "3 2 1\n", "\n", "4 3 2 1\n", "\n", "5 4 3 2 1'''\n", "\n", "num=1\n", "for i in range(1,6):\n", " for j in range(i,0,-1):\n", " print(j,end=\" \")\n", " print()\n", " " ] }, { "cell_type": "code", "execution_count": 26, "id": "fa3dee47-ec66-4eb4-8863-33179d84502e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 4 3 2 1 \n", "4 3 2 1 \n", "3 2 1 \n", "2 1 \n", "1 \n" ] } ], "source": [ "''' ## Pattern 3\n", "\t\n", "5 4 3 2 1\n", "\n", "4 3 2 1\n", "\n", "3 2 1\n", "\n", "2 1\n", "\n", "1\t'''\n", "\n", "for i in range(5,0,-1):\n", " for j in range(i,0,-1):\n", " print(j,end=\" \")\n", " print()" ] }, { "cell_type": "code", "execution_count": 27, "id": "894c6ecc-62c9-4a69-bf73-31df0f3469bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Pattern 4:\n", " * \n", " * * \n", " * * * \n", " * * * * \n", "* * * * * \n" ] } ], "source": [ "''' ## Pattern 4\n", " * \n", " * *\n", " * * *\n", " * * * *\n", " * * * * *\t'''\n", "print(\"\\nPattern 4:\")\n", "for i in range(1, 6):\n", " print(\" \" * (5 - i), end=\"\")\n", " print(\"* \" * i)" ] }, { "cell_type": "code", "execution_count": 28, "id": "5651d7cb-cdb3-445f-bad4-1575f18d3224", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Pattern 5:\n", " 1 \n", " 1 2 \n", " 1 2 3 \n", "1 2 3 4 \n" ] } ], "source": [ "print(\"\\nPattern 5:\")\n", "for i in range(1, 5):\n", " print(\" \" * (4 - i) * 2, end=\"\") # Extra spacing for alignment\n", " for j in range(1, i + 1):\n", " print(j, end=\" \")\n", " print()" ] }, { "cell_type": "code", "execution_count": 5, "id": "d9cc08d5-fe40-4734-9dfe-8951efca981a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "1 \n", "2 3 \n", "4 5 6 \n", "7 8 9 10 \n", "11 12 13 14 15 \n" ] } ], "source": [ "num=1\n", "for i in range(1,7):\n", " for j in range(1,i):\n", " print(num,end=\" \")\n", " num=num+1\n", " print()" ] }, { "cell_type": "code", "execution_count": 6, "id": "8fbebe25-8b9b-4799-ad58-cc0084008af3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "* \n", "* * \n", "* * * \n", "* * * * \n", "* * * * * \n" ] } ], "source": [ "num=1\n", "for i in range(1,7):\n", " for j in range(1,i):\n", " print(\"*\",end=\" \")\n", " num=num+1\n", " print()" ] }, { "cell_type": "code", "execution_count": 7, "id": "aa70a422-b458-4165-813c-3bf2990ec71f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 \n", "2 2 \n", "3 3 3 \n", "4 4 4 4 \n", "5 5 5 5 5 \n", "6 6 6 6 6 6 \n" ] } ], "source": [ "num=1\n", "for i in range(1,7):\n", " for j in range(i):\n", " print(i,end=\" \")\n", " num=num+1\n", " print()" ] }, { "cell_type": "code", "execution_count": 9, "id": "6a4e1002-e82c-40b8-83a1-8009a498b83d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 \n", "0 1 \n", "0 1 2 \n", "0 1 2 3 \n", "0 1 2 3 4 \n", "0 1 2 3 4 5 \n" ] } ], "source": [ "num=1\n", "for i in range(1,7):\n", " for j in range(i):\n", " print(j,end=\" \")\n", " num=num+1\n", " print()" ] }, { "cell_type": "code", "execution_count": 14, "id": "7074d944-5d58-4d82-b934-a9912306d5c1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 4 3 2 1 \n", "4 3 2 1 \n", "3 2 1 \n", "2 1 \n", "1 \n" ] } ], "source": [ "\n", "for i in range(5,0,-1):\n", " for j in range(i,0,-1):\n", " print(j,end=\" \")\n", " print()" ] }, { "cell_type": "code", "execution_count": 23, "id": "886cd017-8c29-450d-96a2-ccbe982eb3b0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 \n", "2 1 \n", "3 2 1 \n", "4 3 2 1 \n", "5 4 3 2 1 \n" ] } ], "source": [ "for i in range(1,6):\n", " for j in range(i,0,-1):\n", " print(j,end=\" \")\n", " print()" ] }, { "cell_type": "code", "execution_count": 25, "id": "5ec1a0ac-c22b-4535-8eb2-4b5aa432ef6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 4 3 2 1 \n", "4 3 2 1 \n", "3 2 1 \n", "2 1 \n", "1 \n" ] } ], "source": [ "for i in range(5,0,-1):\n", " for j in range(i,0,-1):\n", " print(j,end=\" \")\n", " print()\n", " " ] }, { "cell_type": "code", "execution_count": 53, "id": "f77056b1-2dd1-43fa-9746-b9297859b3b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__getnewargs__',\n", " '__getstate__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'capitalize',\n", " 'casefold',\n", " 'center',\n", " 'count',\n", " 'encode',\n", " 'endswith',\n", " 'expandtabs',\n", " 'find',\n", " 'format',\n", " 'format_map',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isascii',\n", " 'isdecimal',\n", " 'isdigit',\n", " 'isidentifier',\n", " 'islower',\n", " 'isnumeric',\n", " 'isprintable',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper',\n", " 'join',\n", " 'ljust',\n", " 'lower',\n", " 'lstrip',\n", " 'maketrans',\n", " 'partition',\n", " 'removeprefix',\n", " 'removesuffix',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rpartition',\n", " 'rsplit',\n", " 'rstrip',\n", " 'split',\n", " 'splitlines',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'title',\n", " 'translate',\n", " 'upper',\n", " 'zfill']" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(str)" ] }, { "cell_type": "code", "execution_count": 55, "id": "f504688d-5b6d-4d5d-b113-984e6c729920", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "## capitalize() returns first letter into capital letter\n", "s=\"hello\"\n", "print(s.capitalize())" ] }, { "cell_type": "code", "execution_count": 57, "id": "a61a4289-12e4-4488-bac4-bf13d4c3f1fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "## casefold() converts uppercase characters to lowercase\n", "s=\"HELLO\"\n", "print(s.casefold())" ] }, { "cell_type": "code", "execution_count": 148, "id": "6f6f534c-c56d-4e22-8d0d-aab085cafdc8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " hello \n" ] } ], "source": [ "## center() will give padding space from starting\n", "s=\"hello\"\n", "print(s.center(10))" ] }, { "cell_type": "code", "execution_count": 62, "id": "750b643e-c7ea-4b1d-93da-faf520fda29b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "# count() will return the frequency of the character\n", "s=\"hellooooo\"\n", "print(s.count(\"o\"))" ] }, { "cell_type": "code", "execution_count": 152, "id": "0fad941d-66de-4d48-9a13-1b892b84f839", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'hello'\n" ] } ], "source": [ "## encode() method converts string to bytes\n", "s=\"hello\"\n", "encode=s.encode(\"utf-8\")\n", "print(encode)" ] }, { "cell_type": "code", "execution_count": 84, "id": "a6db2daa-85ff-44c7-86d2-60afe55d49bb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## endswith() checks whether the string is ending with particular character\n", "s=\"hello\"\n", "print(s.endswith(\"o\"))" ] }, { "cell_type": "code", "execution_count": 71, "id": "6fcee3c5-106a-46ee-85e7-8d8d9116664d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Name age salary\n" ] } ], "source": [ "## expandtabs() will replace \\t with tab spaces\n", "s=\"Name\\tage\\tsalary\"\n", "print(s.expandtabs())" ] }, { "cell_type": "code", "execution_count": 2, "id": "c9366cf8-2bb6-4ab1-9521-2a901f0b68fa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "## find() will return index positions if string is present at particular place \n", "s=\"I love India\"\n", "print(s.find(\"India\"))" ] }, { "cell_type": "code", "execution_count": 6, "id": "ded9158f-5fa2-4726-afc3-dc56b44c698e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my name is bhavya and age is 31 and my friend name is bhavya\n" ] } ], "source": [ "## format method will substitute the selected string\n", "name=\"bhavya\"\n", "age=31\n", "print(\"my name is {} and age is {} and my friend name is {}\".format(name,age,name))" ] }, { "cell_type": "code", "execution_count": 76, "id": "78203392-64fb-4260-a854-5a970781b09a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my name is bhavya and age is 31 bhavya 31\n" ] } ], "source": [ "name=\"bhavya\"\n", "age=31\n", "print(f\"my name is {name} and age is {age}\",name,age)" ] }, { "cell_type": "code", "execution_count": 85, "id": "2b42ca3f-b189-40cd-b949-56ad50bc73ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is bhavya and age is 31\n" ] } ], "source": [ "## format_map() is used for dictionaries to place values by passing keys\n", "s={\"name\":\"bhavya\",\"age\":31}\n", "x=\"My name is {name} and age is {age}\"\n", "print(x.format_map(s))" ] }, { "cell_type": "code", "execution_count": 86, "id": "5d7dd917-71c6-4b49-9383-7035219bf150", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Bhavya and I am 31 years old.\n" ] } ], "source": [ "info = {'name': 'Bhavya', 'age': 31}\n", "text = \"My name is {name} and I am {age} years old.\"\n", "print(text.format_map(info))\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "ef2415f6-b5b1-400d-b4c4-78194ed0d7ed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "## index() is used to get the index position of a particular character\n", "s=\"python\"\n", "print(s.index(\"n\"))" ] }, { "cell_type": "code", "execution_count": 8, "id": "acc0bffa-073d-420b-b0a4-148190c9b1dd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isalnum() is used to check whether the string contains both alphabets and numbers\n", "s=\"abcd123\"\n", "print(s.isalnum())" ] }, { "cell_type": "code", "execution_count": 9, "id": "525b8232-dc39-495f-9340-eb289876290f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "## isalpha() is used to check whether string contains only alphabets\n", "s=\"abcd\"\n", "print(s.isalpha())" ] }, { "cell_type": "code", "execution_count": 92, "id": "b6f313a3-1a83-434c-a514-b7cfc76f0129", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isascii() method is used to check whether it contains valid ascii characters range(character code < 128)\n", "text = \"Hello123!@#\"\n", "print(text.isascii())\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "05e4a39b-6009-45b0-ae7a-c4ef622a1e54", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# isdecimal() to check whether the string contains only digits or not \n", "num = \"2025\"\n", "print(num.isdecimal()) " ] }, { "cell_type": "code", "execution_count": 5, "id": "f287b98e-89f8-48d9-8e54-0014e9d14ffb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isdigit() returns True if our string contains digits and superscripts\n", "value = \"²34\"\n", "print(value.isdigit()) \n" ] }, { "cell_type": "code", "execution_count": 6, "id": "d7f02f35-55e5-4d52-9fe1-60a5e02d008b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isnumeric() returns True if our string contains digits and mathematical functions\n", "fraction = \"⅕\"\n", "print(fraction.isnumeric()) " ] }, { "cell_type": "code", "execution_count": 7, "id": "d14860ae-c00f-4057-a1a8-baeb1e1b1ea9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isidentifier() returns True if our string is an valid identifier\n", "identifier = \"my_var1\"\n", "print(identifier.isidentifier()) " ] }, { "cell_type": "code", "execution_count": 8, "id": "f89f2c8a-be9b-4335-86f3-46173f86de68", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## islower() If the string contains only lower case letters it returns True\n", "text = \"python\"\n", "print(text.islower()) " ] }, { "cell_type": "code", "execution_count": 9, "id": "4ac78b1b-6fa8-491f-8e45-3c5a31604ac6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isupper() If the string contains only upper case letters it returns True\n", "text = \"PYTHON\"\n", "print(text.isupper()) \n" ] }, { "cell_type": "code", "execution_count": 12, "id": "72bf3303-40ec-4b52-b446-0b80d617e078", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## istitle() if the word starts with uppercase characters\n", "text = \"Hello World\"\n", "print(text.istitle()) \n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "0739d416-6f0f-419a-9600-e93a535598c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## isprintable() if all characters are printable\n", "text = \"Welcome!\"\n", "print(text.isprintable()) \n" ] }, { "cell_type": "code", "execution_count": 2, "id": "4bf7e8de-4f9d-4ad7-ac43-8463e03478eb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "s=\"python\"\n", "for i in range(0,len(s)):\n", " print(s[i])" ] }, { "cell_type": "code", "execution_count": 3, "id": "d663dd6c-0b23-466c-85ba-a0bd0e9d3515", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "for i in s:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "id": "b147090e-c87c-4e79-a697-c29b26bffe2c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 17, "id": "bb6df7ec-afd1-4b1b-bd81-c9ae5b096500", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true, "source_hidden": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'mango']\n", "\n" ] } ], "source": [ "## split() Breaks a string into a list of words (or substrings) based on a separator (like space or comma).\n", "texxt = \"apple@banana@mango\"\n", "result = text.split(\"@\")\n", "print(result)\n", "print(type(result))\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "1425be6d-d76d-4042-affa-3609e31c046f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple, banana, mango\n", "\n" ] } ], "source": [ "## join() takes a list of strings and joins them into one single string, using the given separator.\n", "fruits = ['apple', 'banana', 'mango']\n", "result = \", \".join(fruits)\n", "print(result)\n", "print(type(result))\n", "\n" ] }, { "cell_type": "code", "execution_count": 98, "id": "69ac8aba-cf9e-45d9-a838-fa0fb0d86b6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "## strip() – Removes leading and trailing whitespace (or characters)\n", "text = \" Hello \"\n", "print(text.strip())\n" ] }, { "cell_type": "code", "execution_count": 99, "id": "d9358dac-991d-4b43-9da3-b95bde0c2119", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Hello\n" ] } ], "source": [ "text = \" Hello \"\n", "print(text.rstrip())\n" ] }, { "cell_type": "code", "execution_count": 100, "id": "c35a1e7f-ea04-4fe5-a07b-3bb40f94d8bd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello \n" ] } ], "source": [ "text = \" Hello \"\n", "print(text.lstrip())\n" ] }, { "cell_type": "code", "execution_count": 19, "id": "bbe6e1dc-9781-47b6-9beb-d50255368818", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "## swapcase\n", "a=\"HELLO\"\n", "print(a.swapcase())" ] }, { "cell_type": "code", "execution_count": 102, "id": "6e4df54f-3b89-4607-b9cc-44f74221b277", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "happy\n" ] } ], "source": [ "## removeprefix\n", "## Removes a prefix from the string, if it exists.\n", "text = \"unhappy\"\n", "print(text.removeprefix(\"un\"))\n" ] }, { "cell_type": "code", "execution_count": 103, "id": "68f38f89-3d0c-4ff3-8d23-31d793cbaf71", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "filename\n" ] } ], "source": [ "## removesuffix()\n", "## Removes a suffix from the string, if it exists.\n", "text = \"filename.txt\"\n", "print(text.removesuffix(\".txt\"))\n" ] }, { "cell_type": "code", "execution_count": 105, "id": "ef6d8f4d-d296-4a0c-b8b9-8b4f194e54f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h2ll4\n" ] } ], "source": [ "## maketrans() and translate()\n", "## maketrans() creates a translation table; translate() applies it to the string.\n", "table = str.maketrans(\"aeiou\", \"12345\")\n", "text = \"hello\"\n", "print(text.translate(table))\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "6ad81707-c0b8-4ce7-a9ad-b4490fb81faf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "000045\n" ] } ], "source": [ "## zfill()\n", "## Pads the string from the left with zeros (0) until it reaches the specified width.\n", "num = \"45\"\n", "print(num.zfill(6))\n" ] }, { "cell_type": "code", "execution_count": 107, "id": "acc1bde4-2515-41c4-97e7-80f6c1720320", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Line1', 'Line2', 'Line3']\n" ] } ], "source": [ "## splitlines()\n", "## Splits a multiline string into a list of lines.\n", "text = \"Line1\\nLine2\\nLine3\"\n", "print(text.splitlines())\n" ] }, { "cell_type": "code", "execution_count": 108, "id": "f27ee7ff-0d26-449d-b5f0-6a8636e8a61d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python Is Fun\n" ] } ], "source": [ "## title()\n", "## Converts the first character of each word to uppercase.\n", "text = \"python is fun\"\n", "print(text.title())\n" ] }, { "cell_type": "code", "execution_count": 109, "id": "ca4e3a14-45d7-4c28-b0f5-96b8e9dadce0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I love oranges\n" ] } ], "source": [ "## replace()\n", "# Replaces all occurrences of a substring with another.\n", "text = \"I love apples\"\n", "print(text.replace(\"apples\", \"oranges\"))\n" ] }, { "cell_type": "code", "execution_count": 25, "id": "7509d073-fa28-4cb3-9694-494f2729cb9a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "## Iterating a loop through a string\n", "s=\"python\"\n", "for i in range(0,len(s)):\n", " print(s[i])" ] }, { "cell_type": "code", "execution_count": 26, "id": "e1ed1fe9-ddd9-4b3f-a83a-80b00a184778", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'p'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": 23, "id": "ed2be665-e253-491b-bd9d-f6890896224c", "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "6\n" ] } ], "source": [ "s=\"python\"\n", "print(s.index(\"n\"))\n", "print(len(s))" ] }, { "cell_type": "code", "execution_count": 8, "id": "e14233e8-ad58-4dae-b174-d740ae395491", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ " python@123language@456&\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "python@language@&\n" ] } ], "source": [ "s=input().lower()\n", "j=\"0123456789\"\n", "for i in s:\n", " if i in j:\n", " s=s.replace(i,\"\")\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 28, "id": "11cde923-d465-4456-8c12-9adabb94b91a", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter input string: hello\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "Enter input string: goodafternoon\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Final string: GOODAFTERNOON\n" ] } ], "source": [ "## programs using strings\n", "## Count characters in a string\n", "s=input(\"Enter input string: \")\n", "count=0\n", "for i in range(0,len(s)):\n", " count=count+1\n", "print(count)\n", "\n", "## Convert string to uppercase\n", "k=input(\"Enter input string: \")\n", "j=k.upper()\n", "print(\"Final string:\",j)\n", "## Check if string is a palindrome" ] }, { "cell_type": "code", "execution_count": 32, "id": "bea0666c-2b85-4094-a118-021bc1215aa0", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter the input string: PROGRAMME\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "## Count vowels in a string\n", "s=input(\"Enter the input string:\").lower()\n", "a=\"aeiou\"\n", "count=0\n", "for i in range(0,len(s)):\n", " if s[i] in a:\n", " count=count+1\n", "print(count)" ] }, { "cell_type": "code", "execution_count": 33, "id": "9835cfe4-4b9c-4a48-bc8e-866f1928cf4e", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter input string: abcd123@1as\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "abcd@as\n" ] } ], "source": [ "## ## Remove all digits from a string\n", "s=input(\"Enter input string: \")\n", "a=\"0123456789\"\n", "for i in a:\n", " s=s.replace(i,\"\")\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 34, "id": "78240c03-86dc-4b96-b340-c43e37aa8910", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcd@as\n" ] } ], "source": [ "s=s.translate(str.maketrans(\"\",\"\",\"0123456789\"))\n", "print(s)" ] }, { "cell_type": "markdown", "id": "75e425e5-9b88-488d-a66e-f31ec4d7b407", "metadata": {}, "source": [ "## 10. Write a python program without using the built in functions to find the length of the string, reverse the string." ] }, { "cell_type": "code", "execution_count": 38, "id": "f83a8da6-60cf-47aa-b619-61c63cd57cfb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "4\n", "3\n", "2\n", "1\n" ] } ], "source": [ "for i in range(5,0,-1):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 9, "id": "13949a39-c703-47eb-ab42-06282a910cda", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter input string: python\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "6\n", "nohtyp\n" ] } ], "source": [ "s=input(\"Enter input string: \")\n", "count=0\n", "for i in range(0,len(s)):\n", " count=count+1\n", "print(count)\n", "## reversing a string\n", "rev=\"\"\n", "for i in range(count-1,-1,-1):\n", " rev=rev+s[i]\n", "print(rev)" ] }, { "cell_type": "markdown", "id": "f7271849-0f71-476c-8234-7a552fce45bf", "metadata": {}, "source": [ "## 11. Program to arrange string characters such that lowercase letters come first" ] }, { "cell_type": "code", "execution_count": 1, "id": "e19e4582-b2b3-4248-840d-5a2d852e18f7", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a string: nnmmAASDDDDDGFCFCaaaacgfcf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Arranged string: nnmmaaaacgfcfAASDDDDDGFCFC\n" ] } ], "source": [ "# Input string\n", "s = input(\"Enter a string: \")\n", "\n", "# Separate lowercase and other characters\n", "lowercase = \"\"\n", "others = \"\"\n", "\n", "for ch in s:\n", " if ch.islower(): # check if lowercase\n", " lowercase += ch\n", " else:\n", " others += ch\n", "\n", "# Combine results\n", "result = lowercase + others\n", "\n", "# Output\n", "print(\"Arranged string:\", result)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "27c9f49a-e7b3-4af4-97a9-df0d4f29a451", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "00916982-e011-4419-b1b7-4a7d442b6636", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2b56f882-8246-4a08-9208-8f57a7c2500e", "metadata": {}, "source": [ "## Regular Expressions" ] }, { "cell_type": "markdown", "id": "1440cce1-1155-4997-8be6-9f4e43dc0eb9", "metadata": {}, "source": [ "| Method | Description | Returns |\n", "|--------------|---------------------------------------|-----------------------------|\n", "| `re.match()` | Matches pattern at the **start** | Match object or `None` |\n", "| `re.search()` | Finds **first occurrence** anywhere | Match object or `None` |\n", "| `re.findall()` | Returns **all matches** | List of matches |\n", "| `re.finditer()` | Returns **iterator of match objects** | Iterator |\n", "| `re.fullmatch()` | Matches **entire string** | Match object or `None` |\n", "| `re.split()` | Splits string based on pattern | List of substrings |\n", "| `re.sub()` | Replaces matches with a new string | Modified string |\n", "| `re.subn()` | Same as `re.sub()`, but returns count | (Modified string, Count) |\n", "| `re.compile()` | Precompiles a regex pattern | Compiled regex object |\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "04cf3fa6-315f-45c5-b12a-e37b16b5b999", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Match found: World\n" ] } ], "source": [ "## re.match() checks the first occurance of the string\n", "## re.match(pattern,inputstring)\n", "## When you need to check if a string starts with a pattern\n", "import re\n", "\n", "text = \"World World Hello\"\n", "pattern=\"World\"\n", "match = re.match(pattern, text)\n", "\n", "if match:\n", " print(\"Match found:\", match.group())\n", "else:\n", " print(\"No match found\")\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "b406882b-6caf-4248-a9b5-b4aebbea9d41", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Match found at position: is\n" ] } ], "source": [ "## re.search(pattern, string)Searches for the first occurrence of the pattern anywhere in the string.\n", "## Returns a match object if found; otherwise, returns None.\n", "import re\n", "\n", "text = \"Python is awesome and is a great language!\"\n", "search = re.search(\"is\", text)\n", "\n", "if search:\n", " print(\"Match found at position:\", search.group())\n", "else:\n", " print(\"No match found\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "49f9ce8d-a757-4a65-a9aa-3f3fd991597f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "All occurrences: ['101', '101', '101']\n", "\n" ] } ], "source": [ "## re.findall(pattern, string)Returns a list of all occurrences of the pattern in the string.\n", "\n", "## Returns a list of all matches.When you only need the matched text\n", "import re\n", "\n", "text = \"apple banana 101 apple orange 202 apple 101 apple 101\"\n", "matches = re.findall(\"101\", text)\n", "print(\"All occurrences:\", matches) \n", "print(type(matches))" ] }, { "cell_type": "code", "execution_count": 13, "id": "1d94be34-83fa-4093-9994-118dd8edd919", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found number: 101\n", "Found number: 101\n", "\n" ] } ], "source": [ "## re.finditer(pattern, string)Returns an iterator yielding match objects for all occurrences.\n", "## Returns an iterator of match objects.When you need match positions (start(), end(), etc.)\n", "import re\n", "\n", "text = \"Python 101 and Java 202 Java 202 Python 101\"\n", "matches = re.finditer(\"101\", text) # Find all numbers\n", "for match in matches:\n", " print(\"Found number:\",match.group())\n", "\n", "print(type(matches))" ] }, { "cell_type": "code", "execution_count": 14, "id": "2a37d2bc-a7d7-45a9-abb2-96e41af4dddd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I like dogs. Cats are cute.\n" ] } ], "source": [ "## re.sub(pattern, replacement, string)Replaces occurrences of the pattern with a specified replacement.\n", "import re\n", "\n", "text = \"I like cats. Cats are cute.\"\n", "new_text = re.sub(\"cats\", \"dogs\", text, flags=re.IGNORECASE)\n", "\n", "print(new_text) \n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "2408d729-cd15-4880-a689-287c2774b603", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Modified Text: Java is awesome. Java is powerful.\n", "Replacements Made: 2\n" ] } ], "source": [ "## re.subn() → Replace and Get Replacement \n", "## Count=re.subn(pattern, replacement, string)\n", "## Works like re.sub(), but also returns the number of replacements made.\n", "import re\n", "\n", "text = \"Python is awesome. Python is powerful.\"\n", "\n", "new_text,count = re.subn('Python', 'Java', text)\n", "\n", "print(\"Modified Text:\", new_text)\n", "print(\"Replacements Made:\", count)" ] }, { "cell_type": "code", "execution_count": 16, "id": "e88cc07a-23d9-4d6e-b4f5-2bfc73965946", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'orange|grape']\n" ] } ], "source": [ "## re.split(pattern, string) Splits a string at each occurrence of the pattern.\n", "import re\n", "\n", "text = \"apple,banana;orange|grape\"\n", "split_text = re.split(\"[,;]\", text)\n", "\n", "print(split_text) \n" ] }, { "cell_type": "code", "execution_count": 19, "id": "bfa58c7a-1b05-41d8-924a-f072c1d1244c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid Username\n" ] } ], "source": [ "## re.fullmatch() → Match the Entire String.re.fullmatch(pattern, string)\n", "## Checks if the entire string matches the pattern. If even one character doesn’t match, it returns None.\n", "text = \"Python123@\"\n", "\n", "match = re.fullmatch('Python123', text)\n", "\n", "if match:\n", " print(\"Valid Username\")\n", "else:\n", " print(\"Invalid Username\")\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "53b1afea-00aa-486c-a681-5d9ee0cf19d1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid Username\n" ] } ], "source": [ "text = \"Python123!\"\n", "\n", "\n", "match = re.fullmatch(r'\\w+', text)\n", "\n", "if match:\n", " print(\"Valid Username\")\n", "else:\n", " print(\"Invalid Username\")\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "0fbbda12-fe3f-400d-8c00-0047f0b6a054", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1234\n" ] } ], "source": [ "## Compiling Regular Expressions\n", "## For performance improvement, you can compile regex patterns:\n", "import re\n", "text = \"Order 1234 is ready.\"\n", "pattern = re.compile(\"1234\")\n", "match = pattern.search(text)\n", "print(match.group()) \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0c853c62-0a5b-418e-a41e-4e0eef25f964", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b7e19c83-b0cd-481a-ae19-bee1ab631e73", "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e9128671-8a31-4497-91e4-2d255e840b23", "metadata": {}, "source": [ "| Symbol | Meaning | Example Pattern | Matches | Does NOT Match |\n", "|--------|---------|----------------|---------|----------------|\n", "| `.` | Matches **any character except newline** | `a.b` | `acb`, `a7b` | `ab` |\n", "| `^` | Matches the **beginning** of a string | `^Hello` | `Hello world` | `world Hello` |\n", "| `$` | Matches the **end** of a string | `world$` | `Hello world` | `world Hello` |\n", "| `*` | Matches **0 or more repetitions** | `ca*t` | `ct`, `cat`, `caaat` | — |\n", "| `+` | Matches **1 or more repetitions** | `ca+t` | `cat`, `caaat` | `ct` |\n", "| `?` | Matches **0 or 1 occurrences** | `ca?t` | `ct`, `cat` | `caaat` |\n", "| `{n}` | Matches **exactly n occurrences** | `a{3}` | `aaa` | `aa`, `aaaa` |\n", "| `{n,}` | Matches **n or more occurrences** | `a{2,}` | `aa`, `aaa`, `aaaa` | `a` |\n", "| `{n,m}` | Matches **between n and m occurrences** | `a{2,4}` | `aa`, `aaa`, `aaaa` | `a`, `aaaaa` |\n", "| `[]` | Matches **any one character** inside brackets | `[aeiou]` | `a`, `e`, `i`, `o`, `u` | `b`, `c` |\n", "| `\\|` | OR operator | `cat\\|dog` | `cat`, `dog` | `bat` |\n", "| `\\` | Escape special characters | `\\.` | `.` (dot character) | — |\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "70210425-381e-453c-b7cb-14a610975344", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['acb', 'a7b', 'a\\tb']\n" ] } ], "source": [ "## . symbol accepts any one character except \\n\n", "import re\n", "\n", "print(re.findall(\"a.b\", \"acb a7b a\\nb a\\tb a777778b\"))" ] }, { "cell_type": "code", "execution_count": 26, "id": "20e6d281-46ae-4cff-8815-b0ce512249a2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Hello']\n" ] } ], "source": [ "## ^ symbol accepts only the starting of the string\n", "print(re.findall(r\"^Hello\", \"Hello world\"))" ] }, { "cell_type": "code", "execution_count": 27, "id": "46f2edb5-b4d1-44fe-98f1-75e48bb97e0f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "## $ symbol accepts the pattern at the end of the string\n", "print(re.findall(r\"world$\", \"worldHello\"))" ] }, { "cell_type": "code", "execution_count": 13, "id": "078a1ed5-7c7f-476f-a1d5-d2153a002d91", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ct', 'cat', 'caaat']\n" ] } ], "source": [ "## * symbol represents 0 or more representations\n", "## will not accept if anyother character is replaced with pattern\n", "print(re.findall(r\"ca*t\", \"ct cat caaat cbt\"))" ] }, { "cell_type": "code", "execution_count": 28, "id": "0948ee76-35c2-4ee5-a0b2-ae7882c0780b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['cat', 'caaat']\n" ] } ], "source": [ "## + symbol will accept the one or more repetitions of pattern\n", "## will not accept 0 repetitions of pattern\n", "print(re.findall(r\"ca+t\", \"ct cat caaat cbt\")) " ] }, { "cell_type": "code", "execution_count": 20, "id": "dcf34bd8-7e7f-4181-ad9a-ea80c2fdc3c5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ct', 'cat']\n" ] } ], "source": [ "## ? symbol accepts 0 or 1 occurances\n", "\n", "print(re.findall(r\"ca?t\", \"ct cat caaat\"))" ] }, { "cell_type": "code", "execution_count": 21, "id": "9a5a2224-e45c-4013-9922-0cd15ae966f3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['aaa', 'aaa']\n" ] } ], "source": [ "#{n} matches exactly n number of occurances\n", "## n represents number or length of the pattern\n", "print(re.findall(r\"a{3}\", \"aa aaa aaaa\")) " ] }, { "cell_type": "code", "execution_count": 22, "id": "158002a2-6a9d-437c-82dd-89c3834268af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['aa', 'aaa', 'aaaa']\n" ] } ], "source": [ "#{n,} start matching from n to infinity\n", "\n", "print(re.findall(r\"a{2,}\", \"a aa aaa aaaa\"))" ] }, { "cell_type": "code", "execution_count": 23, "id": "be155f8c-a1d3-4090-b970-a3c01b8f56cc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['aa', 'aaa', 'aaaa', 'aaaa']\n" ] } ], "source": [ "#{n,m} start matching from n and end at m (with in the range)\n", "print(re.findall(r\"a{2,4}\", \"a aa aaa aaaa aaaaa\"))" ] }, { "cell_type": "code", "execution_count": 30, "id": "dd3ea62e-822e-4b36-a3b2-206e2b06d94d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'e', 'a', 'e']\n" ] } ], "source": [ "# [] matches any one charcter in the sqaure braces\n", "print(re.findall(r\"[ae]\", \"apple orange sky\"))" ] }, { "cell_type": "code", "execution_count": 31, "id": "eed6c8a6-7c82-4521-865e-47a8dda5fae1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['cat', 'dog']\n" ] } ], "source": [ "# | or symbol matches any one pattern (either or)\n", "print(re.findall(r\"cat|dog\", \"cat bat dog\"))" ] }, { "cell_type": "code", "execution_count": 26, "id": "d07809ce-61e9-4297-84c3-c8ba564f3550", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['.']\n" ] } ], "source": [ "print(re.findall(r\"\\.\", \"This is a dot.\")) " ] }, { "cell_type": "markdown", "id": "69640fbf-fbea-4317-a78f-954fde91a082", "metadata": {}, "source": [ "| Symbol | Meaning | Example Pattern | Matches | Does NOT Match |\n", "|--------|---------|-----------------|---------|----------------|\n", "| `\\d` | Matches **any digit (0-9)** | `\\d+` | `123`, `56` | `abc`, `hello` |\n", "| `\\D` | Matches **any non-digit** | `\\D+` | `hello`, `abc` | `123`, `456` |\n", "| `\\w` | Matches **any word character** (letters, numbers, underscore) | `\\w+` | `hello_123`, `abc` | `@#$`, `---` |\n", "| `\\W` | Matches **any non-word character** | `\\W+` | `@#$%`, `!!` | `abc`, `word123` |\n", "| `\\s` | Matches **whitespace** (spaces, tabs, newlines) | `\\s+` | `\" \"` (spaces), `\\t` (tab) | `abc`, `123` |\n", "| `\\S` | Matches **non-whitespace characters** | `\\S+` | `Hello`, `word123` | `\" \"`, `\\t` |\n", "| `\\b` | Matches a **word boundary** | `\\bHello\\b` | `\"Hello\"`, `\"Hello world\"` | `\"Hello123\"`, `\"SayHello\"` |\n", "| `\\B` | Matches **non-word boundary** | `\\BHello\\B` | `\"SayHello123\"` | `\"Hello\"`, `\"Hello world\"` |\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "eca6f37e-2d39-45ee-af64-0c7e6085843f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Digits: ['123', '123']\n" ] } ], "source": [ "import re\n", "\n", "text = \"Hello 123 @#$_ worldHello123\"\n", "\n", "print(\"Digits:\", re.findall(r\"\\d+\", text))\n", "\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "9f631c70-5a42-4f0c-a60d-e4488b804084", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Non-digits: ['Hello ', ' @#$_ worldHello']\n" ] } ], "source": [ "print(\"Non-digits:\", re.findall(r\"\\D+\", text))\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "36ddcbbf-9290-4b11-88da-88f4bf53737c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Word chars: ['Hello', '123', '_', 'worldHello123']\n" ] } ], "source": [ "print(\"Word chars:\", re.findall(r\"\\w+\", text))\n" ] }, { "cell_type": "code", "execution_count": 30, "id": "9ce73cf3-5b16-47d9-b684-9a148ce80e5c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Non-word chars: [' ', ' @#$', ' ']\n" ] } ], "source": [ "print(\"Non-word chars:\", re.findall(r\"\\W+\", text))\n" ] }, { "cell_type": "code", "execution_count": 35, "id": "445438f3-5d4d-47c6-90e9-50f0ab4bd2a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Whitespace: [' ', ' ', ' ', '\\t', ' ']\n" ] } ], "source": [ "\n", "text = \"Hello 123 @#$_ worldHello123\\tHelloall Hello\"\n", "print(\"Whitespace:\", re.findall(r\"\\s+\", text))\n" ] }, { "cell_type": "code", "execution_count": 36, "id": "396ad113-d7bb-4965-9555-9e41e3cebf54", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Non-whitespace: ['Hello', '123', '@#$_', 'worldHello123', 'Helloall', 'Hello']\n" ] } ], "source": [ "text = \"Hello 123 @#$_ worldHello123\\tHelloall Hello\"\n", "print(\"Non-whitespace:\", re.findall(r\"\\S+\", text))\n" ] }, { "cell_type": "code", "execution_count": 55, "id": "26c2db76-6c5c-4f4d-8c4f-0ed7cf4047b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Word boundary 'Hello': ['Hello']\n" ] } ], "source": [ "text = \"Hello 123 @#$_ worldHello123HelloallHelloall\"\n", "print(\"Word boundary 'Hello':\", re.findall(r\"\\bHello\\b\", text))\n" ] }, { "cell_type": "code", "execution_count": 56, "id": "0a8c1b00-bd6e-4e0a-82be-f4669a5b00c1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Non-word boundary 'Hello': ['Hello', 'Hello', 'Hello']\n" ] } ], "source": [ "print(\"Non-word boundary 'Hello':\", re.findall(r\"\\BHello\\B\", text))" ] }, { "cell_type": "code", "execution_count": 11, "id": "c33102c3-c3f7-4239-97a2-a938aa168c48", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Valid Email\n" ] } ], "source": [ "## Email Validation\n", "import re\n", "\n", "email = \"user@example.com\"\n", "pattern = r\"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$\"\n", "\n", "if re.match(pattern, email):\n", " print(\"Valid Email\")\n", "else:\n", " print(\"Invalid Email\")\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "5c592a1d-8890-4961-b2ec-ea110824a7f7", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a mobile number: 9989870478\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Matched\n" ] } ], "source": [ "## validate a phone number\n", "## Validate a phone number\n", "import re\n", "ph=input(\"Enter a mobile number: \")\n", "pattern=r\"^(\\+91|0)?[6-9]\\d{9}$\"\n", "match=re.match(pattern,ph)\n", "if match:\n", " print(\"Matched\")\n", "else:\n", " print(\"Not matched\")\n", " " ] }, { "cell_type": "markdown", "id": "f3ca95c0-ca7f-4f30-b9f2-df51fafb9860", "metadata": {}, "source": [ "## 12.\tWrite a program that uses regular expressions to validate dates entered by users. The program should check that the date is in a valid format, such as MM/DD/YYYY and that the month, day, and year values are within a valid range." ] }, { "cell_type": "code", "execution_count": 2, "id": "eec85dc9-41e0-4d05-ac90-9aa3dfde2a46", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a date (MM/DD/YYYY): 12/24/2025\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Valid date: 12/24/2025\n" ] } ], "source": [ "import re\n", "\n", "# Regular expression for MM/DD/YYYY format\n", "pattern = r\"^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/[0-9]{4}$\"\n", "\n", "# Function to validate date\n", "def validate_date(date):\n", " # First check format\n", " if not re.match(pattern, date):\n", " return False\n", " \n", " # Split the string manually\n", " parts = date.split('/')\n", " month = int(parts[0])\n", " day = int(parts[1])\n", " year = int(parts[2])\n", " \n", " # Days in each month\n", " if month in [1, 3, 5, 7, 8, 10, 12]:\n", " max_day = 31\n", " elif month in [4, 6, 9, 11]:\n", " max_day = 30\n", " else: # February\n", " # Check leap year\n", " if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):\n", " max_day = 29\n", " else:\n", " max_day = 28\n", " \n", " # Check valid day\n", " if day < 1 or day > max_day:\n", " return False\n", " \n", " return True\n", "\n", "# Input from user\n", "date = input(\"Enter a date (MM/DD/YYYY): \")\n", "\n", "# Output\n", "if validate_date(date):\n", " print(\"Valid date:\", date)\n", "else:\n", " print(\"Invalid date:\", date)\n" ] }, { "cell_type": "markdown", "id": "e75025ff-52c4-4787-bb5d-ec6880bd1078", "metadata": {}, "source": [ "## 13.\tWrite a program to validate a password using regular expressions using the following rules\n", "\n", "•\tAt least 8 characters long\n", "\n", "•\tContains at least one uppercase letter\n", "\n", "•\tContains at least one lowercase letter\n", "\n", "•\tContains at least one digit\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "6610c190-344b-4f42-8269-941076a4a4d9", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a password: manamBhavya123\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Valid Password \n" ] } ], "source": [ "import re\n", "\n", "# Function to validate password\n", "def validate_password(password):\n", " # Regex pattern\n", " pattern = r\"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$\"\n", " \n", " if re.match(pattern, password):\n", " return True\n", " else:\n", " return False\n", "\n", "# Input from user\n", "pwd = input(\"Enter a password: \")\n", "\n", "# Output\n", "if validate_password(pwd):\n", " print(\"Valid Password \")\n", "else:\n", " print(\"Invalid Password \")\n" ] }, { "cell_type": "markdown", "id": "a6579538-7384-4128-aad0-ebe5e8f90264", "metadata": {}, "source": [ "## 14.\tWrite a program to remove all non-alphanumeric characters from a given string using regular expressions." ] }, { "cell_type": "code", "execution_count": 5, "id": "89270d34-09a0-4825-8b38-76dcd93f5fec", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Enter a string: abvdjgj75688!@@#$%\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "String after removing non-alphanumeric characters: abvdjgj75688\n" ] } ], "source": [ "import re\n", "\n", "# Input string\n", "s = input(\"Enter a string: \")\n", "\n", "# Replace all non-alphanumeric characters with empty string\n", "result = re.sub(r'[^a-zA-Z0-9]', '', s)\n", "\n", "# Output\n", "print(\"String after removing non-alphanumeric characters:\", result)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7869e3b2-d716-48a9-9ec4-514537dbf403", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:base] *", "language": "python", "name": "conda-base-py" }, "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }