1. 程式人生 > >python學習總結(一)

python學習總結(一)

1.計算圓的面積

radius = 25
area = 3.1415 * radius * radius
print("{:.2f}".format(area))

2.簡單的人名對話(切片使用)

name = input("inptu your name:")
print("{}同學,學好python,前途無量".format(name))
print("{}同學,學好python,前途無量".format(name[0]))
print("{}同學,學好python,前途無量".format(name[0:4]))
print("{}同學,學好python,前途無量".format(name[3:5]))

3.斐波那契數列計算

a,b = 0,1
while a<1000:
    print(a,end=',')
    a,b = b,a+b

4.同切圓的繪製

import turtle
turtle.pensize(2)
turtle.circle(10)
turtle.circle(20)
turtle.circle(40)
turtle.circle(60)
turtle.circle(80)
turtle.circle(100)
turtle.circle(120)
turtle.circle(140)
turtle.circle(160)
turtle.circle(180)

5.日期和時間輸出

from datetime import datetime
now = datetime.now()
print(now)
now.strftime("%x")
now.strftime("%x")

6.太陽花繪製

from turtle import *
color('red','yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

7.菜譜組合

dite= ['西紅柿','花椰菜','黃瓜','牛排','蝦仁']
for x in range(0,5):
    for y in range(0,5):
        if not(x==y):
            print("{}{}".format(dite[x], dite[y]))

8.求1!+2!+3!+…+10!

sum,tmp=0,1
for i in range(1,11):
    tmp=tmp*i
    sum+=tmp
print("運算結果為:{}".format(sum))

9.九九乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{} = {:2}\t".format(j,i,j*i),end=' ')
    print(' ')

10.溫度單位轉換

def tempConvert(ValueStr):
    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 = (eval(TempStr[0:-1]))*1.8+32
       print("轉換後的溫度:{:.2f}F".format(F))
    else: print("輸入格式有錯")
TempStr = input("請輸入帶有符號的溫度值:")
tempConvert(TempStr)

11畫蛇(turtle的使用)

import turtle
def drawSanke(radius,angle,length):
    turtle.seth(-40)
    for i in range(length):
        turtle.circle(radius,angle)
        turtle.circle(-radius,angle)
    turtle.circle(40,80/2)
    turtle.fd(40)
    turtle.circle(16,180)
    turtle.fd(40*2/3)
turtle.setup(650,350,200,200)
#turtle.fd(250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
drawSanke(40,80,4)
turtle.done()

12.turtle的使用

import turtle
turtle.pendown()
turtle.forward(50)
turtle.seth(50)
turtle.forward(50)
turtle.circle(40)

13.turtle的使用(三)

"""import turtle
turtle.color('orange')
turtle.pensize(3)
turtle.circle(75)
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.circle(100)
turtle.done()
"""
import turtle
turtle.color('purple')

for i in range(10):
    turtle.speed(5)
    turtle.circle(i*20)
    turtle.pensize(2)
    turtle.left(126)#逆時針移動126度
turtle.clear()#清空turtle視窗,但是turtle的位置和狀態不會改變
turtle.done()

14.正方式螺旋線

import turtle as t
t.pen(speed=0)   #加快繪圖速度
t.penup()
t.goto(-200, -200)     #以左下角某處為起點
t.pendown()
t.seth(0)
length = 400
while (length !=0):    #利用正方形螺旋線的性質來繪圖
    t.fd(length)
    t.left(90)
    length -= 2.5
t.hideturtle()         #繪圖結束後把海龜頭(筆觸頭)隱藏起來
t.done()               #繪圖結束後使視窗停留

15.同心圓繪製

from turtle import *
setup(600,400,500,200)
color("red")
pensize(2)
circle(20)
pensize(2)
for i in range(1,9):
    penup()
    goto(0,-10*i)
    pendown()
    circle(20+i*10)
done()

16.工作效益計算

import math
dayup = math.pow((1+0.01),365)
daydown = math.pow((1-0.01),365)
print("一年以後的工作效益")
print("天天堅持做的效益:")
print("up:{:.2f}\tdown:{:.2f}".format(dayup,daydown))
dayup1,dayfactor=1.0,0.01
for i in range(1,366):
    if i%7==1 or i%7==2 or i%7==3 or i%7==4 or i%7==5:
        dayup1 = dayup1*(1+dayfactor)
    else:
        dayup1 = dayup1*(1-dayfactor)
print("工作日學習,週末休息")
print("up: {:.6f}".format(dayup1))

17.凱撒密碼轉換(明文)

plaincode = input("請輸入明文: ")
for p in plaincode:
    if ord("a") <= ord("p") <= ord("z"):
        print(chr(ord("a")+(ord(p)-ord("a")+3)%26),end = '')
    else:
        print(p,end='')

#ord()函式把單字元轉換成為unicode編碼
#chr()把unicode編碼轉換成為單字元

18.顯示進度條

import time
scale = 10

print("開始執行".center(scale//2,'*'))
t = time.clock()
#print("{0:*^20}".format('開始執行'))
for i in range(scale+1):
    a = (i/scale)*100
    b, c = '**'*i,'--'*(scale-i)
    t -= time.clock()
    print("%{:^3.0f}\t||{}》》{}{:.2f}".format(a,b,c,-t))
    time.sleep(0.05)
print("{0:*^20}".format('執行結束'))

20.利用隨機分佈計算π值

from random import random
from math import sqrt
from time import clock

DATAS = 10000
hits = 0
#clock()
for i in range(1,DATAS+1):
    x = random()
    y = random()
    s = x**2 + y**2
    if s<=1:
        hits = hits + 1
pi = 4* (hits/DATAS)
print("pi的值是: {:8f}".format(pi))
#print("執行時間:{}".format(clock()))

21.單行動態重新整理(IDLE本身遮蔽了單行重新整理功能)

import time
for i in range(101):
    print("\r{:2}%".format(i), end="")
    time.sleep(0.05)