1. 程式人生 > 其它 >折半查詢(二分法)

折半查詢(二分法)

Python學習筆記

2021/12/6

註釋

註釋以#開頭,python將忽略這一行
	 單行註釋:
	 	# 單行註釋
     多行註釋:
    	# 多行註釋1
        # 多行註釋2
        # 多行註釋3

變數

python只需要在給物件賦值的時候建立物件,並不需要給變數指定任何特定型別的宣告,甚至可以在設定後過呢更改型別。
	eg:
        x = 5 # x is of type int
        x = "Steve" # x is now of type str
        print(x)
··················································        
	字串變數可以用單引號/雙引號進行宣告
            x = "Bill"
            # is the same as
            x = 'Bill'		
··················································	
	可以一行中為多個變數賦值 
			x, y, z = "Orange", "Banana", "Cherry"
··················································
	函式的定義:
		def 函式名:
		(空格)函式體
	
	注意:在python中空格是控制程式碼塊的
··················································	
	區域性變數:
		當函式內外同時存在一個相同名稱的變數的時候,函式中呼叫的是函式內部的變數。
		eg:
            x = "awesome" #全域性變數
            def myfunc():#函式的定義
              x = "asd" 
              print("Python is " + x) 
            myfunc() #呼叫myfunc函式,輸出Python is asd #
            print("Python is " + x) #Python is awesome
··················································		
	全域性變數
		在函式外部建立的變數稱之為全域性變數,全域性變數可以在任何地方使用
		在函式內部建立的變數,在使用global關鍵字聲明後,該變數屬於全域性範圍
		eg:
                def myfunc():
                  global x
                  x = "fantastic"
                  print("Python is " + x) #Python is fantastic
                myfunc()
                print("Python is " + x) #Python is fantastic
                
	另外,如果要在函式內部改變全域性變數的值,使用global關鍵字引用該變數
		eg:
            x = "awesome"
            def myfunc():
              global x
              x = "fantastic"
            myfunc()
            print("Python is " + x) #Python is fantastic
	

資料型別

設定資料型別

設定特定的資料型別

如果希望指定資料型別,澤可以使用一下的建構函式:

數字

三種數字型別
	int(整數) 整數或負數,長度不限  x = 10
    float(小數) 包含小數的正數或負數 y = 2.1
    complex(複數) 用"j"作為虛部編寫 z= 2i
    
    驗證python中任何物件的型別,使用type()函式
    	eg:
			x = 10
            print(type(x)) 
··················································
型別轉換
	eg:
        x = 10 #整型
        y = 1.2#小數
        z = 1j#複數
        
        a = float(x)#將整數轉換成小數
        b = int(y)#將小數轉換成整數
        c = complex(x)#將整數轉換成複數
        
        print(a)
        print(b)
        print(c)
        
        print(type(a))
        print(type(b))
        print(type(c))