起飞的 python!!!

一、python基础实例

** 以代码实例进行综合展示**

1、画了三个圆

#fengzhuanglainxi.py
'''
fengzhuanglainxi.py
学习编程派第四章,封装
2020/10/11
'''
import turtle
import math

bob=turtle.Turtle()
#t=bob
def square(t,length):
    '''
    定义了一个画正方形的函数,t为turtle,length为边长
    '''
    for i in range(4):
        t.fd(length)
        t.lt(90)
#square(bob,300)

#m=int(input("qing shuru ni xiangyaode duobianxingde bian: "))

def polygon(t,n,length):
    '''
    定义了一个画多边形的函数,t为turtle,n为边数,
    length为边长
    '''
    m=360/n
    for i in range(n):
        t.fd(length)
        t.lt(m)


def circle(t,r):
    '''
    定义一个画圆(近似圆)的函数,t为turtle,r为半径
    '''
    circumfercence = 2 * math.pi * r
    n = int(circumfercence / 3) + 1
    length = circumfercence / n
    polygon(t,n,length)

circle(bob,500)

2、画了三朵花

# -*- coding: utf-8 -*-
"""
This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/

Created on Sun Oct 11 21:10:08 2020
#three flowers.py
"""

'''
 在开头加上from __future__ import print_function这句之后,
 即使在python2.X,使用print就得像python3.X那样加括号使用。
 python2.X中print不需要括号,而在python3.X中则需要。
 (学到了新知识)
但是带着这句话在python3.7中会报错
'''
import turtle
from mypolygon import arc
#上面调的mypolygon函数库其实是自己写的,因为安装的库没有,所以需要自己写个有的
'''
def arc(t, r, angle):
    """Draws an arc with the given radius and angle.
    t: Turtle
    r: radius
    angle: angle subtended by the arc, in degrees
    """
    arc_length = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_length / 4) + 3
    step_length = arc_length / n
    step_angle = float(angle) / n

    # making a slight left turn before starting reduces
    # the error caused by the linear approximation of the arc
    t.lt(step_angle/2)
    polyline(t, n, step_length, step_angle)
    t.rt(step_angle/2)

'''

def petal(t,r,angle):
	'''
	定义一个用弧线画花瓣的函数
	t是turtle
	r是半径
	angle是角度
	'''
	for i in range(2):
		arc(t,r,angle)
		t.lt(180-angle)
		
def flower (t,n,r,angle):
	'''
	定义一个用花瓣画花朵的函数
	t是turtle
	n是花瓣的数量
	r是半径
	angle是角度
	'''
	for i in range(n):
		petal(t,r,angle)
		t.lt(360.0/n)
		
def move(t,length):
	'''
	定义一个移动画笔的函数
	t是turtle
	length是移动的长度
	'''
	t.pu()#turtle.penup() 别名turtle.pu()
	t.fd(length)
	t.pd()
	
bob = turtle.Turtle()

move(bob,-100)
flower(bob,7,60.0,60.0)


move(bob,100)
flower(bob,10,40.0,80.0)

move(bob,100)
flower(bob,20,140.0,20.0)

bob.hideturtle()
turtle.mainloop()
#这里第一次运行没有问题,第二次会报错:Reloaded modules:  
解决办法是:Preferences->Python interpreter->User Module Reloader (UMR),取消Enable UMR的选项,重新启动
--问题可能因为spyder的python内核的某种原因导致的。

在这里插入图片描述

3、分出四个饼

# -*- coding: utf-8 -*-
"""
Drawpie.py
thinkpython xiti 4-3

This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/

Created on Mon Oct 12 14:59:30 2020
@author: xingxingya

"""

import math
import turtle

def draw_pie(t,n,r):
	'''
	画一个饼状图,并让他可以沿着一定的方向移动
	----------
	t : turtle
	r : 饼的数量
	n : 这个饼图的半径

	'''
	polypie(t,n,r)
	t.pu()
	t.fd(r*2+10)#向前移动出比直径多一点的距离
	t.pd()
	
def polypie(t,n,r):
	'''
	将饼图做成饼
	----------
	t : turtle
	n : 分出饼块的数量
	r : 饼的半径
	'''
	angle = 360.0/n
	for i in range(n):#打算饼图里有几块就是几
		isosceles(t,r,angle/2)
		t.lt(angle)#画完一块饼后向左转,做好画下一块的准备
		
def isosceles(t,r,angle):
	'''
	绘制等腰三角形,画饼块
	----------
	t : turtle
	r : 等边三角形的边长
	angle : 角度
	'''
	y = r * math.sin(angle * math.pi/180)
	#这可能是个求边长?的公式
	
	t.rt(angle)
	t.fd(r)
	t.lt(90+angle)
	t.fd(2*y)
	t.lt(90+angle)
	t.fd(r)
	t.lt(180-angle)
	
bob = turtle.Turtle()

bob.pu()
bob.bk(130)
bob.pd()

#可以以不同的尺寸画饼
size = 40
draw_pie(bob,5,size)
draw_pie(bob,6,size)
draw_pie(bob,7,size)
draw_pie(bob,8,size)	

bob.hideturtle()
turtle.mainloop()

在这里插入图片描述


二、python方法

1、字符串方法

1.1 str.lower()字符串小写

str.lower()可以把字符串全部小写

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
>>> string='ABCdef'
>>> string.lower()
'abcdef'

与之对应的则是字符串大写

>>> string.upper()
'ABCDEF'

2、列表方法

3、元组方法

4、其他方法

4.1 round()

对数字进行四舍五入

Python 3.8.1 
>>> num=123.123546
>>> round(num,2)
123.12
>>> round(num)
123
>>> round(num,-2)
100.0
>>> round(num,0)
123.0

4.2 eval()

官方解释为:将字符串str当成有效的表达式来求值并返回计算结果。

from nltk.book import *

def get_Vocabulary_diversity(n):
    '''
    传入文本n,
    获得其词汇多样性
    '''
    words=[t.lower() for t in n if not re.search(r'\W|\d',t)]
    token_number = len(words)
    type_number = len(set(words))
    Vocabulary_diversity = token_number/type_number
    print(n,', Vocabulary_diversity:',round(Vocabulary_diversity,2))

for i in range(1,10):
    textname='text'+str(i)
    text=eval(textname)#将字符串变量化(转成表达式执行)谨慎使用
    get_Vocabulary_diversity(text)

4.2 collections方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值