1.1變數
常量變數:
變數名稱:
1、不使用保留名
2、變數命名規定
必須以字母或者下劃線開頭
必須包含字母、數字、下劃線
大小寫敏感
保留名賦值操作
1.2型別
整數、浮點數、字串、布林值、空值
使用者輸入type()
##輸出型別
int(
)##轉為整型
float()
##轉為浮點型
str(
)##轉為字串
input()
##輸入資訊
格式化input
('who\n'
)
if函式注意冒號和縮排,一層縮排是乙個tab或者四個空格,減少一層縮排表示**塊結束a=
0.6
b=12
c=a*b
('%.3f'
%c)
##巢狀條件x=
5if x>10:
('big'
)if x<10:
('small'
('finish')
small
finish
大於5x=
6if x>5:
('大於5'
)if x==6:
('等於6'
)
等於6##雙路判斷
大於5x=
6if x>5:
('大於5'
)else
('小於5'
)
##多路判斷
大於5x=
6if x>5:
('大於5'
)elif x>3:
('大於3'
)else
('小於等於3'
)
##作業:
分數錯誤x=-1
if x>
100or x<0:
('分數錯誤'
)elif x>=85:
('優秀'
)elif x>=60:
('及格'
)else
('不及格'
)
##捕捉異常
wrongtry
:print(1
/0)except
('wrong'
)
##生肖換算
animals=
['鼠'
,'牛'
,'虎'
,'兔'
(animals[0]
)鼠
543##while迴圈n=5
while n>0:
(n) n=n-
1print
('blastoff'
(n)
21blastoff
0
543##加延遲
import time n=5
while n>0:
(n) time.sleep(1)
n=n-
1print
('blastoff'
(n)
21blastoff
0
死迴圈n=5
while n>0:
(n)print
('blastoff'
)
##迴圈控制
import time n=5
while n>0:
(n) time.sleep(1)
break
('blastoff'
(n)
##迴圈控制
while true:
line=
'quit'
if line==
'quit'
:break
if line==
'sleep'
:continue
(line)
##計算累加和n=5
s=0while n>0:
s=s+n
n=n-
1print
(n)print
(s)
##使用random產生隨機數,前閉後開
import random
m = random.randrange(
100)
##0到100隨機數
(m)n = random.randrange(1,
10,2)
(n)
##for迴圈
##和while的區別為遍歷
for i in[5
,4,3
,2,1
(i)
for n in
['aaa'
,'bbb']:
('ccc'
,n)
##最大數s=0
for i in[5
,4,3
,2,1
,123
,314
,1232]:
if ss=i
(s)
##turtle繪圖
import turtle
t=turtle.turtle(
)t.shape(
'turtle'
)t.forward(50)
##前進50
t.left(
90)
Python基礎入門 集合 阿里雲天池
集合 python 中set與dict類似,也是一組key的集合,但不儲存value。由於key不能重複,所以,在set中,沒有重複的key。注意,key為不可變型別,即可雜湊的值。例子 num print type num num print type num 集合的建立 先建立物件再加入元素。在...
Docker基礎 天池Docker入門
容器 container 容器是執行中的映象,他的實質是程序,通過docker ps可以檢視執行中的容器。倉庫 repository 首先我們需要登陸乙個伺服器docker registry,然後每個registry上可以包含多個repository,每個repository下可以多個tag相當於不...
Python基礎入門 列表和元組 阿里雲天池
簡單資料型別 整型浮點型 布林型容器資料型別 列表元組 字典集合 字串在python語言中,是用中括號 來解析列表的。列表中的元素可以是數學 字串 列表 元組等。建立乙個列表,只要把逗號分隔的不同資料項使用中括號括起來即可。例如 list1 天貓 2007 2019 2020 list2 1 2,3...