PYTHON 基础: 变量与数据类型

转载自品略图书馆  http://www.pinlue.com/article/2020/07/0114/0910908378306.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 + "")

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值