3.数据类型和变量---用Python做数学运算

变量----保存内容的地方

变量名---以字母为开头,其他的字符必须是字母、数字、下划线,区分大小写,中文也可以,但不推荐。

举例

my_name = "Bryson"

my_age = 43

your_name = input("What is your name? ")

your_age = input("How old are you? ")

print("My name is", my_name, ", and I am", my_age, "years old.")

print("Your name is", your_name, ", and you are", your_age, ".")

print("Thank you for buying my book,", your_name, "!")

​​​​​​​Python中的数字和数学运算

数字类型主要是整数和浮点数(带有小数点的数字)

简单的数据类型:布尔值(Boolean),存储了True或False

操作符:+ 、-、*、 /、 **求幂、 ()分组

表达式:数学题

练习:在命令行中输入数学题,计算结果

语法错误:Syntax Error,语法就是语句要遵守的规则

赋值语句:变量=表达式

真除法:Python的/是真除法,例如5/2=2.5

举例:养成写注释的习惯,程序的步骤够成了算法

# AtlantaPizza.py – a simple pizza cost calculator

# Ask the person how many pizzas they want, get the number with eval()

#eval()字符串变成数字

number_of_pizzas = eval( input("How many pizzas do you want: ") )

# Ask for the menu cost of each pizza

cost_per_pizza = eval( input("How much does each pizza cost: ") )

# Calculate the total cost of the pizzas as our subtotal

subtotal = number_of_pizzas * cost_per_pizza

# Calculate the sales tax owed, at 8% of the subtotal

tax_rate = 0.08 # we store 8% as the decimal value 0.08

sales_tax = subtotal * tax_rate

# Add the sales tax to the subtotal for the final total

total = subtotal + sales_tax

# Show the user the total amount due, including tax

print("The total cost is $", total)

print("This includes $", subtotal, "for the pizza and")

print("$", sales_tax, "in sales tax.")

​​​​​​​字符串---Python中真正的字符

# SayMyName.py - prints a screen full of the user's name
# Ask the user for their name
name = input("What is your name? ")
# Print their name 100 times
for x in range(100):
    # Print their name followed by a space, not a new line
    print(name, end = " ")

sep, end, file and flush, if present, must be given as keyword arguments.

关键字参数:end

字符串用双引号或者单引号引起来

​​​​​​​用字符串改进彩色螺旋线

# SpiralMyName.py - prints a colorful spiral of the user's name

import turtle # Set up turtle graphics

t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]

# Ask the user's name using turtle's textinput pop-up window

your_name = turtle.textinput("Enter your name", "What is your name?")

# Draw a spiral of the name on the screen, written 100 times

for x in range(100):
    t.pencolor(colors[x%4]) # Rotate through the four colors
    t.penup()   # Don't draw the regular spiral lines
    t.forward(x*4)  # Just move the turtle on the screen
    t.pendown() # Write the user's name, bigger each time
    t.write(your_name, font = ("Arial", int( (x + 4) / 4), "bold") )
    t.left(92)  # Turn left, just as in our other spirals

文本对话框:turtle.textinput("Enter your name", "What is your name?")

​​​​​​​列表---将所有内容放到一起

列表是一组值,用逗号隔开,放在方括号之间。

# ColorSpiralInput.py
import turtle                       # Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black")
# Set up a list of any 8 valid Python color names
colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "gray"]
# Ask the user for the number of sides, between 1 and 8, with a default of 4
sides = int(turtle.numinput("Number of sides",
                            "How many sides do you want (1-8)?", 4, 1, 8))
# Draw a colorful spiral with the user-specified number of sides
for x in range(360):
    t.pencolor(colors[x % sides])   # Only use the right number of colors
    t.forward(x * 3 / sides + x)    # Change the size to match number of sides
    t.left(360 / sides + 1)         # Turn 360 degrees / number of sides, plus 1
    t.width(x * sides / 200)        # Make the pen larger as it goes outward

    

数字对话框:int(turtle.numinput("Number of sides",  "How many sides do you want (1-8)?", 4, 1, 8))

 

​​​​​​​Python做作业

print("MathHomework.py")
# Ask the user to enter a math problem
problem = input("Enter a math problem, or 'q' to quit: ")
# Keep going until the user enters 'q' to quit
while (problem != "q"):
    # Show the problem, and the answer using eval()
    print("The answer to ", problem, "is:", eval(problem) )
    # Ask for another math problem
    problem = input("Enter another math problem, or 'q' to quit: ")
    # This while loop will keep going until you enter 'q' to quit 

整数除法://

eval():计算这样的表达式如:“5//2”

Guess you like

Origin blog.csdn.net/daqi1983/article/details/121249210