天池python基礎入門課程筆記

2021-10-04 20:59:05 字數 3516 閱讀 8765

1.1變數

常量變數:

變數名稱:

1、不使用保留名

2、變數命名規定

必須以字母或者下劃線開頭

必須包含字母、數字、下劃線

大小寫敏感

保留名賦值操作

1.2型別

整數、浮點數、字串、布林值、空值

type()

##輸出型別

int(

)##轉為整型

float()

##轉為浮點型

str(

)##轉為字串

input()

##輸入資訊

使用者輸入

input

('who\n'

)

格式化

a=

0.6

b=12

c=a*b

print

('%.3f'

%c)

if函式注意冒號和縮排,一層縮排是乙個tab或者四個空格,減少一層縮排表示**塊結束

x=

5if x>10:

print

('big'

)if x<10:

print

('small'

)print

('finish')​

small

finish

##巢狀條件

x=

6if x>5:

print

('大於5'

)if x==6:

print

('等於6'

)

大於5

等於6##雙路判斷

x=

6if x>5:

print

('大於5'

)else

:print

('小於5'

)

大於5

##多路判斷

x=

6if x>5:

print

('大於5'

)elif x>3:

print

('大於3'

)else

:print

('小於等於3'

)

大於5

##作業:

x=-1

if x>

100or x<0:

print

('分數錯誤'

)elif x>=85:

print

('優秀'

)elif x>=60:

print

('及格'

)else

:print

('不及格'

)

分數錯誤

##捕捉異常

try

:print(1

/0)except

:print

('wrong'

)

wrong

##生肖換算

animals=

['鼠'

,'牛'

,'虎'

,'兔'

]print

(animals[0]

)鼠

##while迴圈n=5

while n>0:

print

(n) n=n-

1print

('blastoff'

)print

(n)

543

21blastoff

0

##加延遲

import time n=5

while n>0:

print

(n) time.sleep(1)

n=n-

1print

('blastoff'

)print

(n)

543

21blastoff

0

死迴圈n=5

while n>0:

print

(n)print

('blastoff'

)

##迴圈控制

import time n=5

while n>0:

print

(n) time.sleep(1)

break

print

('blastoff'

)print

(n)

##迴圈控制

while true:

line=

'quit'

if line==

'quit'

:break

if line==

'sleep'

:continue

print

(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隨機數

print

(m)n = random.randrange(1,

10,2)

print

(n)

##for迴圈

##和while的區別為遍歷

for i in[5

,4,3

,2,1

]:print

(i)​

​for n in

['aaa'

,'bbb']:

print

('ccc'

,n)

##最大數s=0

for i in[5

,4,3

,2,1

,123

,314

,1232]:

if ss=i

print

(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...