Giter VIP home page Giter VIP logo

python_patterns_tutorial's Introduction

Python_Patterns_Tutorial

How to draw patterns with Python code

There are 5 main patterns in Python, which can be used as building blocks for almost any pattern. They are:

  • square
  • increasing triangle
  • decreasing triangle
  • increasing right triangle
  • decreasing right triangle

All the code is included in the repository.

Square

The square is the simplest pattern, but its code can be used as a foundation to all the other patterns.

n = 5
for i in range(n):
    for k in range(n):
        print("*", end=" ")
    print()

Increasing triangle

By changing the nested loop's range to (i + 1) we now make it iterate from 1 to 5 times not just 5.

n = 5
for i in range(n):
    for k in range(i + 1):
        print("*", end=" ")
    print()

Decreasing triangle

By changing the nested loop's range to (i, n) we now make it iterate from 0 to 5 on the first iteration, but i keeps increasing so we print less stars each iteration.

n = 5
for i in range(n):
    for k in range(i, n):
        print("*", end=" ")
    print()

Increasing right triangle

In order to print a triangle on the right, we first have to print blank spaces. To do that, we just copy the decreasing triangle loop and replace the '*' with a blank space. After that bellow it, we paste the increasing triangle loop.

n = 5
for i in range(n):
    for k in range(i, n):
        print(" ", end=" ")
    for j in range(i + 1):
        print("*", end=" ")
    print()

Decreasing right triangle

The decreasing right triangle is almost the same, we just flip around the loop ranges of k and j.

for i in range(n):
    for k in range(i + 1):
        print(" ", end=" ")
    for j in range(i, n):
        print("*", end=" ")
    print()

Thank you for reading. Now with all these basic shapes you can create more complicated ones by putting them together.

python_patterns_tutorial's People

Contributors

mark-delchev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.