You can learn Python articles without reading online classes (the first day)

Why_ does_ it_ work 2021-11-25 15:32:33

Why learn python?

In the age of big data , Tiktok and Kwai Fu are all around the world. , Database of homework to help ape search questions , Are inseparable from big data , Scientific computing of big data , The most important language is python, Web crawler ( Web crawler - Sogou encyclopedia ) Search engines are also mainly python, Including some game images and so on python, The most important thing is that Baidu's artificial intelligence technology continues to improve , The rise of artificial intelligence , and ai So is the main language of intelligence python. The most magical python Can be mixed programming can be used c Language ,java Wait, hybrid programming , Then let's read an article together , introduction python.

Environment building

The compiler I use is pycharm

Generally speaking, download the community version ( Free version ) Ook , One set of services in place , Don't watch the video

Download PyCharm: Python IDE for Professional Developers by JetBrains

å¨è¿éæå¥å¾çæè¿°

During the installation process

1、 Double click the downloaded PyCharm Installation package , The interface as shown in the figure below appears , Click on “next”.

å¨è¿éæå¥å¾çæè¿°
2、 Select the installation directory ,Pycharm More memory required , It is recommended to install it in D Disk or E disc , Not recommended on system disk C disc .

å¨è¿éæå¥å¾çæè¿°


3、 Make relevant settings , If you have no special need, just check the box in the picture .

å¨è¿éæå¥å¾çæè¿°
Numbers 1:create desktop shortcut( Create a desktop shortcut ), System 32 You can choose 32-bit, System 64 You can choose 64-bit.
My computer is 64 Bit system , It automatically displays 64 position .
Numbers 2:update path variable(restart needed) Update path variables ( Reboot required ),add launchers dir to the path( Add the initiator directory to the path ).
Numbers 3:update context menu( Update context menu ),add open folder as project( Add open folder as project ). Add right mouse button menu , Open this shelf by opening the project . If you often need to download some other people's code to see , You can check this option , This will increase the right mouse button menu options . That is, you double-click on your computer py file , Will be used by default pycharm open .
Numbers 4:create associations create associations , relation .py file . Will all py The file is associated with pycharm.
4、 The default can be , Click on install.

å¨è¿éæå¥å¾çæè¿°

installation is complete

5、 open PyCharm

Restart the computer , double-click PyCharm Turn on run .
 Insert picture description here



5、 ... and 、 To configure PyCharm



1、 Click on New Project Create a new project

 Insert picture description here



2、 Project settings

 Insert picture description here



3、 Environment configuration , It takes time

 å¨è¿éæå¥å¾çæè¿°

å¨è¿éæå¥å¾çæè¿°

4、 establish python file

å¨è¿éæå¥å¾çæè¿°
 


First set off a fireworks to check whether the operation is correct

# -*- coding: utf-8 -*-
import math, random,time
import threading
import tkinter as tk
import re
#import uuid
Fireworks=[]
maxFireworks=8
height,width=600,600
class firework(object):
def __init__(self,color,speed,width,height):
#uid=uuid.uuid1()
self.radius=random.randint(2,4) # The particle radius is 2~4 Pixels
self.color=color # Particle color
self.speed=speed #speed yes 1.5-3.5 second
self.status=0 # Without the fireworks exploding ,status=0; After the explosion ,status>=1; When status>100 when , The life of fireworks ends
self.nParticle=random.randint(20,30) # Number of particles
self.center=[random.randint(0,width-1),random.randint(0,height-1)] # Fireworks random center coordinates
self.oneParticle=[] # Original particle coordinates (100% In the state of )
self.rotTheta=random.uniform(0,2*math.pi) # Rotation angle of elliptic plane
# Elliptic parametric equation :x=a*cos(theta),y=b*sin(theta)
#ellipsePara=[a,b]
self.ellipsePara=[random.randint(30,40),random.randint(20,30)]
theta=2*math.pi/self.nParticle
for i in range(self.nParticle):
t=random.uniform(-1.0/16,1.0/16) # Produce a [-1/16,1/16) The random number
x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t) # Elliptic parametric equation
xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta), y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta) # Plane rotation equation
self.oneParticle.append([xx,yy])
self.curParticle=self.oneParticle[0:] # Current particle coordinates
self.thread=threading.Thread(target=self.extend) # Create a thread object
def extend(self): # Particle swarm state change function
for i in range(100):
self.status+=1 # Update status identification
self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle] # Update particle swarm coordinates
time.sleep(self.speed/50)
def explode(self):
self.thread.setDaemon(True) # Set the current process as a daemon thread
self.thread.start() # Start thread
def __repr__(self):
return ('color:{color}\n'
'speed:{speed}\n'
'number of particle: {np}\n'
'center:[{cx} , {cy}]\n'
'ellipse:a={ea} , b={eb}\n'
'particle:\n{p}\n'
).format(color=self.color,speed=self.speed,np=self.nParticle,cx=self.center[0],cy=self.center[1],p=str(self.oneParticle),ea=self.ellipsePara[0],eb=self.ellipsePara[1])
def colorChange(fire):
rgb=re.findall(r'(.{2})',fire.color[1:])
cs=fire.status
f=lambda x,c: hex(int(int(x,16)*(100-c)/30))[2:] # When the particle life reaches 70% when , The color begins to decay linearly
if cs>70:
ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs)
else:
ccr,ccg,ccb=rgb[0],rgb[1],rgb[2]
return '#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb)
def appendFirework(n=1): # Recursively generate fireworks objects
if n>maxFireworks or len(Fireworks)>maxFireworks:
pass
elif n==1:
cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:]) # Produce a 0~16777215(0xFFFFFF) The random number , As a random color
a=firework(cl,random.uniform(1.5,3.5),width,height)
Fireworks.append( {'particle':a,'points':[]} ) # Create a particle display list ,‘particle’ For a fireworks object ,‘points’ The object variable set displayed for each particle
a.explode()
else:
appendFirework()
appendFirework(n-1)
def show(c):
for p in Fireworks: # Every time you refresh the display , First delete all the existing particles
for pp in p['points']:
c.delete(pp)
for p in Fireworks: # According to each fireworks object , Calculate the display object of each particle
oneP=p['particle']
if oneP.status==100: # The status is marked as 100, It means that the life of fireworks is over
Fireworks.remove(p) # Remove the current fireworks
appendFirework() # Add a new fireworks
continue
else:
li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle] # Translate the ellipse whose center is the origin to the random center coordinates
color=colorChange(oneP) # Calculate the current color according to the current state of fireworks
for pp in li:
p['points'].append(c.create_oval(pp[0]-oneP.radius, pp[1]-oneP.radius, pp[0]+oneP.radius, pp[1]+oneP.radius, fill=color)) # Draw each particle of fireworks
root.after(50, show,c) # Callback , Every time 50ms Refresh once
if __name__=='__main__':
appendFirework(maxFireworks)
root = tk.Tk()
cv = tk.Canvas(root, height=height, width=width)
cv.create_rectangle(0, 0, width, height, fill="black")
cv.pack()
root.after(50, show,cv)
root.mainloop()

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25