python 階段性總結__python

來源:互聯網
上載者:User

庫名的兩種調用方式

import turtlefrom turtle import *

1.9*9乘法口訣

ps:注意縮排不能變,{:2} ‘:’前無空格意思為結果保留兩位有效位 end=”也無空格 可在print{:2}後加空格,代碼更加美觀

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

2.階乘累加

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

3.猴子吃桃

n = 1 for i in range(5,0,-1):    n = (n+1)<<1print(n)

4.菜譜

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

5.五角星繪製

color和fillcolor的區別,color可以確定兩個即畫筆顏色和填充顏色,fillcolor只能確定填充顏色

from turtle import *fillcolor('red')begin_fill()while True:    forward(200)    right(144)    if abs(pos()) < 1:        breakend_fill()

6.太陽花繪製

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

7.螺旋線繪製

turtle.speed(‘fastest’)速度很快

import turtleimport timeturtle.speed('fastest')turtle.pensize(2)for x in range(100):    turtle.forward(2*x)    turtle.left(90)time.sleep(3)

8.彩色螺旋線繪製

turtle.tracer(False)直接畫完,不顯示過程,只顯示結果

import turtleimport timeturtle.pensize(2)turtle.bgcolor('black')colors = ['red', 'yellow', 'purple', 'blue']turtle.tracer(False)for x in range(400):    turtle.forward(2*x)    turtle.color(colors[x % 4])    turtle.left(91)turtle.tracer(True)time.sleep(3)

9 float可用eval替換 ,將字串str當成有效運算式來求值並返回計算結果。

參考http://blog.csdn.net/u011138533/article/details/61920477

#tempConvert.pyval = input('請輸入溫度值(如:32C): ')if val[-1] in ['C', 'c']:    f = 1.8 * float(val[0:-1]) + 32    print('轉化後的溫度為: %.2fF'%f)elif val[-1] in ['F', 'f']:    c = (float(val[0:-1] - 32))/1.8    print('轉化後的溫度為: %.2fC'%c)else:    print('輸入有誤')

10.蟒蛇繪製

import turtledef drawSnake(rad, angle, len, neckrad):    for i in range(len):        turtle.circle(rad, angle)        turtle.circle(-rad, angle)    turtle.circle(rad, angle/2)    turtle.fd(rad)    turtle.circle(neckrad+1, 180)    turtle.fd(rad*2/3)def main():    turtle.setup(3100, 800, 0, 0)    pythonsize = 30    turtle.pensize(pythonsize)    turtle.pencolor('blue')    turtle.seth(-40)    drawSnake(40, 80, 5, pythonsize/2)main()彩色蟒蛇import turtledef drawSnake(rad, angle, len, neckrad):    colors = ['red', 'yellow', 'purple', 'blue', 'green']    for i in range(len):        turtle.circle(rad, angle)        turtle.circle(-rad, angle)        turtle.pencolor(colors[i%5])    turtle.circle(rad, angle/2)    turtle.fd(rad)    turtle.circle(neckrad+1, 180)    turtle.fd(rad*2/3)    turtle.done()def main():    turtle.setup(3100, 800, 0, 0)    pythonsize = 30    turtle.pensize(pythonsize)    turtle.seth(-40)    drawSnake(40, 80, 5, pythonsize/2)main()
# -*- coding: utf-8 -*-def main():    fileName = input("FileName")    infile = open(fileName, 'r')    sum = 0    cnt = 0    for line in infile:        sum += eval(line)        cnt += 1    print("Average Num is : ", sum/cnt)    main()def main():fileName = input("FileName")infile = open(fileName, 'r')sum = 0.0cnt = 0line = infile.readline()while line != "":    for xstr in line.split(","):        sum += eval(xstr)        cnt += 1    line = infile.readline()print("Average Num is : ", sum/cnt)main()num1, num2 = eval(input("Please input 2 num sepratedbu a comma"))
python 的6種資料類型數字類型、字串類型元群組類型、清單類型檔案類型、字典類型
複述表示為complex可用type查看資料類型len可查看字串長度eval 可把字串運算式轉化為運算式str 可將數字轉化為字串    
花括弧叫字典dict中括弧叫列表list小括弧叫元組tuple 不可修改
python採用兩種字串格式的方法1.類似C語言,使用%2.使用format() <模板字串>.format(<逗號分隔的參數>)模板字串用一系列的槽組成,按照序號進行參數替換。槽中沒有序號則按照順序依次替換槽的內部樣式為:{<參數序號>:<格式控制標記>}
結束一個死迴圈程式可以用 ctrl+cstrip()字串用於移除字串頭尾制定的字元,預設是空格
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.