python基礎知識(一)運算符-字符串詳解

python基礎知識

    

一:python簡單介紹

a.python基礎

-基礎練習

-基礎數據類型

-函數

-面向duix

b.網絡編程

c.web編程

d.設計模式與算法

介紹:Python是一種解釋型、面向對象、動態數據類型的高級程序設計語言。

   學習python課程,從基礎知識到web項目開發以及爬蟲系統項目代碼開發實踐課程記錄。

二:本次python開發版本3.0+ ,python安裝不進行演示,使用開發工具pycharm

三:執行python

變量:

n1 = input("輸入變量名")
print(n1)

    #變量構成:

        字母

    數字

    下劃線

    不能使用數字開頭,不能使用python關鍵字


    字符串:

name = "hello"

    運算符:

       + - *  / % **

        in  not in 判斷字符是否包含在其中

		n1=9
		n2=2
		n3=n1+n2
		n3=n1-n2
		n3=n1*n2
		n3=n1/n2
		n3=n1%n2
		n3=n1**n2

    循環:

死循環:
while 1==1:
	print("ok")
循環遍歷0到9
count=0
while count<10:
	print(ok)
	count += 1
1、輸出1-100內的所以偶數
    count=1
    while count<=100:
        if(count%2!=0):
            pass
        else :
            print(count)
        count = count+1
2、求1-2+3-4+5...99的所有數的和
    count=1
    sum1=0
    sum2=0
    while count<100:
        if(count%2!=0):
            sum1=count+sum1
        else :
            sum2=count+sum2
        count = count+1
    print(sum1-sum2)

    判斷:

name = xixihaha
if "xi" not in name:
	print("ok")
else :
	print("error")

    布爾值: 真True 假 False

    邏輯運算 : not   and   or  

    數據類型:

        str:

        test="alex"
	test.capitalize() 首字母大寫,其他小寫
	test.casefold()   全部小寫(牛)
	test.lower()      全部小寫
	test.upper()      全部大寫
	test.swapcase()   大小寫轉換
	test.islower()    是否全是小寫
	test.isupper()    是否全是大寫
	test.center(20,"*")設置寬度,並將內容居中,20代指總長度,* 空白位置填充(一個字符)
	test.count("a")   計算字符中出現個數從0開始
	test.count("a",5,7) 從第5位開始,到第7位結束
	test.endswith("a") 以什麼結尾,返回bool值
	test.startswith('x')以什麼開始,返回bool值
	test.find("ex")   從開始到最後,找到第一個後獲取其位置,未找到返回-1
	test.find("ex",3,7) 從第3位開始,到底7位結束
	
	格式化,將一個字符串的佔位符替換爲指定的值
	test='i am {name},age {age}'
	test.format(name='alex',age='20')
	格式化,將一個字符串的佔位符替換爲指定的值
	test='i am {0},age {1}'
	test.format('alex',10)
	格式化,傳入的值{"name":"xie","age":23}
	test='i am {name},age {age}'
	test.format('alex',10)
	test.format_map({"name":"xie","age":23})
	
	識別\t ,\n 等標籤
	test="1234567\t89"
	test.expandtabs(2)空白位置數
	
	join插入,將字符串的每一個元素安裝指定分隔進行拼接
	test="我就是我不一樣的煙火"
	t=''
	t.join(test)   #等同於 "".join(test)
	t='_s'
	test.join(t)
	
	分隔
	test="testegesjuisfsgwsfhwrweydasgstee"
	test.partition("s")#只能把字符串分隔爲三份
	test.rpartition()
	test="testegesjuisfsgwsfhwrweydasgstee"
	test.split("s")#全部找到進行分隔,無法保留分隔符“s”
	test.split("s",1)
	test.rsplit()

	test="alex" #索引,下標,獲取字符串中的某一個字符
	test[2]
	test[0:2]# 0<= val <2
	test[0:-1] #切片

	字符串一旦創建,不可以修改
	一旦修改或者拼接,都會造成重新生成字符串
	#替換
	test="alexalexalex"
	test.replace("ex","bbb")#全部替換
	test.replace("ex","bbb",1)#替換第一個
	test.replace("ex","bbb",2)#替換前兩個


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章