View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12890 Members
Join this group to post and comment.

Play with For Loops in Python

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 😀

Share this content on your social channels -