1. Home >
  2. Apps >
  3. Groups >

Play with For Loops in Python

Question asked by Manish Goyal in #Coffee Room on Nov 11, 2011
Manish Goyal
Manish Goyal · Nov 11, 2011
Member of CrazyEngineers
I guess this is one of most coolest way to improve your logic and better understanding of any language
This is very small program, i have developed in python to create a simple patter

*
**
***
****
*****


import sys
# Define a main() function that prints a little greeting.
def main():
 
 # Get the name from the command line, using 'World' as a fallback.
    for x in range(1,6):
        print '*' * x
            
            
# This is the standard boilerplate that calls the main() function.

if __name__ == '__main__':
  main()
Explanation of code,

def main(): //Function definition, since in python main function is not called automatically

for x in range(1,6): // Similar to for(i = 1; i<6;i ++) in c++
print '*' * x // for printing * as many times value of x

if __name__ == '__main__': // basically similar to call of any function
main()


This is simplest explanation of above code ,Please correct me if i am wrong somewhere

Also Please come out with some more interesting patterns in pythons , i will also come out with more 😀 Posted in: #Coffee Room

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up