1.python有两种编程方式:交互式和文件式
eg1:圆面积的计算
交互式:
>>> r=25
>>> area=3.1415*r*r
>>> print(area)
1963.4375000000002
>>> print("{:.2f}".format(area))
1963.44
文件式,新建Area_Circle文件(run输出结果和交互式一样)
r=25
area=3.1415*r*r
print(area)
rint("{:.2f}".format(area))
eg2:绘制同切圆
交互式:
>>> import turtle
>>> turtle.pensize(2)
>>> turtle.circle(10)
>>> turtle.circle(40)
>>> turtle.circle(80)
>>> turtle.circle(160)
文件式:新建TangentCircleDraw文件
import turtle
turtle.pensize(2)
turtle.circle(10)
turtle.circle(40)
turtle.circle(80)
turtle.circle(160)
运行结果:

当然了,交互式写IDLE的时候,这个圆,写出一行代码就画一个圆,文件嘞,就run的时候一起来,好玩。
eg3:绘制五角星
交互式:
>>> from turtle import *
>>> color('red','red')
>>> begin_fill()
>>> for i in range(5):
fd(200)
rt(144)
>>> end_fill()
文件式:新建StarDraw文件
from turtle import *
color('red','red')
begin_fill()
for i in range(5):
fd(200)
rt(144)
end_fill()
done()
运行结果:

2.实例:温度转换
算法简单,直接上代码咯!
#TempConvert.py
TempStr=input("请输入带有符号的温度值:")
if TempStr[-1] in ['F','f']:
C=(eval(TempStr[0:-1])-32)/1.8
print("转换后的温度值是{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
F=1.8*eval(TempStr[0:-1])+32
print("转换后的温度是{:.2f}F".format(F))
else:
print("输入格式错误")
下一步就是在分析中扒知识点,只是10行的代码,但是知识点都在了!
(1)程序的格式框架:
代码高亮:无所谓,不用管,无影响
缩进:
单层缩进多层缩进;
缩进是语法的一部分,缩进不正确程序会报错;
缩进表达所属关系层次关系;
缩进长度一致。
注释:
单行注释:以#开头,介个程序第一行就是注释
多行注释:以‘’‘开头和结尾。
(2)命名和保留字
变量:变量用标识符来表示,关联标识符的过程叫命名;=是赋值符号。
命名规则:大小写字母,数字,下划线,汉字;首字符不能是数字;大小写敏感;不能和保留字一样。
保留字:python本身保留了33个保留字。

(3)数据类型
整数:正负数
浮点数:带小数的正负数
字符串:用“ ”或者‘ ’包括的,单双引号无区别
正向递增序号:从0开始编号
反向递减序号:从末位开始从-1开始编号

1>索引:[M]
2>切片:[M:N](从M开始,不包括N)
列表:[ , , ,]
用in判断一个元素是否在列表中。
(4)语句与函数
赋值语句:=
分支语句:if,elif,else(注意冒号,冒号是语法一部分,不能省略)
函数:函数名()
(5)python程序的输入输出
1>input函数(输入):
=input()
2>print函数(输出):
一般使用格式:print()
格式化方法:print(“转换后的温度是{:.2f}C”.format©)
{}:槽,将后续变量C填充到槽中。
{:.2f}:将变量C填充到这个位置时取小数点后两位。
3>eval函数(评估函数):去掉参数最外侧引号并执行余下语句的函数eval()
eg1: >>>eval(“1”) 执行结果:1
eg2: >>>eval(“1+2”) 执行结果:3
eg3: >>>eval(’ “1+2” ') 执行结果:“1+2”
eg4: >>>eval( ’ print(“Hello”) ’ ) 执行结果:Hello
这些知识点搞到手,温度转换的代码就能看明白了。
3.课后作业们咯
练习1:Hello World 的条件输出

新建HelloWorld条件输出的文件
n = eval(input("请输入一个整数:"))
if n == 0:
print("Hello World")
elif n > 0:
print("He\nll\no \nWo\nrl\nd")
else:
print("H\ne\nl\nl\no\n \nW\no\nr\nl\nd\n")
运行结果

提交完以后,我看到参考答案给的else部分的代码是介样的。
else:
for c in "Hello World":
print(c)
练习2:数值运算

我爱eval函数!!!
新建数值运算的文件
s = eval(input("请输入一个运算式子的字符串:"))
print("{:.2f}".format(s))
运算结果

练习3:温度转换I(和实例一样,除了input函数中不用写提示语句,其他都一样)。
练习4:Hello World输出,就直接print函数就完事了。
练习5:数字形式转换

template = "零一二三四五六七八九"
s=input("请输入一个正整数:")
for c in s:
print(template[eval(c)], end="")
代码print()中的end=""参数表示输出后不增加换行,多个print()可以连续输出。
运行结果

练习6:温度转换II

TempStr=input() #不增加提示字符串
if TempStr[0] in ['C']:
F=eval(TempStr[1:])*1.8+32
print("F{:.2f}".format(F))
elif TempStr[0] in ['F']:
C=(eval(TempStr[1:])-32)/1.8
print("C{:.2f}".format(C))
else:
print() #不输入错误提示
TempStr[1:]表示字符串除首字符外的所有字符。
运行结果

练习7:货币转换

TempStr=input() #不增加提示字符串
if TempStr[0] in ['U']:
R=eval(TempStr[3:])*6.78
print("RMB{:.2f}".format(R))
elif TempStr[0] in ['R']:
U=eval(TempStr[3:])/6.78
print("USD{:.2f}".format(U))
else:
print() #不输入错误提示
和温度转换II那个题几乎一毛一样
运行结果

参考答案
CurStr = input()
if CurStr[:3] == "RMB":
print("USD{:.2f}".format(eval(CurStr[3:])/6.78))
elif CurStr[:3] in ['USD']:
print("RMB{:.2f}".format(eval(CurStr[3:])*6.78))
参考答案比较是直接比较RMB和USD三个字母,自己写的还是只比较了首字母,不愧是参考答案。
4.总结一下下
emmmm,怎莫说,数字形式转换那个题,我能感觉到是用了数组那种输出,但是有一丝丝迷茫,大概是学的少,还不太明白吧,现在的我本质上还是代码的搬运工,等我再往后学学,要是学明白了到时候再来评论补充。不过这第一周的课的话,练习题大概也就围绕温度转换那个实例来整的,反正那个是学明白了!