Python基礎知識學習筆記

2021-08-21 13:16:27 字數 1957 閱讀 6504

#前置知識#

一切皆物件,有自己的屬性和方法

#變數#

變數:variable ,先賦值後使用

#字串#

轉義字元:' i\'m lzh'

格式化字串:print(f『my name is 』)

name[0:3] = lzh 包括前面不包括後面

#函式#

def triangel():

width=int(input("請輸入長"))

height=int(input("請輸入高"))

result=width*height//2    //表整除

print(str(result))

triangel()

#for迴圈#

letters=['a','b','c','d']

for letter in letters:

print(letter)

#range函式#

numbers=[x for x in range(1,41)]

print(numbers)

numbers=list(range(1,41))

print(numbers)

效果是一樣的,都是生成在1-40的list列表

#decorator裝飾器#    #增強程式的功能

@zhihu                                 #語法糖,讓**更方便簡潔

def answer1():

print("我也不知道")

answer1()

#class#

class phone():

def __init__(self):

self.name='iphone'

def call(self):

print("喂喂喂")

iphone7= phone()

iphone7.call()

#python對縮排要求非常嚴格

class phone():    #增加傳入引數,self預設自己傳入

def __init__(self,name):

self.name=name

def call(self):

print("喂喂喂")

htc_one=phone('htc one')

print(htc_one.name)

#類屬性,方法和例項屬性,方法#

class phone():

shape = 'rectangle'  #類屬性

def __init__(self,name):

self.name=name

@classmethod  #類方法

def call(self):

return "喂喂喂"

htc_one=phone('htc one')

print(phone.shape)

print(htc_one.call())

#module模組#

方法1:

import print_sth

from print_sth import *

print_sth.print_num()

print_sth.print_color()

方法2:

from print_sth import *

print_num()

print_color()

引入單個函式可改名

from print_sth import print_color as color

Python學習筆記 基礎知識

coding cp936 此行是為了和linux的平台保持相容,是用來告訴shell執行時,去用 bin python來解釋執行 bin python 此行指明了編碼方式,預設是utf 8,如果我們要輸出中文或者採用中文的注釋,就會出現錯誤 coding gb2312 python特色一 三引號注釋...

Python學習筆記 基礎知識

python中單行注釋以 開頭,多行注釋可以用多個 號,或者三個單引號或雙引號。我是注釋 我也是注釋 我也是注釋 我也是注釋 python使用縮排空格數來識別 塊而不是大括號,同乙個 塊的語句必須包含相同的縮排空格數。if true print hello world else print hell...

Python學習筆記一 基礎知識

lovely python整理 python 弱資料型別,不需要強制宣告 python 一切都是物件 pyhton 語句塊以 結尾,並且下句縮排 python 復用級別 行 函式 類 模組 python 支援函式 python kiss原則 keep it stupid 一 中文支援 二 使用模組 ...