1. 变量----保存内容的地方
变量名---以字母为开头,其他的字符必须是字母、数字、下划线,区分大小写,中文也可以,但不推荐。
举例
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, "!")
2. 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.")
3. 字符串---Python中真正的字符
# SayMyName.py - prints a screen full of the user's name
# Ask the user for their name
name = input("What is your name? ")
name2 = input("What is your name2? ")
# Print their name 100 times
for x in range(50):
# Print their name followed by a space, not a new line
print(name,name2,sep='--',end=" rules! ")
关键字参数:end
字符串用双引号或者单引号引起来