python学习记录

python

第一章

优点:
可移植、可混合、

在使用时,使用TDLE比较方便
编程:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200126213513586.png?x-oss-process=image/watermark,type
V0L2xpdHV0dXlh,size_16,color_FFFFFF,t_70)

程序:print () 必须把字符串放在引号里
print可以直接输出数值、计算一个表达式的值并输出、可以接受多个参数并按顺序输出
*语句语法:*不同参数按逗号分隔开

图形化程序*

采用import关键字来引用
导入turtle模块import turtule
显示turtle当前的位置和方向 turtle.showturtle
输入命令绘制一个文本字符串 turtle.write(“welcome to”)
输入命令符将箭头向前移动100像素,同时绘制出一条直线 turtle.forward(100)
连续命令:右转120度,换成蓝色,移动100像素,右转120度,换成红色,移动100像素。
import turtle
turtle.showturtle()
turtle.write(“welcom to turtle”)
turtle.forward(100)
turtle.right(120)
turtle.color(“blue”)
turtle.forward(100)
turtle.right(120)
turtle.color(“red”)
turtle.forward(100)

练习一 画一个像素为200的十字
方法一
import turtle
turtle.showturtle
turtle.goto(0,0)
turtle.forward(100)
turtle.goto(0,0)
turtle.backward(100)
turtle.goto(0,0)
turtle.left(90)
turtle.forward(100)
turtle.goto(0,0)
turtle.backward(100)
方法二
import turtle
turtle.penup()
turtle.goto(-100,0)
turtle.pendown()
turtle.goto(100,0)
turtle.penup()
turtle.goto(0,100)
turtle.pendown()
turtle.goto(0,-100)
练习二 画一个五角星
方法一
import turtle
turtle.showturtle
turtle.goto(0,0)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
方法二
import turtle
for i in range(5):
turtle.forward(200)
turtle.right(144)

资料

有关turtle的函数

python学习记录_第1张图片

赋值

1、一个值可赋给多个变量
2、同时赋值 如:x,y=1,2 和x=1,y=2 不等价
3、如何交换两个变量 如:x,y=1,2 ; x,y=y,x

一个变量可以用大写字母表示
计算圆的面积
radius=eval(input("please enter a value for radius: "))
PI=3.1415926
if radius >0:
area=PIradiusradius
print("The area for the circle of radius ",radius,“is”,area)
else:
print(“Input error”)
解释:python的缩进四个空格键或者tab键
使用循环结构,可以无限使用。可计算圆的面积。
radius=eval(input("please enter a value for radius: "))
PI=3.1415926
while radius >0:
area=PIradiusradius
print("The area for the circle of radius ",radius,“is”,area)
radius=eval(input("please enter a value for radius: "))
程序开发过程
理解问题 、设计算法 、编写程序 、测试程序
练习题一
将10000存入银行,年利率固定为5%,多少年后可以翻倍
year=0
balance=10000
while balance < 20000:
year=year+1
interest = balance*0.05
balance = balance + interest
print(year)
练习题二
计算两个点之间的距离,画出两个点之间的连线。

import turtle

x1,y1=eval(input(“please input point:”))
x2,y2=eval(input(“please input point:”))
distance = ((x1-x2)**2+(y1-y2)**2)**0.5
print(“the distance between the two point”,distance)

turtle.penup()
turtle.goto(x1,y1)
turtle.pendown()
turtle.write(“point1”)
turtle.goto(x2,y2)
turtle.write(“point2”)

turtle.penup()
turtle.goto((x1 + x2) / 2,( y1 + y2 ) / 2)
turtle.write(distance)

python学习记录_第2张图片
数字类型
整数类型

python学习记录_第3张图片
浮点数类型
带有小数点和小数的数字
科学计数法用e或E表示
整除法含义python学习记录_第4张图片
python学习记录_第5张图片
int(x)将x转化为整数,x可以是整数或者字符串
float(x)将x转化为 浮点数,x可以是浮点数或者字符串
数学函数
abs(x): x的绝对值
divmod(x,y): (x//y,x%y)
pow(x,y[,z]): (x**y)%z […]表示该数可以省略。
round(x[,ndigits]): 表示与x最接近的整数
max() min()

math库的使用
引用函数
在这里插入图片描述
python学习记录_第6张图片python学习记录_第7张图片
练习题python学习记录_第8张图片
import math
x1,y1=eval(inpt("Enter point1(latitude and longitude) in degrees "))
x2,y2=eval(inpt("Enter point2(latitude and longitude) in degrees "))
x1 = math.radians(x1)
x2 = math.radians(x2)
y1 = math.radians(y1)
y2 = math.radians(y2)
RADIUS = 6371.3
d = RADIUS*math.acos(math.sin(x1)*math.sin(x2)+math.cos(x1)
*math.cos(x2)*math.cos(y1-y2))
print(“the distance between the two points is”,d,“km”)

字符串类型
在这里插入图片描述
转义字符:

+表示连接,*表示复制

str将 整数转化为字符串
print函数的sep和end

python学习记录_第9张图片
formastring%()
python学习记录_第10张图片
程序
a=123.1
b=123.4567
print(“a=%d,b=%.2f” % (123,123.456))

d和f是格式控制符
%d表示输出一个整数,%f表示输出一个浮点数
python学习记录_第11张图片
python学习记录_第12张图片
python学习记录_第13张图片
{} 可以放信息
方法的调用格式
object . method

s=“welcome”
s .lower()
python学习记录_第14张图片
使用turtle的复杂绘图
python学习记录_第15张图片
绘制奥运五环
python学习记录_第16张图片
import turtle
turtle.pensize(5)
turtle.color(“blue”)
turtle.penup()
turtle.goto(-110,-25)
turtle.pendown()
turtle.circle(45)
turtle.pensize(5)
turtle.color(“black”)
turtle.penup()
turtle.goto(0,-25)
turtle.pendown()
turtle.circle(45)
turtle.pensize(5)
turtle.color(“red”)
turtle.penup()
turtle.goto(110,-25)
turtle.pendown()
turtle.circle(45)
turtle.pensize(5)
turtle.color(“yellow”)
turtle.penup()
turtle.goto(-55,-75)
turtle.pendown()
turtle.circle(45)
turtle.pensize(5)
turtle.color(“green”)
turtle.penup()
turtle.goto(55,-75)
turtle.pendown()
turtle.circle(45)

画红色的三角形

turtle.penup()
turtle.goto(-100,55)
turtle.pendown()
turtle.setheading(60)
turtle.color(“red”)
turtle.begin_fill()
turtle.circle(100,steps=3)
turtle.end_fill()

逻辑运算
python学习记录_第17张图片
例题 判断年份是否为闰年,闰年条件是:能被4整除但不能被100整除,或者能被400整除。
程序
(y%4==0 and y%100!= 0) or y % 400 == 0

例题
输入两个整数据,放入a和b中,并使a中存入的数据小于b
程序
a=int(input("shu ru di yi ge: "))
b=int(input("shu ru di er ge: "))
print("chu li qian: ")
print(“a=%d,b=%d”%(a,b))
if a>b:
a,b=b,a
print("chu li hou: ")
print(“a=%d,b=%d”%(a,b))

python学习记录_第18张图片多分支语句
python学习记录_第19张图片
例题
计算分段函数
python学习记录_第20张图片
程序

x=float(input("shu fu x: "))
if x >=1:
y=x
elif x<=-1:
y=-x
else:
y=1
print(“y=%f”%y)
例题
判断某点(x,y)在第几象限
程序

x=int(input("x=: " ))
y=int(input("y=: " ))
if x0 and y0:
print(“wei yu yuan dian”)
elif y0:
print(“wei yu x zhou”)
elif x
0:
print(“wei yu y zhou”)
elif x>0 and y>0:
print(“yi”)
elif x<0 and y>0:
print(“er”)
elif x<0 and y<0:
print(“san”)
else:print(“si”)

嵌套if语句

python学习记录_第21张图片

常用字符串测试方法

python学习记录_第22张图片
for循环

在这里插入图片描述
使用while函数和for函数,分别算出某班的平均成绩。
for 程序
n=input("qing shu ru yi ge shu: ")
i=1
sum=0
while i<=n:
grade=float(input("qing shu ru xue sheng cheng jin: "))
sum=sum+grade
ave=sum/n
print(“xue sheng cheng ji de ping jun zhi wei=%.1f”%ave)

while程序
n=input("qing shu ru yi ge shu: ")
sum=0
for i in range(1,n+1):
grade=float(input("qing shu ru xue sheng cheng jin: "))
sum=sum+grade
ave=sum/n
print(“xue sheng cheng ji de ping jun zhi wei=%.1f”%ave)

循环嵌套
打印9-9乘法口诀表
程序
for i in range(1,10):
for j in range(1,i+1):
print(j,"",i,"=",ij,end= “\t”,sep="")
print()
在这里插入图片描述
break用来结束整个循环
continue用来结束当次循环
例题
求200以内能被17整除的最大的数
程序
for i in range(200,1,-1):
if i%17==0:
break
print("200yi nei neng bei 17 zheng chu de zui da de shu: ",i)

例题
求1-200所有素数之和,并输入个数。
程序
num=0
for i in range(2, 200):
k = True
for j in range(2, i):
if i % j == 0:
k = False
break
if k == True:
print("%d"%i)
num=num+1
print(“200以内素数的个数是%d”%num)

或者
import math
num=0
for i in range(2, 200):
m = int (math.sqrt(i))
k = True
for j in range(2, m+1):
if i % j == 0:
k = False
break
if k == True:
print("%d"%i)
num=num+1
print(“200以内素数的个数是%d”%num)
字符串的访问
python学习记录_第23张图片
python学习记录_第24张图片
循环实例
例题
请找出一段字符串的数字、大写字母、小写字母和数字的个数。
程序
a=b=c=d=0
str=input("please input a string: ")
for ch in str: #遍历输入的字符串
if’0’<=ch<=‘9’:
a=a+1
elif’A’<=ch<=‘z’:
b=b+1
elif’a’<=ch<=‘z’:
c=c+1
else:
d=d+1
print(“数字、大写字母、小写字母、其他个数是%d、%d、%d、%d\n”%(a,b,c,d))
random库
python学习记录_第25张图片
用蒙特卡诺方法算PI
程序
from random import random
from math import sqrt

DARTS=10000
hits=0.0

for i in range(1, DARTS + 1):
x, y=random(), random()
dist=sqrt(x ** 2 + y ** 2)
if dist <= 1.0:
hits = hits + 1
pi = 4 * (hits / DARTS)
print(“pi的值()”.format(pi))

数组比较
python学习记录_第26张图片
python学习记录_第27张图片
python学习记录_第28张图片
python学习记录_第29张图片
python学习记录_第30张图片

你可能感兴趣的:(python学习记录)