CrazyEngineers
  • Manish
    Manish

    MemberNov 11, 2011

    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 😀
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register