Python 02 基礎

來源:互聯網
上載者:User

標籤:append   編碼   基礎   退出   bsp   iba   cas   orm   move   

alex老師 http://www.cnblogs.com/alex3714/articles/5717620.html  武沛齊老師 http://www.cnblogs.com/wupeiqi/articles/4911365.html

列表

names = ["zhangsan","lisi","wangwu"]

取值,下標從0開始

names[0]

names[2]

names[-1]

切片

names[1:2]

names[1:-1]

names[1:]

names[0:2]

names[:2]

names[1:]

names[0::2]

追加

names.append("xiaoliu")

插入

names.insert(1,"laoqi")

修改

names[1] = "miba"

刪除

del names[2]

names.remove[2]

擴充

b = [1,2,3]

names.extend(b)

拷貝

names_copy = names.copy()

統計

names.count("zhangsan")

排序

names.sort()    #3.0裡不同資料類型不能放在一起排序了

反轉

names.reverse()

取下標
names.index("zhangsan")

元組

元組其實跟列表差不多,也是存一組數,只不是它一旦建立,便不能再修改,所以又叫唯讀列表

names = ("zhangsan","lisi","wangwu")

它只有2個方法,一個是count,一個是index

 

程式練習 

請閉眼寫出以下程式。

程式:購物車程式

需求:

  1. 啟動程式後,讓使用者輸入工資,然後列印商品列表
  2. 允許使用者根據商品編號購買商品
  3. 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 
  4. 可隨時退出,退出時,列印已購買商品和餘額

 

2. 字串操作   

特性:不可修改 

name.capitalize()  首字母大寫

name.casefold()    大寫全部變小寫

name.center(50,"-")  字串左右兩側各添加50個"-"

name.count("n")    統計字元n出現的次數

name.encode()    將字串編碼成byte格式

name.endswith("ng")    判斷是否以字元"ng"結尾

"Yue\tYue".expandtabs(10)    將\t轉換成10個空格

name.find(‘b‘)  尋找b,找到返回其索引位置,找不到返回-1

 

format :
    >>> msg = "my name is {}, and age is {}"
    >>> msg.format("alex",22)
    ‘my name is alex, and age is 22‘
    >>> msg = "my name is {1}, and age is {0}"
    >>> msg.format("alex",22)
    ‘my name is 22, and age is alex‘
    >>> msg = "my name is {name}, and age is {age}"
    >>> msg.format(age=22,name="ale")
    ‘my name is ale, and age is 22‘
format_map
    >>> msg.format_map({‘name‘:‘alex‘,‘age‘:22})
    ‘my name is alex, and age is 22‘

Python 02 基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.