***

5667 ワード

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)