python初学小结一
python代码运行环境:PyCharm 2017.1.3
age=1.7 print(type (age )) 输出是 <class 'int'>
age=1.7 print(type (age )) 输出是 <class 'float'>
age = input("age:") 即使输入的是数字17,print(type (age )) 输出是 <class 'str'>
故要age = int (input(" age:")) 使其变成int类型
下面是三种类型但输出完全相同的代码:
name=input("name=")
age=int(input("age="))
job=input("job=")
info1='''
---------- info1 of %s-----
Name:%s
Age:%d
Job:%s
'''%(name,name,age,job)
info2='''
---------- info2 of {_name}-----
Name:{_name}
Age:{_age}
Job:{_job}
'''.format(_name=name,
_age=age,
_job=job)
info3='''
---------- info3 of {0}-----
Name:{0}
Age:{1}
Job:{2}
'''.format(name,age,job)#format执行字符串格式化操作
print(info1)
print(info2)
print(info3)
print(type(info))#<class 'str'>
部分输出:
name=mumu
age=0
job=st
---------- info1 of mumu-----
Name:mumu
Age:0
Job:st
python 中可以使用for...else与while...else,仅举例前者:
age= 17
for i in range (3):
guess_age = int(input("guess age:"))
if guess_age == age:
print("yes,you got it!")
break
elif guess_age > age:
print("Think smaller...")
else:
print("Think bigger...")
else:
print("you have trid too many times")
range:
for i in range (0,10,2)表示i从0开始取值,每次递增2,到10结束而不取10,如果print(i),结果是:0 2 4 6 8