1. 程式人生 > >Python相關基礎

Python相關基礎

line please .cn case 出現 num 位置 ron 功能

1》變量:

技術分享

技術分享

2》條件判斷與縮進:
  sex = raw_input("Please input your gender:")
  if sex == "girl":
    print ("I would like to have a little monker with jj!")
  elif sex == "man":
    print ("Going to homosexual!")
  else:
    print

("You are pervert!")
  =====================
  lunkey_number = 8
  number = input("Please input your guess number:")

  if number > lunkey_number:
    print ("Your guess number is too big,please input again:")
  elif number < lunkey_number:
    print
("Your guess number is too small,please input again:")
  else:
    print ("Your guess number is correct,you are lunkey!")

3》循環控制

技術分享

優化代碼:

技術分享

技術分享

4》常用數據類型:
  數字:
    Int
    Float
    Long
  布爾:
    真或假
    1或0
  字符串:
   1>字符串的格式化:

技術分享

2>字符串常用功能:
      移除空白:可以去掉兩邊空格,也可去掉兩邊字符;

技術分享

技術分享

分割:
       長度:
       索引:
       切片:
        1>固定頭部,不固定尾部,包含頭部,不包含尾部;

技術分享

技術分享

2>指定分割的距離,不包含指定所用的索引

技術分享

replace():轉換特定字符;可以指定位置,將前幾個替換;

技術分享

技術分享

splitlines():按照換行符來分割;

技術分享

技術分享

技術分享

swapcase():大小寫轉換;

技術分享

技術分享

列表:可以存多個信息
      append:追加
         eg:name_list.append("haha")
      count:計算,統計;計算其中某個元素有幾個
         eg:name_list.count("haha")
      extend:擴展

技術分享

index:索引,輸出最近的一個,如果有相同的
         eg:name_list.index("che")
       insert:插入,在某個位置插入一個元素
          eg:name_list.insert(2,"hhehe")
       pop:刪除一個,每次都刪除最後一個;
          eg:name_list.pop()
      remove:刪除指定的一個
          eg:name_list.remove("zhang")
      reverse:反轉,元素排序相反;

技術分享

sort:排序,安裝ASSCI碼排序,數字排在最前面;
        eg:a.sort()
     extend():擴展列表;

技術分享

技術分享

元組:
      tuple():可以將列表轉換為元組;
      count():查看某元素出現的次數;

技術分享

字典 :
       fromkeys():生成一個新字典;

技術分享

get():當key不存在時可以輸出一個默認值;

技術分享

技術分享

keys(),values(),items():

技術分享

技術分享

技術分享

pop():刪除某個鍵對,需要制定key值;

技術分享

技術分享

update():更新字典;

技術分享

Python相關基礎