Topic:
- Hello world!
- Simple Interactive (interactive, file-style) textbook P19
- The user enters two numbers, calculates and outputs the sum of two digits (try to implement it in one line of code).
- The user enters the triangular three-side length and calculates the area of the triangle: (Helen Formula)
- Enter a radius to calculate the area of the circle.
- Draw a set of identical tangent circles
- Draw a Pentagram
- Draw an all-yellow pentagram
1: Output Hello world!
Print ('helloworld') Hello World
2: Simple interaction
Name = input (" Enter name:")print("{} Students, learn python, promising!") . Format (name)print("{} heroes, learn Python, promising!") . Format (name[0))print("{} brother, learn Python, promising!") ". Format (name[1:]))
Effect:
Interactive:
while a <: Print(A, end=',') = B, A + B0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
3: Add two numbers
>>> NUM1 = input (' Please enter first number:') Enter the first number:11>>> num2 = input (' Please enter a second number:') Enter the second number:12>>> sum = Int (NUM1) +int (num2)Print ("sum=%d"%(sum)) sum
One line of code implementation:
Print (' two and%.2f'% (float (input (' Enter first number:') ') +float (input (' Please enter the second number:'))) Enter the first number: Please enter the second number:two sum of each .
4: User input triangle three-side length, and calculate the area of the triangle: (Helen Formula)
A = float (input (' enter ' The first edge of the triangle:'= float (input (' Please enter the second side of the triangle length: '= float (input (' Enter ' triangle third side length:'= (a+b+c)/2= (p* (p-a * (p-b) * (P-C)) * * * 0.5print(' triangular area bit%0.2f' %area)
Results:
5: Enter the radius to calculate the area of the circle
Import= float (input (' Enter radius of circle:'= math.pi* (r**2) Print(' round area%0.2f'%area)
Effect:
6: Draw a same tangent circle
Import Turtle>>> turtle.circle (+)>>> turtle.circle (+)>>> Turtle.circle (+)
Effect:
7 Draw a Pentagram
Import Turtle>>> turtle.forward (+)>>> turtle.right (144)>>> Turtle.forward >>> turtle.right(144)>>> turtle.forward (+)> >> turtle.right (144)>>> turtle.forward (+)>>> turtle.right (144) >>> Turtle.forward (+)
Effect:
8: Draw a color-filled pentagram
>>>ImportTurtle>>> Turtle.fillcolor ("Yellow")>>> Turtle.pencolor ("Yellow")>>>Turtle.begin_fill ()>>> Turtle.forward (100)>>> Turtle.right (144)>>> Turtle.forward (100)>>> Turtle.right (144)>>> Turtle.forward (100)>>> Turtle.right (144)>>> Turtle.forward (100)>>> Turtle.right (144)>>> Turtle.forward (100)>>>Turtle.end_fill ()>>>
Effect:
Python input and output exercises, arithmetic exercises, turtle preliminary exercises