學python的第五天

2022-09-25 19:12:11 字數 2832 閱讀 9199

一、字串操作(三)

# 1、切分字串

language = "python and j**a and c++ and golang and scala"

# split 切割字串 生成乙個列表: 暫時理解為乙個容器 有序序列

result1 = language.split("and")

print(result1)

# 連線序列 生成字串 跟split 是相反的操作

lang = ["english", "chinese", "jananese"]

# 2、通過 - 連線上面的語言 形成字串

result2 = "-".join(lang)

print(result2, type(result2))

# 3、刪除字串倆邊的空格 strip

class_name = " big data"

print(len(class_name))

# 刪除倆邊空格

class_name_new = class_name.strip()

print(class_name_new, len(class_name_new))

# 4、判斷乙個字串是否以指定子串開始

mystr = "hello world"

# mystr 以hello開始 則返回true

print(mystr.startswith("holle"))

# 倍數以world開口 則返回false

print(mystr.startswith("world"))

# 以world結束 返回true

print(mystr.endswith("world"))

# 判斷在指定範圍內是否以hello開始

print(mystr.startswith("hello", 3, 8))

print(mystr.startswith("lo", 3, 8))

二、列表操作(一)

三、列表操作(二)

# 1、刪除列表

name_list1 = ["張飛", "關羽", "劉備"]

print("刪除前:", name_list1)

del name_list1

# 刪除之後 name_list1 不存在 報錯

# print("刪除後:", name_list1)

# 刪除列表中的指定下標的元素

# 0 1 2 3

name_list2 = ["孫悟空", "唐僧", "八戒", "沙僧"]

del name_list2[1]

print(name_list2)

# 刪除掉指定下標的元素 然後返回該元素

result1 = name_list2.pop(1)

print(name_list2)

print(result1)

# pop 裡面沒有引數 則預設刪除列表中的最後乙個元素 然後返回該元素

name_list3 = ["帥帥", "東東", "根根"]

result2 = name_list3.pop()

print(result2)

print(name_list3)

# remove刪除指定元素 沒有返回值

name_list4 = ["田田", "豪豪", "浩浩"]

name_list4.remove("豪豪")

print(name_list4)

# 清空列表 沒有返回值

name_list4.clear()

print(name_list4)

# 2、修改列表 0 1 2

name_list5 = ["孝孝", "昊昊", "小仙女"]

name_list5[0] = "榮榮"

print(name_list5)

# 3、列表翻轉 沒有返回值

name_list5.reverse()

print(name_list5)

# 4、排序 預設是從小到大

score_list = [35, 89, 77, 0]

score_list.sort()

print(score_list)

# 從大到小進行排序

score_list.sort(reverse=true)

print(score_list)

# 5、複製列表

height_list = [183, 155, 185, 145]

height_list_new = height_list.copy()

print("新的複製列表:", height_list_new)

print("原來的列表:", height_list)

四、列表迴圈

# while 迴圈列表    0       1       2       3

country_list = ["烏克蘭", "俄羅斯", "漂亮國", "中國"]

i = 0

while i < len(country_list):

print(i, country_list[i])

i += 1

print("***********************************")

# for迴圈 迴圈列表

scenery_list = ["船舶大樓", "毛家屋場", "白鹿寺", "秀峰公園"]

# 通過j這個臨時變數 挨個地取列表中取資料 從頭到尾 沒有更多資料之後結束

for j in scenery_list:

print(j)

五、列表巢狀

第五天學python

1 切分字串 language python and j a and c and golang and scala split 切割字串 生成乙個列表 暫時理解為乙個容器 有序序列 result1 language.split and print result1 2 連線序列 生成字串 跟split...

第五天開始學python

多路分支 if 條件表示式 語句1elif 條件表示式 可以有很多個 語句2elif 條件表 達式 語句3else 條件表示式 只會執行一種情況 迴圈語句 重複執行某乙個固定的動作或任務 分為for和while for 變數 in 序列 語句1 語句2 l 1 2,3 4,5 6,7 for i i...

立習習學python 第五天

推導式建立序列 函式用法和底層分析 for 迴圈和可迭代物件遍歷 for迴圈常用於可迭代物件的便利。可迭代物件 可以被迴圈的物件。python中,可迭代物件包含 1 序列 字串 元組 列表 2 字典 3 迭代器物件 4 生成器函式 for迴圈語法格式 for 變數 in 可迭代物件 迴圈體語句 ra...