Python code of a rose, explanation of python rose code

Hello everyone, this article will focus on using python to draw a rose code to wish a happy birthday. The python code of a rose is something that many people want to understand. To figure out the python code to draw a rose, you need to understand the following matter.

1. How to draw roses with python, please post the code, thank you very much

import turtle
# set initial position
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle. pendown()
turtle.right(90)
# pistil
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()
# petal 1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)
# petals 2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)
# leaf 1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# leaf 2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)

operation result:

2. How to draw a wind direction rose diagram with Python matplotlib can give the program

需要提前安装windrose模块 from windrose import WindroseAxes from matplotlib import pyplot as plt import matplotlib.cm as cm from numpy.random import random from numpy import arange ws = random(500)*6 wd = random(500)*360 #A quick way to create new windrose axes... def new_axes():     fig = plt.figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='w')     rect = [0.1, 0.1, 0.8, 0.8]     ax = WindroseAxes(fig, rect, axisbg='w')     fig.add_axes(ax)     return ax #...and adjust the legend box def set_legend(ax):     l = ax.legend(shadow=True, bbox_to_anchor=[-0.1, 0], loc='lower left')     plt.setp(l.get_texts(), fontsize=10)   ax = new_axes() ax.bar(wd, ws, normed=True, opening=0.8, edgecolor='white') set_legend(ax) plt.show()

3. How to draw a wind direction rose diagram with Python matplotlib can give the program

import numpy as np
import matplotlib.pyplot as plt
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)
# Use custom colors and opacity
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.jet(r / 10.))
    bar.set_alpha(0.5)
plt.show()

The principle of the above code is almost the same, and the specific changes are made according to the official documents.

4. Python code for drawing love

I've tried my best, but it might be a bit difficult to see the hearts really well (except for the stickers, but that's not good after all, right).

The following is the Python code:

from turtle import *

speed(0)#speed, if you think it is too fast, you can adjust it to 1 (1 is slow, 10 is fast, 0 is the fastest)

tracer(2)#Display the speed, if you think it is too fast, you can adjust it to 1 (same as above)

hideturtle()#Hide the brush

setup(500,500)#500×500 square screen

up()#lift the pen

goto(-150,50)#go to the target point

down()# pen down

color("red","red")#The brush is filled with red

seth(90)# change direction to up

begin_fill()#start filling

circle(-75,180)#Draw a semicircle to the right

circle(75,-180)#Draw a semicircle to the right

step = 4.06635

seth(270)#down

for i in range(90):#draw ellipse

  forward(step)# walk step length

  step = step - 0.025# gradually reduce the step size

  right(1)# turn right 1 degree

for i in range(90):

  step = step + 0.025# gradually increase the step size

  right(1)# turn right 1 degree

  forward(step)# walk step length

end_fill()


The following is the running effect:

a heart

5. Python prints out the number of all four-leaf roses

Python prints out all the four-leaf rose numbers, if m == number: #judgment condition
result.append(number) #Add "four-leaf rose number" to the list
print(result) #output list to judge whether the number is four leaves The number of roses is assigned to m, and all numbers that meet the requirements are added to the list and output

6. Write a self-exponentiation program in python

# -*- coding:utf-8 -*-
import math
'''
A self-exponentiation number refers to an n-digit number whose sum to the nth power of each digit is equal to itself.
For example: when n is 3, 1^3 + 5^3 + 3^3 = 153, 153 is a self-power number when n is 3
'''
n=int(input("Please enter the number of powers:\n")) 
if n==1:
	print "Number of Singles:"
	print "0\t"
elif n == 2:
	print "two powers:"
	print "No self-exponentiation!"
elif n == 3:
	print "Number of daffodils:"
elif n == 4:
	print "Number of roses:"
elif n == 5:
	print "Number of five-pointed stars:"
elif n == 6:
	print "Sexample:"
elif == 7:
	print "Number of the Big Dipper:"
elif n == 8:
	print "Number of Eight Immortals:"
elif n == 9:
	print "Nine Nine Double Nine Numbers:"
elif n == 10:
	print "Perfect number:"
else:
	print "Other self-powers:"
#Define a number, starting from 10^(n-1) and ending at 10^n
for i in range(int(math.pow(10,n-1)),int(math.pow(10,n))):
	num = 0
	for j in str(i):
		num += int(math.pow(int(j), n))
	if i == num:
		print i

operation result:

Please enter the number of self-power digits you want to check:

3

Number of daffodils:

153

370

371

407

7. python father's day code

The python code for Father’s Day is as follows:
Blowing kisses: ____, kissesandhugs, baci, cium (Indonesian version of "Meme Da"), Besos (Spanish version of "Kiss"), XOXO, Meme Da, kiss cake: _______________, happybirthday, birthday Happy, birthday, star rain: ______, missu, miss you, I miss you, Teechodemenos (Spanish version of "missing you") flames: booming and firecrackers: Happy New Year Money bag: Gong Xi Fa Cai, Lucky Stars: Lucky Stars According to, the lucky star shines brightly.
First write down what you want to say to your father, and use the split() function to cut it into separate words according to the space; then use the loop statement according to the heart-shaped function, print line by line, print each word separately inside the function, and print spaces outside the function; finally , the heart-shaped patterns corresponding to each word will be displayed in sequence.

8. What is the source code of python cool fireworks confession?

After finishing this tutorial, you can also make such a firework show.

As shown above, here we simulate the explosion effect by splitting a particle on the screen into X number of particles. The particles "expand," meaning they move at a constant speed and at equal angles to each other. This allows us to simulate the fireworks blooming as an outwardly expanding circle.

After a certain amount of time, the particles will enter the "" stage, that is, they will start to fall to the ground due to gravity, like fireworks that bloom and then go out.

The Basics: Design Fireworks with Python and Tkinter.

Here we no longer throw out all the mathematical knowledge in one brain, we write code and talk about theory. First, make sure you have installed and imported Tkinter, which is Python's standard GUI library and is widely used in various projects and program development. Using Tkinter in Python can quickly create GUIs.

import tkinter as tk

from PIL import Image, ImageTk

from time import time, sleep

from random import choice, uniform, randint

from math import sin, cos, radians

In addition to Tkinter, in order to make the interface have a beautiful background, we also import PIL for use, and import some other packages, such as time, random and math. They allow us to more easily control the trajectory of fireworks particles.

The basic setup for a Tkinter application is as follows:

root = tk.Tk()

In order to initialize Tkinter, we must create a Tk() root widget, which is a window with a title bar and other decorations provided by the window manager. The root widget must be created before we create other widgets, and there can only be one root widget.

w = tk.Label(root, text="Hello Tkinter!")

This line of code contains the Label widget. The first parameter in the Label call is the name of the parent window, which is the "root" we use here. The keyword argument "text" specifies the text content to display. You can also call other widgets: Button, Canvas, etc.

w.pack()

root.mainloop()

The next two lines of code are important. The packing method here is to tell Tkinter to resize the window to fit the widget used. The window doesn't appear until we enter the Tkinter event loop, which is called by root.mainloop(). The script will stay in the event loop until we close the window.

Translate Fireworks Blooms to code

Now we design an object that represents each particle in the firework event. Each particle will have some important properties that govern its appearance and movement: size, color, position, speed, etc.

 

Guess you like

Origin blog.csdn.net/chatgpt001/article/details/129113300