Skip to content

Instantly share code, notes, and snippets.

@magbanum
Last active April 22, 2021 17:07
Show Gist options
  • Save magbanum/030368f6da54f38cf8bb0672c50824a9 to your computer and use it in GitHub Desktop.
Save magbanum/030368f6da54f38cf8bb0672c50824a9 to your computer and use it in GitHub Desktop.
pattern_printing_python.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "pattern_printing_python.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNmtACCui/R7nUhcGHPTRo+",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/magbanum/030368f6da54f38cf8bb0672c50824a9/pattern_printing_python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4hwa57pxKET8"
},
"source": [
"# **Pattern Printing**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "B-chHc4nJ7MJ",
"outputId": "d673cef4-71b4-4615-f5d8-653c696ae900"
},
"source": [
"name = \"Shantanu\"\n",
"print(\"Hello, I'm\", name, \"and this is Parttern printing using Python\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello Shantanu\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CJcJzH2Q8KU2"
},
"source": [
"# **Simple Number Triangle Pattern**\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "e6l5cSxyKxVN",
"outputId": "64ede420-34c5-4c07-85e9-7ac847196bb1"
},
"source": [
"rows = 6\n",
"\n",
"for num in range(rows):\n",
"\n",
" for i in range(num):\n",
"\n",
" print(num, end=\" \") # print number\n",
"\n",
" # line after each row to display pattern correctly\n",
"\n",
" print(\" \")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
" \n",
"1 \n",
"2 2 \n",
"3 3 3 \n",
"4 4 4 4 \n",
"5 5 5 5 5 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "McHsu2qr8Ueu"
},
"source": [
"# **Inverted Pyramid of Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mC-YeuSCL8CY",
"outputId": "6b8b3eb7-29f5-452a-cb3d-cf1e0a1357ed"
},
"source": [
"rows = 5\n",
"\n",
"b = 0\n",
"\n",
"for i in range(rows, 0, -1):\n",
"\n",
" b += 1\n",
"\n",
" for j in range(1, i + 1):\n",
"\n",
" print(b, end=\" \")\n",
"\n",
" print(\"\\r\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 1 1 1 1 \r\n",
"2 2 2 2 \r\n",
"3 3 3 \r\n",
"4 4 \r\n",
"5 \r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Nh8hZI_e8o9U"
},
"source": [
"# **Half Pyramid Pattern of Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Tw0--2UCNupf",
"outputId": "0e4eae50-4369-493b-fd13-db45de0c6b46"
},
"source": [
"rows = 5\n",
"\n",
"for row in range(1, rows+1):\n",
"\n",
" for column in range(1, row + 1):\n",
"\n",
" print(column, end=\" \")\n",
"\n",
" print(\" \")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 \n",
"1 2 \n",
"1 2 3 \n",
"1 2 3 4 \n",
"1 2 3 4 5 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "U_DAxZNd83cb"
},
"source": [
"# **Inverted Pyramid of Descending Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jk6y5Xs8OqyR",
"outputId": "44e6fd88-ce55-44c1-886c-134cfe12bcad"
},
"source": [
"rows = 5\n",
"\n",
"for i in range(rows, 0, -1):\n",
"\n",
" num = i\n",
"\n",
" for j in range(0, i):\n",
"\n",
" print(num, end=\" \")\n",
"\n",
" print(\"\\r\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"5 5 5 5 5 \r\n",
"4 4 4 4 \r\n",
"3 3 3 \r\n",
"2 2 \r\n",
"1 \r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MRAeWP-h9K7e"
},
"source": [
"# **Inverted Pyramid of the Single Digit**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "-7Gj5O5HiBTR",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "94e3d731-22a6-473c-ae7c-c2ecc4d8eaf6"
},
"source": [
"rows = 5\n",
"\n",
"num = rows\n",
"\n",
"for i in range(rows, 0, -1):\n",
"\n",
" for j in range(0, i):\n",
"\n",
" print(num, end=\" \")\n",
"\n",
" print(\"\\r\")\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"5 5 5 5 5 \r\n",
"5 5 5 5 \r\n",
"5 5 5 \r\n",
"5 5 \r\n",
"5 \r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zW2at1mq9TRv"
},
"source": [
"# **Reverse Pyramid of Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "p16IEqMI1EZ4",
"outputId": "12d2673b-0795-48b1-efe5-00a9c691a025"
},
"source": [
"rows = 6\n",
"\n",
"for row in range(1, rows):\n",
"\n",
" for column in range(row, 0, -1):\n",
"\n",
" print(column, end=\" \")\n",
"\n",
" print(\"\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 \n",
"2 1 \n",
"3 2 1 \n",
"4 3 2 1 \n",
"5 4 3 2 1 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AH2PXgzb9bD8"
},
"source": [
"# **Inverted Half Pyramid Number Pattern**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4Ra6pO5W1OtC",
"outputId": "08dead38-37df-4a98-8767-aae4d0463f95"
},
"source": [
"rows = 5\n",
"\n",
"for i in range(rows, 0, -1):\n",
"\n",
" for j in range(1, i + 1):\n",
"\n",
" print(j, end=\" \")\n",
"\n",
" print(\"\\r\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 2 3 4 5 \r\n",
"1 2 3 4 \r\n",
"1 2 3 \r\n",
"1 2 \r\n",
"1 \r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4_-36WWu9i5f"
},
"source": [
"# **Pyramid of Natural Numbers Less Than 10**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fDtuFxG51a7_",
"outputId": "14eaf81b-ca13-48bb-8944-a5a671e906d2"
},
"source": [
"currentNumber = 1\n",
"\n",
"stop = 2\n",
"\n",
"rows = 4 # Rows you want in your pattern\n",
"\n",
"for i in range(rows):\n",
"\n",
" for column in range(1, stop):\n",
"\n",
" print(currentNumber, end=\" \")\n",
"\n",
" currentNumber += 1\n",
"\n",
" print(\"\")\n",
"\n",
" stop += 1"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 \n",
"2 3 \n",
"4 5 6 \n",
"7 8 9 10 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iRL6UKsu9sqU"
},
"source": [
"# **Reverse Pattern of Digits from 10**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xC7-5a3k11VJ",
"outputId": "e9c805ca-61ad-4cdc-c794-6b4228248666"
},
"source": [
"start = 1\n",
"\n",
"stop = 2\n",
"\n",
"currentNumber = stop\n",
"\n",
"for row in range(2, 6):\n",
"\n",
" for col in range(start, stop):\n",
"\n",
" currentNumber -= 1\n",
"\n",
" print(currentNumber, end=\" \")\n",
"\n",
" print(\"\")\n",
"\n",
" start = stop\n",
"\n",
" stop += row\n",
"\n",
" currentNumber = stop"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 \n",
"3 2 \n",
"6 5 4 \n",
"10 9 8 7 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TP9YFcnC9yfq"
},
"source": [
"# **Unique Pyramid Pattern of Digits**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pjTVQhwO2EUs",
"outputId": "99180bbe-6183-4685-a713-817601779501"
},
"source": [
"rows = 6\n",
"\n",
"for i in range(1, rows + 1):\n",
"\n",
" for j in range(1, i):\n",
"\n",
" print(j, end=\" \")\n",
"\n",
" for j in range(i, 0, -1):\n",
"\n",
" print(j, end=\" \")\n",
"\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"1 \n",
"1 2 1 \n",
"1 2 3 2 1 \n",
"1 2 3 4 3 2 1 \n",
"1 2 3 4 5 4 3 2 1 \n",
"1 2 3 4 5 6 5 4 3 2 1 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eUFCgxCC95_T"
},
"source": [
"# **Even Number Pyramid Pattern**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7-94_D704mN3",
"outputId": "a7596ad2-f637-4c64-8175-4f7002612e68"
},
"source": [
"rows = 5\n",
"\n",
"LastEvenNumber = 2 * rows\n",
"\n",
"evenNumber = LastEvenNumber\n",
"\n",
"for i in range(1, rows+1):\n",
"\n",
" evenNumber = LastEvenNumber\n",
"\n",
" for j in range(i):\n",
"\n",
" print(evenNumber, end=\" \")\n",
"\n",
" evenNumber -= 2\n",
"\n",
" print(\"\\r\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"10 \r\n",
"10 8 \r\n",
"10 8 6 \r\n",
"10 8 6 4 \r\n",
"10 8 6 4 2 \r\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "srnGdxMX9_S3"
},
"source": [
"# **Pyramid of Horizontal Tables**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0ePptW704086",
"outputId": "34cd3849-4f06-466e-c601-533600b55e84"
},
"source": [
"rows = 7\n",
"\n",
"for i in range(0, rows):\n",
"\n",
" for j in range(1, i + 1):\n",
"\n",
" print(i * j, end=\" \")\n",
"\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"1 \n",
"2 4 \n",
"3 6 9 \n",
"4 8 12 16 \n",
"5 10 15 20 25 \n",
"6 12 18 24 30 36 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0eo8aaN7-FjO"
},
"source": [
"# **Pyramid Pattern of odd Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "zQEpmVr-5KY6",
"outputId": "a7ded2ee-3121-4e60-e9ec-bc4124951088"
},
"source": [
"rows = 4\n",
"\n",
"i = 1\n",
"\n",
"while i <= rows:\n",
"\n",
" j = 1\n",
"\n",
" while j <= i:\n",
"\n",
" print((i * 2) + 1, end=\" \")\n",
"\n",
" j = j + 1\n",
"\n",
" i = i + 1\n",
"\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"3 \n",
"5 5 \n",
"7 7 7 \n",
"9 9 9 9 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Mwb7h6vX-Xch"
},
"source": [
"# **Pyramid Pattern of even Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "x_V87h2M5fBw",
"outputId": "095d365a-822e-4a68-ae33-0b874ad07e4c"
},
"source": [
"rows = 4\n",
"\n",
"i = 1\n",
"\n",
"while i <= rows:\n",
"\n",
" j = 1\n",
"\n",
" while j <= i:\n",
"\n",
" print((i * 2), end=\" \")\n",
"\n",
" j = j + 1\n",
"\n",
" i = i + 1\n",
"\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"2 \n",
"4 4 \n",
"6 6 6 \n",
"8 8 8 8 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7XLSWPGq-i-T"
},
"source": [
"# **Mirrored Pyramid (Right-angled Triangle) Pattern of Numbers**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PGu93fyK6HfO",
"outputId": "2fa51d53-c389-4c1e-d713-fbea246d5cf0"
},
"source": [
"rows = 6\n",
"\n",
"for row in range(1, rows):\n",
"\n",
" num = 1\n",
"\n",
" for j in range(rows, 0, -1):\n",
"\n",
" if j > row:\n",
"\n",
" print(\" \", end=\" \")\n",
"\n",
" else:\n",
"\n",
" print(num, end=\" \")\n",
"\n",
" num += 1\n",
"\n",
" print(\"\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
" 1 \n",
" 1 2 \n",
" 1 2 3 \n",
" 1 2 3 4 \n",
" 1 2 3 4 5 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cpo08XeV7Tx2"
},
"source": [
"# **Equilateral Triangle with Stars**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yAtpwgYr6V3l",
"outputId": "f935d536-d2a2-41f4-c7a4-f8fb2f1c896c"
},
"source": [
"print(\"Print equilateral triangle Pyramid using stars \")\n",
"\n",
"size = 7\n",
"\n",
"m = (2 * size) - 2\n",
"\n",
"for i in range(0, size):\n",
"\n",
" for j in range(0, m):\n",
"\n",
" print(end=\" \")\n",
"\n",
" m = m - 1 # decrementing m after each loop\n",
"\n",
" for j in range(0, i + 1):\n",
"\n",
" # printing full Triangle pyramid using stars\n",
"\n",
" print(\"*\", end=\" \")\n",
"\n",
" print(\" \")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Print equilateral triangle Pyramid using stars \n",
" * \n",
" * * \n",
" * * * \n",
" * * * * \n",
" * * * * * \n",
" * * * * * * \n",
" * * * * * * * \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_0cEdEEi_VRf"
},
"source": [
"# **Downward Triangle Pattern of Stars**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "W1f8g2Qb-v-V",
"outputId": "fd11bf33-c51b-4fcb-dd53-6cd9a42d763f"
},
"source": [
"rows = 5\n",
"\n",
"k = 2 * rows - 2\n",
"\n",
"for i in range(rows, -1, -1):\n",
"\n",
" for j in range(k, 0, -1):\n",
"\n",
" print(end=\" \")\n",
"\n",
" k = k + 1\n",
"\n",
" for j in range(0, i + 1):\n",
"\n",
" print(\"*\", end=\" \")\n",
"\n",
" print(\"\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
" * * * * * * \n",
" * * * * * \n",
" * * * * \n",
" * * * \n",
" * * \n",
" * \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TjGa9SwuASKR"
},
"source": [
"# **Pyramid Pattern of Stars**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6ARREhRS_Y_z",
"outputId": "34eb2070-e01f-4e14-9d5a-9e2e924371db"
},
"source": [
"rows = 5\n",
"\n",
"for i in range(0, rows):\n",
"\n",
" for j in range(0, i + 1):\n",
"\n",
" print(\"*\", end=\" \")\n",
" print(\"\\r\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"* \r\n",
"* * \r\n",
"* * * \r\n",
"* * * * \r\n",
"* * * * * \r\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment