Python入門之if和while語句

2021-09-25 11:20:14 字數 2427 閱讀 1897

if語句,如果滿足某種情況就繼續往下執行**塊,break表示跳出迴圈,continue表示重新開始迴圈

while語句,用來在任何條件為真(需要定義iteration variables)的情況下重複執行乙個**塊,一般用作無限迴圈

a =

"python"

,"php"

type

(a)

tuple
s =

"python"

if"p"

in s:

s ="you need "

+ s

print

(s)

you need python
break表示跳出迴圈

a =

0while a <3:

#a表示iteration variables

s =input

("input your language:"

)if s ==

"python"

:print

("your lang is "

.format

(s))

break

else

: a +=

1print

("a = "

,a)

input your language:1

a = 1

input your language:python

your lang is python

continue表示繼續重新開始迴圈

a =

11while a >0:

a -=

1if a%2==

0:continue

else

:print

(a)

975

31

#隨機數

import random

number = random.randint(1,

100)

#建立從1開始的100的元素的行列式,即low=1,size=100

guess =

0while

true

: num_input =

input

("please input an iteger(1-100):"

) guess +=1if

not num_input.isdigit():

print

("please input integer"

)elif

int(num_input)

<0or

int(num_input)

>=

100:

print

("the number should be in 1-100."

)else

:if number ==

int(num_input)

:print

("ok.it\'s only "

.format

(guess)

)break

elif number >

int(num_input)

:print

("your number is smaller."

)else

:print

("your number is bigger."

)

please input an iteger(1-100):4

your number is smaller.

please input an iteger(1-100):50

your number is bigger.

please input an iteger(1-100):25

your number is smaller.

please input an iteger(1-100):38

your number is smaller.

please input an iteger(1-100):40

your number is smaller.

please input an iteger(1-100):45

your number is bigger.

please input an iteger(1-100):43

your number is bigger.

please input an iteger(1-100):42

ok.it's only 8

python基礎入門之字典和集合

python 中有6大標準型別 數字 number 字串 string 列表 list 元組 tumple 集合 set 字典 dictionary 前面已經介紹了上面4種,還有下面兩種來介紹一下。字典看起來非常像json,是的非常非常像,但是我們起碼需要明白的是json是乙個字元格式,而字典是一種...

Python入門之語句

前邊基本了解了python的基本資料結構,接下來我們可以安裝pycharm,也就是之前提到的整合開發環境 if 條件表示式 滿足條件表示式執行的語句 else 不滿足條件表示式執行的語句 name westos if name westos print hello westos else print...

python入門之函式

1.1 函式是一段功能 的封裝,可以被其他程式 重複呼叫。1.2 函式一般包括三要素 函式名 引數和返回值 建立函式要使用def關鍵字 例如 def say name print 名字叫做 format name return name 1 python中的單行注釋和多行注釋在在編譯後會被去掉,如果...