日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 条件循环赋值_python学习(五) 条件、循环和其他语句

發布時間:2023/12/16 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 条件循环赋值_python学习(五) 条件、循环和其他语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第五章 條件、循環和其他語句

5.1 print和import的更多信息

5.1.1 使用逗號輸出

>>> print('age',43,45) ? ? ? ? // 可以用逗號隔開多個表達式,中間會有空格

age 43 45

5.1.2 把某事件作為另外事件的導入

import somemodule

from somemodule improt aaa, bbb, ccc

from somemodule import *

如果兩個模塊有同名函數怎么辦?

第一種方法可以用模塊引用:

module1.open()

module2.open()

另外一種方法是:給模塊起別名,或者給函數起別名

>>> import math as foobar

>>> foobar.sqrt(3)

1.7320508075688772

5.2 賦值魔法

5.2.1 序列解包

>>> x,y,z = 1,2,3 ? ? ? ? ? ? ? // 可以為多個值同時賦值

>>> print(x,y,z)

1 2 3

>>> x,y ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 交換兩個值

(1, 2)

>>> x,y = y, x

>>> x, y

(2, 1)

序列解包(或遞歸解包):將多個值的序列解開,放到變量的序列中。

當函數或方法返回元組(或其他序列或可選迭代對象)時,這個特性尤為重要。

>>> source = {'name':'Robin','girlfriend':'marion'}

>>> key, value = source.popitem() ?// 函數返回的值打包成元組。

>>> key, value

('girlfriend', 'marion')

5.2.2 鏈式賦值

>>> x = y = "fsdfsd"

>>> x, y

('fsdfsd', 'fsdfsd')

5.2.3 增量賦值

+= ?/ ?*=

5.3 語句塊:縮排的樂趣

語句塊:條件為真時,執行或者執行多次的一組語句。在代碼前放置空格來縮進語句即可創建語句塊

python中,冒號用來標識語句塊的開始;塊中的語句都是縮進的,并且縮進量相同。當回退到和已閉合

的塊一樣的縮進量時,就表示當前塊已經結束。

5.4 條件和條件語句

5.4.1 這就是布爾變量的作用

真值:下面的值在作為布爾表達式的時候,會被解釋器看做假(false)

False ? ? ? ?None ? ? ?0(包括浮點,長整和其他類型) ? ? ? ? "" ? ? ? ? ? ? ? () ? ? ? ? ?[] ? ? ? ? ? ?{}

5.4.2 條件執行和if語句

5.4.3 else子句

5.4.4 elif

5.4.5 嵌套代碼塊

name= input("what is your name?")if name.endswith('Gumby'):if name.startswith('Mr.'):print("hello MR Gumyby")elif name.startswith('Mrs.'):print('Hello Mrs. Gumy')else:print('hello Gumby')else:print('hello, stranger')

name= input('jfsdljfs')

5.4.6 更復雜的條件

(1)比較運算符

x == y/ ? x y / x >=y / ? ? x<=y ? / x != ?y / ?x is y ?/ x is not y/ ? x in y ? ?/x not in y

0 < age < 100 ? // ?運算符可以連接

(2)相等運算符

==

(3)is: 同一運算符

>>> x = y = [1,2,3]

>>> z = [1,2,3]

>>> x == y

True

>>> x == z ? ? ? ? ? ? ? ? // ? 相等性判定,判斷數值是否相等

True

>>> x is y

True

>>> x is z ? ? ? ? ? ? ? ? ?// 同一性判定, 判斷對象是否是同一個

False

(4)in:成員資格運算符

(5)字符串和序列比較

>>> "abc" < "edf"

True

>>> "abc".lower() == "def".upper()

False

>>> [1,[1,3]] < [1,[1,4]]

True

(6)布爾運算符

if ? XX and YY ?or ZZ ? // 也滿足短路邏輯

5.4.7 斷言

>>> age = 10

>>> assert 100

Traceback (most recent call last):

File "", line 1, in

assert 100

AssertionError

5.5 循環

5.5.1 ?while循環

x = 1

while x <= 100:

print(x)

x +=1

5.5.2 for 循環

>>> words = ['this','is','an','ex','parrot']

>>> for word in words:

print(word)

this

is

an

ex

parrot

>>>

>>> for xx in range(10): ? ? ? ? // range函數類似于分片,包含下限,但是不包含上限

print(xx)

0

1

2

3

4

5

6

7

8

9

>>>

5.5.3 循環遍歷字典

>>> d = {'x':1, 'y':2, 'z':3}

>>> d

{'x': 1, 'y': 2, 'z': 3}

>>> for key in d:

print(key, " -- ",d[key]) ? ? ? ? // 字典的迭代順序是不一定的

x -- 1

y -- 2

z -- 3

>>> for key, value in d.items():

print(key, '-', value) ? ? ? ? ? ? // 字典的迭代順序是不一定的

x - 1

y - 2

z - 3

>>>

5.5.4 一些迭代工具

python中有一些函數非常好用,有些在itertools中,有一些是內建函數

(1)并行迭代

>>> names = ['anne','beth','george','damon']

>>> ages = [12, 45, 32, 102]

>>> for i in range(len(names)):

print(names[i],' ? ',ages[i])

>>> for name, age in zip(names, ages): ? // ?zip函數可以進行并行迭代,zip函數也可以用于不等長的列表,短的“用完”就停止

print(name,age)

anne 12

beth 45

george 32

damon 102

(2 ) 按索引迭代

strings = ['aa','bb','cc']

>>> for index, string in enumerate(strings):

if 'aa' in string:

strings[index] = "zzzz"

>>> strings

['zzzz', 'bb', 'cc']

(3)翻轉和排序迭代

reversed和 sorted函數

>>> sorted([4,6,9,2])

[2, 4, 6, 9]

>>> var = reversed('hello world')

>>> var

>>> list(var)

['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']

>>> ''.join(reversed('hello world'))

'dlrow olleh'

5.5.5 跳出循環

(1)break: 跳出循環

(2)continue: 跳出本次循環,執行下一輪循環

(3)while true用法

whileTrue:

word= input("please input a word?")if not word:break

print('this workd was' +word)

name= input('jfsdljfs')

5.6 列表推倒式-輕量級循環

利用其它列表創建新列表

>>> [x*x for x in range(0, 10)]

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> [x*x for x in range(0, 10) if x % 3 ==0] ? // 增加一個if部分

[0, 9, 36, 81]

>>> [(x,y) for x in range(0, 10) for y in range(0,4)]// 也可以有多個for語句

[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3), (4, 0), (4, 1), (4, 2), (4, 3), (5, 0), (5, 1), (5, 2), (5, 3), (6, 0), (6, 1), (6, 2), (6, 3), (7, 0), (7, 1), (7, 2), (7, 3), (8, 0), (8, 1), (8, 2), (8, 3), (9, 0), (9, 1), (9, 2), (9, 3)]

>>>

5.7 三人行

5.7.1 占位符:pass

var = "fsdf"

if var == "fsdfsd":print("fsd")elif var == "vcxfds":pass

elif var == "fsdfs":print("fsd")

name= input("fsdfsd")

5.7.2 ?使用del刪除

>>> x = ['hello','world']>>> y =x>>> dely //刪除y后,并不會影響x, 因為只是刪除y這個名字,而值不會刪除。值是通過垃圾回收來刪除的>>>x

['hello', 'world']

5.7.3 使用exec和eval執行和求值字符串

>>> exec("print('fds')") ?// 執行字符串中存儲的代碼,這樣會有安全漏洞

fds

>>> eval("5*5") ? ? ? ? // 和exec類似,不同點是,eval會計算值并且返回結果

25

總結

以上是生活随笔為你收集整理的python 条件循环赋值_python学习(五) 条件、循环和其他语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。