2017年9月18日+2017年9月14日
2017年9月18日
1.实例:输出12个星座符号,以反斜线分隔。
for i in range(12): print(chr(9800+i),end='\\')
2.实例:恺撒密码的编码
s=input('输入一个明文:') print('输出明文:',end=' ') for i in s: if ord('a')<=ord(i)<=ord('z'): print(chr(ord('a')+(ord(i)-ord('a')+3)%26),end=' ') else: print(i,end= ' ')
3.99乘法表
for i in range(1,10): for j in range(1,i+1): print(j,"*",i,"=",i*j," ",end='') print(end='\n')
4.输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
s = input('input your name:') s = str(s) print('{0: ^75}'.format(s))
5.格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
print("中华人民共和国国内生产总值(GDP):{0:^-10,.3f}亿元".format(689,136.89))
2017年9月14日
1.用循环花五角星
import turtle turtle.color("red") turtle.fillcolor("red") turtle.begin_fill() for i in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill()
2.用循环画同心圆
import turtle for i in range(2,10): turtle.up() turtle.goto(0,-20*i) turtle.down() turtle.circle(20*i)
3.用WHILE循环花太阳花
from turtle import * color('red','yellow') begin_fill() while True: forward(200) left(170) if abs(pos())<1: break end_fill()
4.用函数定义画五个五角星
from turtle import * setup(600,400) bgcolor('red') color('yellow') def h_draw(r): begin_fill() for i in range(5): forward(r) right(144) end_fill() def h_goto(x,y,z): up() goto(x,y) setheading(z) down() h_goto(-260,120,0) h_draw(120) h_goto(-110,160,40) h_draw(40) h_goto(-65,125,10) h_draw(40) h_goto(-55,55,40) h_draw(40) h_goto(-110,15,20) h_draw(40)
5.字符串操作
a.输入学号,识别年级、专业、序号
a=input('请输入你的学号:') print('你的年级是{}级'.format(a[2:4])) print('你的专业序号是{}'.format(a[8:10])) print('你的班级学号是{}'.format(a[10:]))
b.输入1-7的数字,输出对应的“星期几”
s="星期一星期二星期三星期四星期五星期六星期天" i=int(input("请输入(1-7):")) if(0<i<8): print(s[-3+3*i:0+3*i]) else: print("输入有误!") 复制代码
c.识别身份证号中的省市区、年龄、性别
ID=input('请输入十八位身份证号码: ') if len(ID)!=18: print("错误的身份证号码!!") ID_add=ID[0:6] ID_birth=ID[6:10] ID_sex=ID[14:17] if int(ID_add)==440101: print("省市区:广东省广州市市辖区") elif int(ID_add)==440102: print("省市区:广东省广州市东山区") elif int(ID_add)==440103: print("省市区:广东省广州市荔湾区") elif int(ID_add)==440104: print("省市区:广东省广州市越秀区") elif int(ID_add)==440105: print("省市区:广东省广州市海珠区") birth=2017-int(ID_birth[0:4]) print("年龄:{}".format(birth)) if int(ID_sex)%2==0: print('性别:女') else: print('性别:男')
d.用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)
a="https://docs.python.org/3.6/library/index" b=".html" address=a+b print(address)