Changing the value of a variable

Many programming languages are statically typed languages where the variable is initially declared with a specific type, and during its lifetime, it must always have that type.

But in Python, variables are dynamically typed and not subject to the data type restriction. A variable may be assigned to a value of one type, and then later, we can also re-assigned a value of a different type. Let's see the example.

var = 10
print(var) # 10

# print its type
print(type(var)) # <class 'int'>

# assign different integer value to var
var = 55
print(var) # 55

# change var to string
var = "Now I'm a string"
print(var) # Now I'm a string
# print its type
print(type(var)) # <class 'str'> 

# change var to float
var = 22.37
print(var) # 22.37
# print its type
print(type(var)) # <class 'float'>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值