avatarAayushi Johari

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

11647

Abstract

"hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(0 , i + 1 ): <span class="hljs-built_in">print</span>(<span class="hljs-string">"* "</span>, <span class="hljs-attribute">end</span>=<span class="hljs-string">""</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"\r"</span>) k = n - 2 <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(n , -1, -1): <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(k , 0 , -1): <span class="hljs-built_in">print</span>(<span class="hljs-attribute">end</span>=<span class="hljs-string">" "</span>) k = k + 1 <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(0 , i + 1): <span class="hljs-built_in">print</span>(<span class="hljs-string">"* "</span>, <span class="hljs-attribute">end</span>=<span class="hljs-string">""</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"\r"</span>)

pattern(5)</pre></div><h2 id="a163">Output:</h2><figure id="c4e1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-mg79KFazzs6_xBcEYVu6g.png"><figcaption></figcaption></figure><h2 id="eb09">Diamond Star Pattern Program</h2><div id="691a"><pre><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(5): <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(5): <span class="hljs-keyword">if</span> i + j == 2 <span class="hljs-keyword">or</span> i - j == 2 <span class="hljs-keyword">or</span> i + j == 6 <span class="hljs-keyword">or</span> j - i == 2: <span class="hljs-built_in">print</span>(<span class="hljs-string">"*"</span>, <span class="hljs-attribute">end</span>=<span class="hljs-string">""</span>) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(<span class="hljs-attribute">end</span>=<span class="hljs-string">" "</span>) <span class="hljs-built_in">print</span>()</pre></div><h2 id="d180">Output:</h2><figure id="274d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wXxW5Zs8fALUxyQllsFkIQ.png"><figcaption></figcaption></figure><h1 id="b37d">Number Pattern Programs</h1><p id="205c">Here are a few programs with numeric patterns in java.</p><h2 id="9d18">Simple Numbers Program</h2><div id="3c5a"><pre>def patter<span class="hljs-meta">n</span>(n): <span class="hljs-keyword">x</span> = 0 for i <span class="hljs-keyword">in</span> <span class="hljs-meta">range</span>(0 , n): <span class="hljs-keyword">x</span> += 1 for j <span class="hljs-keyword">in</span> <span class="hljs-meta">range</span>(0, i + 1): pr<span class="hljs-meta">int</span>(<span class="hljs-keyword">x</span> , <span class="hljs-keyword">end</span>=<span class="hljs-string">" "</span>) pr<span class="hljs-meta">int</span>(<span class="hljs-string">"\r"</span>) patter<span class="hljs-meta">n</span>(5)</pre></div><h2 id="86b5">Output:</h2><figure id="e715"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rwF3z5gqHGQUnABOfS2icQ.png"><figcaption></figcaption></figure><h2 id="cf47">Pascal’s Triangle Program</h2><div id="8ec3"><pre>def pascal(n): <span class="hljs-keyword">for</span> i in <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, n): <span class="hljs-keyword">for</span> <span class="hljs-keyword">j</span> in <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-keyword">print</span>(<span class="hljs-keyword">function</span>(i, <span class="hljs-keyword">j</span>),<span class="hljs-string">" "</span>, end=<span class="hljs-string">""</span>) <span class="hljs-keyword">print</span>()

def <span class="hljs-keyword">function</span>(n, <span class="hljs-keyword">k</span>): <span class="hljs-keyword">res</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">k</span> > n - <span class="hljs-keyword">k</span>): <span class="hljs-keyword">k</span> = n - <span class="hljs-keyword">k</span> <span class="hljs-keyword">for</span> i in <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-keyword">k</span>): <span class="hljs-keyword">res</span> = <span class="hljs-keyword">res</span> * (n - i) <span class="hljs-keyword">res</span> = <span class="hljs-keyword">res</span> // (i + <span class="hljs-number">1</span>)

<span class="hljs-keyword">return</span> <span class="hljs-keyword">res</span>

pascal(<span class="hljs-number">7</span>)</pre></div><h2 id="7eac">Output:</h2><figure id="e7ef"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DJFqfuqUn-oC3H5Dz6IxOg.png"><figcaption></figcaption></figure><h2 id="a720">Half-Pyramid Pattern With Numbers</h2><div id="a7b0"><pre>def patter<span class="hljs-meta">n</span>(n): for i <span class="hljs-keyword">in</span> <span class="hljs-meta">range</span>(1, n): for j <span class="hljs-keyword">in</span> <span class="hljs-meta">range</span>(1, i + 1): pr<span class="hljs-meta">int</span>(j, <span class="hljs-keyword">end</span>= <span class="hljs-string">" "</span>) pr<span class="hljs-meta">int</span>(<span class="hljs-string">"\r"</span>) patter<span class="hljs-meta">n</span>(5)</pre></div><h2 id="f3e5">Output:</h2><figure id="0ca2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*M0o3ligs_2EiwvstD3OtGA.png"><figcaption></figcaption></figure><h2 id="8b91">Diamond Pattern With Numbers</h2><div id="ac66"><pre><span class="hljs-attribute">def</span> pattern(n): <span class="hljs-attribute">k</span> = <span class="hljs-number">2</span> * n - <span class="hljs-number">2</span> <span class="hljs-attribute">x</span> = <span class="hljs-number">0</span> <span class="hljs-attribute">for</span> i in range(<span class="hljs-number">0</span>, n): <span class="hljs-attribute">x</span> += <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, k): <span class="hljs-attribute">print</span>(end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">k</span> = k - <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-attribute">print</span>(x, end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">print</span>(<span class="hljs-string">"\r"</span>) <span class="hljs-attribute">k</span> = n - <span class="hljs-number">2</span> <span class="hljs-attribute">x</span> = n + <span class="hljs-number">2</span> <span class="hljs-attribute">for</span> i in range(n, -<span class="hljs-number">1</span>, -<span class="hljs-number">1</span>): <span class="hljs-attribute">x</span> -= <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(k, <span class="hljs-number">0</span>, -<span class="hljs-number">1</span>): <span class="hljs-attribute">print</span>(end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">k</span> = k + <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-attribute">print</span>(x, end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">print</span>(<span class="hljs-string">"\r"</span>)

<span class="hljs-attribute">pattern</span>(<span class="hljs-number">5</span>)</pre></div><h2 id="5e9c">Output:</h2><figure id="122c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*s7d9gHEyn4762d-df5WDLQ.png"><figcaption></figcaption></figure><h2 id="bebe">Descending Order Pattern Program</h2><div id="d967"><pre>def patter<span class="hljs-meta">n</span>(n): for i <span class="hljs-keyword">in</span> <span class="hljs-meta">range</span>(n, 0, -1): for j <span class="hljs-keyword">in</span> <span class="hljs-meta">range</span>(1, i + 1): pr<span class="hljs-meta">int</span>(j, <span class="hljs-keyword">end</span>=<span class="hljs-string">" "</span>)

    pr<span class="hljs-meta">int</span>(<span class="hljs-string">"\r"</span>)

patter<span class="hljs-meta">n</span>(5)</pre></div><h2 id="f233">Output:</h2><figure id="3a69"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TJEH6Tv6W6aD5PlFmeb5Ag.png"><figcaption></figcaption></figure><h2 id="49a1">Binary Numbers Pattern Program</h2><div id="a718"><pre>def pattern(n): k = 2 * n - 2 <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(0, n): <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(0, k): <span class="hljs-built_in">print</span>(<span class="hljs-attribute">end</span>=<span class="hljs-string">" "</span>) k = k - 1 <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(0, i + 1): <span class="hljs-built_in">print</span>(<span class="hljs-string">'10'</span>, <span class="hljs-attribute">end</span>=<span class="hljs-string">""</span>)

    <span class="hljs-built_in">print</span>(<span class="hljs-string">"\r"</span>)

pattern(5)</pre></div><h2 id="37f3">Output:</h2><figure id="d1b3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*2FC_AxVkj9Kat-hpwyEYVA.png"><figcaption></figcaption></figure><h1 id="f3b4">Characters Pattern Programs</h1><p id="e1c8">Here are a few pattern programs in python with characters.</p><h2 id="7568">Right Alphabetical Triangle</h2><div id="b76a"><pre>def <span class="hljs-built_in">pattern</span>(n): x = <span class="hljs-number">65</span> for i in <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, n): ch = <span class="hljs-built_in">chr</span>(x) x += <span class="hljs-number">1</span> for j in <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-built_in">print</span>(ch, end=<span class="hljs-string">" "</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"\r"</span>)

<span class="hljs-built_in">pattern</span>(<span class="hljs-number">5</span>)</pre></div><h2 id="4983">Output:</h2><figure id="db07"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VxNb7TJ7orHKKN6_XFDZfQ.png"><figcaption></figcaption></figure><h2 id="5bfa">Character Pattern Program</h2><div id="4b26"><pre><span class="hljs-attribute">def</span> pattern(n): <span class="hljs-attribute">k</span> = <span class="hljs-number">2</span> * n - <span class="hljs-number">2</span> <span class="hljs-attribute">x</span> = <span class="hljs-number">65</span> <span class="hljs-attribute">for</span> i in range(<span class="hljs-number">0</span>, n): <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, k): <span class="hljs-attribute">print</span>(end=<span class="hljs-string">" "</span>) <span class="hljs-attr

Options

ibute">k</span> = k - <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-attribute">ch</span> = chr(x) <span class="hljs-attribute">print</span>(ch, end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">x</span> += <span class="hljs-number">1</span> <span class="hljs-attribute">print</span>(<span class="hljs-string">"\r"</span>)

<span class="hljs-attribute">pattern</span>(<span class="hljs-number">7</span>)</pre></div><h2 id="6f91">Output:</h2><figure id="b974"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*x4hYWl-qJXCFIjKIQ4f97w.png"><figcaption></figcaption></figure><h2 id="d79a">K Shape Character Program</h2><div id="7cbc"><pre><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(7): <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(7): <span class="hljs-keyword">if</span> j == 0 <span class="hljs-keyword">or</span> i - j == 3 <span class="hljs-keyword">or</span> i + j == 3: <span class="hljs-built_in">print</span>(<span class="hljs-string">"*"</span>, <span class="hljs-attribute">end</span>=<span class="hljs-string">""</span>) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(<span class="hljs-attribute">end</span>=<span class="hljs-string">" "</span>) <span class="hljs-built_in">print</span>()</pre></div><h2 id="485d">Output:</h2><figure id="7d9e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*g2-GY8RrxTQ7cxUCKxLAxg.png"><figcaption></figcaption></figure><h2 id="7554">Triangle Character Pattern Program</h2><div id="d70f"><pre><span class="hljs-attribute">def</span> pattern(n): <span class="hljs-attribute">k</span> = <span class="hljs-number">2</span> * n - <span class="hljs-number">2</span> <span class="hljs-attribute">x</span> = <span class="hljs-number">65</span> <span class="hljs-attribute">for</span> i in range(<span class="hljs-number">0</span>, n): <span class="hljs-attribute">ch</span> = chr(x) <span class="hljs-attribute">x</span> += <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, k): <span class="hljs-attribute">print</span>(end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">k</span> = k - <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-attribute">print</span>(ch, end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">print</span>(<span class="hljs-string">"\r"</span>)

<span class="hljs-attribute">pattern</span>(<span class="hljs-number">5</span>)</pre></div><h2 id="6566">Output:</h2><figure id="5a91"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rbaa5FMt49N-6rbKCaZBtQ.png"><figcaption></figcaption></figure><h2 id="d738">Diamond Shaped Character Pattern Program</h2><div id="3e83"><pre><span class="hljs-attribute">def</span> pattern(n): <span class="hljs-attribute">k</span> = <span class="hljs-number">2</span> * n - <span class="hljs-number">2</span> <span class="hljs-attribute">for</span> i in range(<span class="hljs-number">0</span>, n): <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, k): <span class="hljs-attribute">print</span>(end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">k</span> = k - <span class="hljs-number">1</span> <span class="hljs-attribute">x</span> = <span class="hljs-number">65</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-attribute">ch</span> = chr(x) <span class="hljs-attribute">print</span>(ch, end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">x</span> += <span class="hljs-number">1</span> <span class="hljs-attribute">print</span>(<span class="hljs-string">"\r"</span>) <span class="hljs-attribute">k</span> = n - <span class="hljs-number">2</span> <span class="hljs-attribute">x</span> = <span class="hljs-number">65</span> <span class="hljs-attribute">for</span> i in range(n, -<span class="hljs-number">1</span>, -<span class="hljs-number">1</span>): <span class="hljs-attribute">for</span> j in range(k, <span class="hljs-number">0</span>, -<span class="hljs-number">1</span>): <span class="hljs-attribute">print</span>(end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">k</span> = k + <span class="hljs-number">1</span> <span class="hljs-attribute">for</span> j in range(<span class="hljs-number">0</span>, i + <span class="hljs-number">1</span>): <span class="hljs-attribute">ch</span> = chr(x) <span class="hljs-attribute">print</span>(ch, end=<span class="hljs-string">" "</span>) <span class="hljs-attribute">x</span> += <span class="hljs-number">1</span> <span class="hljs-attribute">print</span>(<span class="hljs-string">"\r"</span>)

<span class="hljs-attribute">pattern</span>(<span class="hljs-number">5</span>)</pre></div><h2 id="9241">Output:</h2><figure id="1381"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*o2aKiGKCEXWGC-ZbL1nFQA.png"><figcaption></figcaption></figure><p id="3a1b">This brings us to the end of this article where we have learned how we can implement different patterns in python using asterisk, numbers, and characters with the help of loops in python. I hope you are clear with all that has been shared with you in this tutorial.</p><p id="b5d5">If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to <a href="https://www.edureka.co/blog/?utm_source=medium&amp;utm_medium=content-link&amp;utm_campaign=python-pattern-programs">Edureka’s official site.</a></p><p id="8ef6">Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.</p><blockquote id="e75a"><p>1. <a href="https://readmedium.com/machine-learning-classifier-c02fbd8400c9">Machine Learning Classifier in Python</a></p></blockquote><blockquote id="cfd1"><p>2. <a href="https://readmedium.com/python-scikit-learn-cheat-sheet-9786382be9f5">Python Scikit-Learn Cheat Sheet</a></p></blockquote><blockquote id="5fb1"><p>3. <a href="https://readmedium.com/python-libraries-for-data-science-and-machine-learning-1c502744f277">Machine Learning Tools</a></p></blockquote><blockquote id="384f"><p>4. <a href="https://readmedium.com/python-libraries-for-data-science-and-machine-learning-1c502744f277">Python Libraries For Data Science And Machine Learning</a></p></blockquote><blockquote id="8943"><p>5. <a href="https://readmedium.com/how-to-make-a-chatbot-in-python-b68fd390b219">Chatbot In Python</a></p></blockquote><blockquote id="567f"><p>6. <a href="https://readmedium.com/collections-in-python-d0bc0ed8d938">Python Collections</a></p></blockquote><blockquote id="4506"><p>7. <a href="https://readmedium.com/python-modules-abb0145a5963">Python Modules</a></p></blockquote><blockquote id="cfe2"><p>8. <a href="https://readmedium.com/python-developer-skills-371583a69be1">Python developer Skills</a></p></blockquote><blockquote id="835d"><p>9. <a href="https://readmedium.com/oops-interview-questions-621fc922cdf4">OOPs Interview Questions and Answers</a></p></blockquote><blockquote id="756d"><p>10. <a href="https://readmedium.com/python-developer-resume-ded7799b4389">Resume For A Python Developer</a></p></blockquote><blockquote id="8a06"><p>11. <a href="https://readmedium.com/exploratory-data-analysis-in-python-3ee69362a46e">Exploratory Data Analysis In Python</a></p></blockquote><blockquote id="159f"><p>12. <a href="https://readmedium.com/python-turtle-module-361816449390">Snake Game With Python’s Turtle Module</a></p></blockquote><blockquote id="1de8"><p>13. <a href="https://readmedium.com/python-developer-salary-ba2eff6a502e">Python Developer Salary</a></p></blockquote><blockquote id="cf88"><p>14.<a href="https://readmedium.com/principal-component-analysis-69d7a4babc96"> Principal Component Analysis</a></p></blockquote><blockquote id="fb52"><p>15. <a href="https://readmedium.com/python-vs-cpp-c3ffbea01eec">Python vs C++</a></p></blockquote><blockquote id="3a6c"><p>16. <a href="https://readmedium.com/scrapy-tutorial-5584517658fb">Scrapy Tutorial</a></p></blockquote><blockquote id="a96a"><p>17. <a href="https://readmedium.com/scipy-tutorial-38723361ba4b">Python SciPy</a></p></blockquote><blockquote id="353a"><p>18. <a href="https://readmedium.com/least-square-regression-40b59cca8ea7">Least Squares Regression Method</a></p></blockquote><blockquote id="2b33"><p>19. <a href="https://readmedium.com/jupyter-notebook-cheat-sheet-88f60d1aca7">Jupyter Notebook Cheat Sheet</a></p></blockquote><blockquote id="c408"><p>20. <a href="https://readmedium.com/python-basics-f371d7fc0054">Python Basics</a></p></blockquote><blockquote id="aba0"><p>21.<a href="https://readmedium.com/web-scraping-with-python-d9e6506007bf">Web Scraping With Python</a></p></blockquote><blockquote id="83b7"><p>22. <a href="https://readmedium.com/generators-in-python-258f21e3d3ff">Generators in Python</a></p></blockquote><blockquote id="d026"><p>23. <a href="https://readmedium.com/python-decorator-tutorial-bf7b21278564">Python Decorator</a></p></blockquote><blockquote id="9e2f"><p>24.<a href="https://readmedium.com/spyder-ide-2a91caac4e46"> Python Spyder IDE</a></p></blockquote><blockquote id="1e45"><p>25. <a href="https://readmedium.com/kivy-tutorial-9a0f02fe53f5">Mobile Applications Using Kivy In Python</a></p></blockquote><blockquote id="b521"><p>26. <a href="https://readmedium.com/best-books-for-python-11137561beb7">Top 10 Best Books To Learn & Practice Python</a></p></blockquote><blockquote id="6f93"><p>27. <a href="https://readmedium.com/robot-framework-tutorial-f8a75ab23cfd">Robot Framework With Python</a></p></blockquote><blockquote id="9068"><p>28. <a href="https://readmedium.com/snake-game-with-pygame-497f1683eeaa">Snake Game in Python using PyGame</a></p></blockquote><blockquote id="fab7"><p>29. <a href="https://readmedium.com/django-interview-questions-a4df7bfeb7e8">Django Interview Questions and Answers</a></p></blockquote><blockquote id="f5c0"><p>30. <a href="https://readmedium.com/python-applications-18b780d64f3b">Top 10 Python Applications</a></p></blockquote><blockquote id="b1c1"><p>31. <a href="https://readmedium.com/hash-tables-and-hashmaps-in-python-3bd7fc1b00b4">Hash Tables and Hashmaps in Python</a></p></blockquote><blockquote id="c680"><p>32. <a href="https://readmedium.com/whats-new-python-3-8-7d52cda747b">Python 3.8</a></p></blockquote><blockquote id="cae9"><p>33. <a href="https://readmedium.com/support-vector-machine-in-python-539dca55c26a">Support Vector Machine</a></p></blockquote><blockquote id="371b"><p>34. <a href="https://readmedium.com/python-tutorial-be1b3d015745">Python Tutorial</a></p></blockquote><p id="4a33"><i>Originally published at <a href="https://www.edureka.co/blog/python-pattern-programs/">https://www.edureka.co</a> on </i>Sep 27, 2019</p></article></body>

Learn How To Make Python Pattern Programs With Examples

Python Pattern Programs — Edureka

Python programming language is quite easy to learn. The implementation of various libraries with the ease of syntax makes it stand out, one of the many reasons why it has become the most popular programming language in this decade. While the learning part is easy, the interviewers often seek your approach in building the logic for pattern programs. As tricky as it may sound, with python it is a piece of cake. In this article, we will learn about various pattern programs in python. The following topics are covered in this blog:

  • Star Pattern Programs
  1. Pyramid Pattern Program
  2. Half-Pyramid Pattern Program
  3. Diamond Shaped Pattern Program
  4. Start Pattern Program
  5. Hourglass Pattern Program
  • Number Pattern Programs
  1. Simple Numbers In A Pyramid
  2. Pascal’s Triangle Pattern
  3. Diamond Pattern Program
  • Characters Patter Programs

The pattern programs incorporate a lot of nested loops. So if you are not familiar with loops in python, make sure to check out the detailed tutorial on loops in python.

Star Pattern Programs

Following are a few star pattern programs in python.

Pyramid Pattern Program

def pattern(n):
      k = 2 * n - 2
      for i in range(0,n):
           for j in range(0,k):
               print(end=" ")
           k = k - 1
           for j in range(0, i+1):
                print("*", end=" ")
           print("\r")
pattern(5)

Output:

Reverse Pyramid Pattern Program

def pattern(n):
      k = 2*n -2
      for i in range(n,-1,-1):
           for j in range(k,0,-1):
                print(end=" ")
           k = k +1
           for j in range(0, i+1):
                print("*", end=" ")
           print("\r")
pattern(5)

Output:

Right Start Pattern Program

def pattern(n):
      for i in range(0, n):
           for j in range(0, i + 1):
                print("* ", end="")
           print("\r")
      for i in range(n, 0 , -1):
          for j in range(0, i + 1):
               print("* ", end="")
          print("\r")
 
pattern(5)

Output:

Left Start Pattern Program

def pattern(n):
    k = 2 * n - 2
    for i in range(0, n-1):
        for j in range(0, k):
            print(end=" ")
        k = k - 2
        for j in range(0, i + 1):
            print("* ", end="")
        print("\r")
    k = -1
    for i in range(n-1,-1,-1):
        for j in range(k,-1,-1):
            print(end=" ")
        k = k + 2
        for j in range(0, i + 1):
            print("* ", end="")
        print("\r")
 
 
pattern(5)

Output:

Hourglass Pattern Program

def pattern(n):
     k = n - 2
     for i in range(n, -1 , -1):
          for j in range(k , 0 , -1):
               print(end=" ")
          k = k + 1    
          for j in range(0, i+1):
               print("* " , end="")
          print("\r")
      k = 2 * n  - 2
      for i in range(0 , n+1):
           for j in range(0 , k):
               print(end="")
           k = k - 1
            for j in range(0, i + 1):
                 print("* ", end="")
            print("\r")
 
pattern(5)

Output:

Half-Pyramid Pattern Program

def pattern(n):
     for i in range(0,n):
         for j in range(0, i+1):
              print("* " , end="")
         print("\r")
 
pattern(5)

Output:

Left Half-Pyramid Pattern Program

def pattern(n):
     k = 2 * n - 2
     for i in range(0, n):
          for j in range(0, k):
               print(end=" ")
          k = k - 2
          for j in range(0, i + 1):
              print("* ", end="")
          print("\r")
  
pattern(5)

Output:

Downward Half-Pyramid Pattern Program

def pattern(n):
      for i in range(n, -1, -1):
           for j in range(0, i + 1):
               print("* ", end="")
           print("\r")
 
pattern(5)

Output:

Diamond Shaped Pattern Program

def pattern(n):
     k = 2 * n - 2
     for i in range(0, n):
          for j in range(0 , k):
               print(end=" ")
          k = k - 1
          for j in range(0 , i + 1 ):
               print("* ", end="")
          print("\r")
     k = n - 2
     for i in range(n , -1, -1):
          for j in range(k , 0 , -1): 
               print(end=" ")
           k = k + 1
           for j in range(0 , i + 1):
                print("* ", end="")
           print("\r")
 
pattern(5)

Output:

Diamond Star Pattern Program

for i in range(5):
    for j in range(5):
        if i + j == 2 or i - j == 2 or i + j == 6 or j - i == 2:
            print("*", end="")
        else:
            print(end=" ")
    print()

Output:

Number Pattern Programs

Here are a few programs with numeric patterns in java.

Simple Numbers Program

def pattern(n):
    x = 0
    for i in range(0 , n):
        x += 1 
        for j in range(0, i + 1):
            print(x , end=" ") 
        print("\r") 
pattern(5)

Output:

Pascal’s Triangle Program

def pascal(n):
    for i in range(0, n):
        for j in range(0, i + 1):
            print(function(i, j)," ", end="")
        print()
 
def function(n, k):
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(0, k):
        res = res * (n - i)
        res = res // (i + 1)
 
    return res
 
pascal(7)

Output:

Half-Pyramid Pattern With Numbers

def pattern(n):
     for i in range(1, n):
         for j in range(1, i + 1):
             print(j, end= " ")
         print("\r")
pattern(5)

Output:

Diamond Pattern With Numbers

def pattern(n):
    k = 2 * n - 2
    x = 0
    for i in range(0, n):
        x += 1
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            print(x, end=" ")
        print("\r")
    k = n - 2
    x = n + 2
    for i in range(n, -1, -1):
        x -= 1
        for j in range(k, 0, -1):
            print(end=" ")
        k = k + 1
        for j in range(0, i + 1):
            print(x, end=" ")
        print("\r")
 
pattern(5)

Output:

Descending Order Pattern Program

def pattern(n):
    for i in range(n, 0, -1):
        for j in range(1, i + 1):
            print(j, end=" ")
 
        print("\r")
 
pattern(5)

Output:

Binary Numbers Pattern Program

def pattern(n):
    k = 2 * n - 2
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            print('10', end="")
 
        print("\r")
 
pattern(5)

Output:

Characters Pattern Programs

Here are a few pattern programs in python with characters.

Right Alphabetical Triangle

def pattern(n):
    x = 65
    for i in range(0, n):
        ch = chr(x)
        x += 1
        for j in range(0, i + 1):
            print(ch, end=" ")
        print("\r")
 
pattern(5)

Output:

Character Pattern Program

def pattern(n):
    k = 2 * n - 2
    x = 65
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            ch = chr(x)
            print(ch, end=" ")
            x += 1
        print("\r")
 
 
pattern(7)

Output:

K Shape Character Program

for i in range(7):
    for j in range(7):
        if j == 0 or i - j == 3 or i + j == 3:
            print("*", end="")
        else:
            print(end=" ")
    print()

Output:

Triangle Character Pattern Program

def pattern(n):
    k = 2 * n - 2
    x = 65
    for i in range(0, n):
        ch = chr(x)
        x += 1
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            print(ch, end=" ")
        print("\r")
 
 
pattern(5)

Output:

Diamond Shaped Character Pattern Program

def pattern(n):
    k = 2 * n - 2
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        x = 65
        for j in range(0, i + 1):
            ch = chr(x)
            print(ch, end=" ")
            x += 1
        print("\r")
    k = n - 2
    x = 65
    for i in range(n, -1, -1):
        for j in range(k, 0, -1):
            print(end=" ")
        k = k + 1
        for j in range(0, i + 1):
            ch = chr(x)
            print(ch, end=" ")
            x += 1
        print("\r")
 
 
pattern(5)

Output:

This brings us to the end of this article where we have learned how we can implement different patterns in python using asterisk, numbers, and characters with the help of loops in python. I hope you are clear with all that has been shared with you in this tutorial.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Python Modules

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21.Web Scraping With Python

22. Generators in Python

23. Python Decorator

24. Python Spyder IDE

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

Originally published at https://www.edureka.co on Sep 27, 2019

Python
Python Programming
Python Pattern
Python Projects
Programming
Recommended from ReadMedium