首页 > 编程语言 > 详细

新手學python之新體驗

时间:2019-08-31 09:53:34      阅读:58      评论:0      收藏:0      [点我收藏+]

1. 使用縮進方式做為程式塊開始結束的標示,程式換行在行末尾加 "\"

2. 元祖(Tuple)數據類型,和List的不同是Tuple不能修改,優點是執行速度比List快,因為不能修改也就比較安全,團隊開發某些情況會用到。

3. Dict字典類型,若鍵有重複時,後面的建值會覆蓋掉前面的。

dict = {"banana": 20, "apple": 30, "orange": 40, "banana": 30}
print(dict["banana"])  #30 

   字典類型的排列順序是隨機的,與設定的順序不一定相同,所以在讀取時就不能使用index。

dict = {"banana":20, "apple": 30}
result = dict.items()  # 取得以[鍵:值]為組合的Array
                               # [("banana":20), ("apple":30)]
result = dict.setdefault("apple", 50)  # 30
result = dict.setdefault("orange", 80)  # create new

4. python3 內建了SQLite, 非常方便储存數據,不需要再額外設定database環境

5. pyhon class 的 structure function.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

# the function called __init__(), which is always executed when the class is being initiated.

# Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created

# The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

# It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class

 

新手學python之新體驗

原文:https://www.cnblogs.com/sipher/p/11437791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!