Python Phased Summary __python

Source: Internet
Author: User
Tags abs readline string format

two ways to call a library name

Import Turtle from
Turtle Import *

1.9*9 Multiplication Formula

PS: Note that indentation can not be changed, {: 2} ': ' Before no space meaning for the result to retain two-bit valid bit end= ' and no space can be added after the print{:2} space, the code more beautiful

For I in Range (1,10):
    for J in Range (1,i+1):
        print ("{}*{}={:2}". Format (j,i,i*j), end= ')
    print (')

2. Factorial summation

SUM, tmp = 0, 1 for
I in range (1,11):
    tmp *= i
    sum = tmp
print (' Result: {} '. Format (sum))    

3. The monkey eats the peach

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

4. Recipes

Diet = [' tomatoes ', ' cauliflower ', ' cucumber ', ' steak ', ' shrimp '] for
x in range (0,5): To
    y in range (0,5):
        if (x!=y):
            print (' {} {} '. Format (Diet[x],diet[y])

5. The pentagram draws

Color and FillColor difference, color can determine two is the brush color and fill color, FillColor can only determine the fill color

From turtle Import *
fillcolor (' red ')
Begin_fill ()-
True:
    forward Right
    (144)
    if ABS (POS ()) < 1:
        break
End_fill ()

6. Solar Flower Drawing

From turtle Import *
color (' red ', ' yellow ')
Begin_fill () while
True:
    forward (
    170
    If ABS (POS ()) < 1: Break
        End_fill () done
()

7. Spiral Drawing

Turtle.speed (' fastest ') is fast

Import Turtle
Import time
turtle.speed (' fastest ')
turtle.pensize (2) to
x in range (MB):
    Turtle.forward (2*x)
    turtle.left (Time.sleep)
(3)

8. Color Spiral Drawing

Turtle.tracer (False) is finished directly, the procedure is not displayed, only the results are displayed

Import Turtle
Import time
turtle.pensize (2)
turtle.bgcolor (' black ')
colors = [' red ', ' yellow ', ' Purple ', ' Blue ']
turtle.tracer (False) for
x in range ():
    turtle.forward (2*x)
    turtle.color (colors [x% 4])
    Turtle.left (Turtle.tracer)
(True)
Time.sleep (3)

9 float can be replaced with eval, evaluate string str as a valid expression, and return the result of the calculation.

Reference http://blog.csdn.net/u011138533/article/details/61920477

#tempConvert. py
val = input (' Please enter a temperature value (such as: 32C): ')
if val[-1] in [' C ', ' C ']:
    f = 1.8 * Float (val[0:-1]) + 32
  print (' converted temperature:%.2ff '%f)
elif val[-1] in [' F ', ' F ']:
    C = (float (val[0:-1)-))/1.8
    print (' Converted temperature is: %.2FC '%c]
else:
    print (' incorrect input ')

10. Python Drawing

Import Turtle def 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) Turtl E.FD (RAD*2/3) def main (): Turtle.setup (3100, 0, 0) pythonsize = turtle.pensize (pythonsize) Turtle . PenColor (' Blue ') Turtle.seth ( -40) Drawsnake (5, PYTHONSIZE/2) main () Color boa import Turtle def drawsnake (RA D, 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.se Tup (3100, 0, 0) pythonsize = turtle.pensize (pythonsize) turtle.seth ( -40) Drawsnake (+, 5, PYT HONSIZE/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.0
cnt = 0 line
= i Nfile.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 ("Please input 2 Num Sepratedbu a comma "))
  
Python's 6 data type numeric types,
string type
tuple types, list type
file types, dictionary types
Retell as complex
available type View data type
len can view string length
eval converts a string expression to an expression str converts a
number to a string    
Curly braces are called dictionaries dict brackets are called
lists list
parentheses are called tuples tuple cannot be modified
Python uses two string format methods
1. Similar to the C language, use%
2. Use the format () < template string >.format (< comma-delimited parameter >)
template strings are composed of a series of slots, Replace the parameter by ordinal. There is no ordinal in the slot, the
internal style of the slot is replaced sequentially by the:{< parameter serial number >:< format control Mark}
End a dead loop the program can use the CTRL + C
strip () string to remove characters that are drawn from the string, and the default is a space
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.