程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning (a)

編輯:Python

pip學習

  1. 查看pip安裝列表
    >pip list
    
  2. pip安裝軟件包
    • Install the latest version of the package
      >pip install redis
      
    • 安裝指定版本包
      >pip install redis==3.2.0
      
  3. 卸載包
    >pip uninstall redis
    
  4. pip升級
    >python -m pip install --upgrade pip
    

字符串

python代碼

person = "小小"
address = "中華路"
phone = "13877775555"
num = 10
# 字符串連接 
print("recipient of the order:"+person+",地址:"+address+",電話:"+phone)
# The type of the concatenation on both ends of the plus sign must both be strings
# print("recipient of the order:"+person+",地址:"+address+",電話:"+phone+",商品數量:"+num)
''' 最後連接的num是整型,So it will report a type error Traceback (most recent call last): File "print.py", line 11, in <module> print("recipient of the order:"+person+",地址:"+address+",電話:"+phone+",商品數量:"+num) TypeError: can only concatenate str (not "int") to str '''
# 強制轉換 int ==> str
print("recipient of the order:"+person+",地址:"+address+",電話:"+phone+",商品數量:"+str(num))
# 格式化輸出 %s 字符串 %d 整型 %f 浮點型
print("recipient of the order:%s,地址:%s,電話:%s,商品數量:%s" % (person,address,phone,num))
# 格式化輸出 The underlying automatically coerces other types to string types
isMarry = False
print("Are you married? 回答:%s" % isMarry) # Strong bottom turn str(False) --> 'False' 
# 結果: Are you married? 回答:False
# format %d digit 整型
age = 13
print("你的年齡是:%d" % age)
# 強轉成int型 
age = 3.14
print("你的年齡是:%d" % age)
#結果: 3
# 格式化輸出 %f 浮點型
money = 123.36873
print("金額:%.2f" % money) # 保留小數點後2位,並四捨五入
# 結果: 123.37
# 練習 字符串原樣輸出
# 電影名稱:葉問
# 庫存:39
# 票價:19.9
# total list price:庫存 * 標價
name = "葉問"
count = 39
price = 19.9
total = count * price
msg = ''' 電影名稱:%s 庫存:%d 票價:%.1f total list price:%.2f ''' % (name,count,price,total)
print(msg)
# 結果:
# 電影名稱:葉問
# 庫存:39
# 票價:19.9
# total list price:776.10

字符中.format函數

name = "小小"
age = 18
movie = "看電影"
money = 15.98
# 字符串.format() 函數
msg = "{}今年{}歲了,喜歡{},有{}零花錢".format(name,age,movie,money)
print(msg)
# 結果:小小今年18歲了,喜歡看電影,有15.98零花錢

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved