转自:微点阅读 https://www.weidianyuedu.com/content/5817414041459.html
变量与数据类型
我们在使用python的时候会面对许多数据,有的时候这些数据很难处理,这些数据就称为变量(variable),从某种程度上讲这些变量也就是数据的载体,当我们把固定的数值放入这些载体中,会使我们更加容易地去处理大量的数据。变量是在python是在中非常重要的一个因素。
例如输入一段文字:
print("There once a man called LITONG")print("He is 23 years old")print("He love the name LITONG")print("He dislikes the age 23")
run之后:
在这段对话中我想改变一下主人公的名字,把主人公的名字改成:kenway,年龄改成18。当然我可以直接选择把里面的每一个LITONG手动换成kenway,年龄也是如此。假如这一段对话中出现了无数次主人公的名字和他的年龄,手动替换就会非常麻烦,这时候主人公的名字和他的年龄就是一个变量。
创建变量:
在创建变量之前我们需要一定的信息,在这里面变量的名字可以设为:people name和people age
变量要在文本上方创建
people_name = "kenway"people_age ="18"print("There once a man called LITONG")print("He is 23 years old")print("He love the name LITONG")print("He dislikes the age 23")
变量设置好以后,分别使用" + people_name + " " + people_age + "
替换掉文本中人物名字和年龄的位置
Run之后,名字和年龄就被kenway和18替换了
people_name = "kenway"people_age ="18"print("There once a man called " + people_name + " ")print("He is " + people_age + " years old")print("He love the name " + people_name + "")print("He dislike the age " + people_age + "")
这时我们就可以在上方的变量处填写任何数值或任何内容,并带入到文本或数据中。
数据也可以分开进行变量设置,只要回车分开文本,在新的地方添加变量就好,
people_name = "kenway"people_age ="18"print("There once a man called " + people_name + " ")print("He is " + people_age + " years old")people_name = "edward"people_age ="44"print("He love the name " + people_name + "")print("He dislike the age " + people_age + "")