banner



How Can We Draw Circle Out Of Squares In Python

draw circle using squares

I am checking out a Python class at udacity and constitute an interesting Python module named Turtle. Now let me share with you a simple code that I just have written.

import turtle  def draw_square():     window = turtle.Screen()     window.bgcolor("crimson")          brad = turtle.Turtle()     brad.shape("turtle")     brad.color("yellow")     brad.speed(5)     counter = 0     while counter < 36:         brad.forward(100)         brad.correct(90)         brad.forward(100)         brad.correct(xc)         brad.frontward(100)         brad.correct(90)         brad.forward(100)         brad.correct(100)         counter += ane      window.exitonclick()  if __name__ == "__main__":     draw_square()                

Just save the code in a file and run it, and you volition see something like this:

At present a simple question for you. How did I come up with the number 36 in the while loop? (while counter < 36).

Popular posts from this weblog

lambda magic to find prime numbers

Observe prime number up to 100 with simply 3 lines of code. The 4th line is for printing ;) nums = range(2, 100) for i in range(two, x):     nums = filter(lambda x: x == i or x % i, nums) impress nums Isn't it astonishing? Check my new postal service about Prime number: http://honey-python.blogspot.com/2010/11/prime-number-generator-in-python-using.html

Convert text to ASCII and ASCII to text - Python lawmaking

Python has a very simple way to convert text to ASCII and ASCII to text. To observe the ASCII value of a character use the ord() function. Example: >>> ord('B') 66 To get a grapheme from it's ASCII value utilise the chr() function. Example: >>> chr(65) 'A' Here is a plan to become the listing of all characters along with their values: # plan to print the listing of characters and their ASCII values for value in range(0, 255): impress "ASCII Value:", value, "\t", "Grapheme:", chr(value), "\n" Here is another uncomplicated ASCII encoding-decoding plan: # Convertion from text to ASCII codes message = raw_input("Enter message to encode: ") print "Decoded string (in ASCII):" for ch in message: print ord(ch), impress "\n\northward" # Convertion from ASCII codes to text message = raw_input("Enter ASCII codes: ") decodedMessage = "" for item in message.split(): decodedM

Go adjacent element from a list in Python

Sometimes y'all will need to continue some items in the listing and go the side by side item. It tin can be done easily using a variable equally the current position tracker. Check the code below: current_position = 0 my_li = [ane, 2, three, 4, five] for i in range(0, viii): value = my_li[current_position % (len(my_li))] current_position += ane impress value for i in range(0, 5): value = my_li[current_position % (len(my_li))] current_position += 1 print value You can also do this using an iterator. Try this python code: def get_next_element(my_itr): try: return my_itr.next() except StopIteration: render None my_li = [1, two, 3, 4, v] #convert the listing to an iterator my_itr = iter(my_li) for i in range(0, 10): impress get_next_element(my_itr) If you run the code yous volition see the post-obit output: 1 two 3 4 five None None None None None This is because the iterator is not circular. It can exist stock-still li

Source: http://love-python.blogspot.com/2014/07/draw-circle-using-squares.html

Posted by: mcculloughbefor1969.blogspot.com

0 Response to "How Can We Draw Circle Out Of Squares In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel