python初學小結一
阿新 • • 發佈:2017-10-16
表示 -- it! family 使用 arm -s while name
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
python初學小結一