python语音变量命名规则_Python语言之变量2(命名规则,类型转换)

1.命名规则

1.起始位为字母(大小写)或下划线('_')

2.其他部分为字母(大小写)、下划线('_')或数字(0-9)

3.大小写敏感

2.先体验一把:

#Ask the user their name

name = input("Enter your name: ")

#Make the first letter of their name upper case#and the rest of the name lower case#and then print it

print( name[0].upper() + name[1:].lower() )

其中#表示注释。

注意python 2.7.8 shell 中输入名字的时候需要用引号表明自己输入的是一个字符串,否则IDLE会认为你输入的是未命名变量,报错(name '***' is not defined)

3.类型转换

代码:

num = input("Enter a number: ")

answer = num*3

print("Your number tripled is:" + answer)

当你输入1的时候,是不是等待程序为你输出一个3呢?

结果:

python 3.4.2 shell 中程序返回 111,将1当做string类型的对象来对待。

python 2.7.8 shell中程序报错了(cannot concatenate 'str' and 'int' objects),告诉你无法连接一个字符串“Your ...”和一个整数answer。

不知道当你看到python各版本给出的各不相同的表现的时候,是不是像我一样感到很头大。。所以下面的编程方法是我强烈推荐的:显式的给出所有可能会产生误解的变量类型。

num = int( input("Enter a number: ") )

answer = num*3

print("Your number tripled is: " + str(answer) )

对于input为什么会在不同的python版本里有不同的表现,我计划以后详细讨论。现在刚刚开始学习,需要适当放过一些疑惑。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值