原文地址https://blog.fanscore.cn/a/61/ 1. wssh 1.1 開發背景 公司內部的發佈系統提供一個連接到k8s pod的web終端,可以在網頁中連接到k8s pod內。實現原理大概為通過websocket協議代理了k8s pod ssh,然後在前端通過xterm.js ...
主要是輸入輸出的內容
1.互動式輸入
2.格式化輸出
1 占位符輸出
2 format輸出
3 f-string輸出
3.基本運算符
1.算術運算符
2.比較運算符
3.賦值運算符
4.邏輯運算符
5.身份運算符
4.賦值方法
1.鏈式賦值
2.交叉賦值
5.解壓縮
6.作業
1 互動式輸入
這裡主要講的是與input()函數的交互,其實交互的意思就是可以讓用戶個人來提供輸入信息
name = input("name<<<")
age = input('age<<<')
height = input('height<<<')
print("my name is:"+name+", my age is:"+age+", my height is"+height+'.')
name<<<xiaocao
age<<<22
height<<<170
my name is:xiaocao, my age is:22, my height is170.
print("my name is:"+name+", my age is:"+age+", my height is"+height+'.')
print('*'*100)
print(type(name))
print(type(age))
print(type(height))
my name is:xiaocao, my age is:22, my height is170.
****************************************************************************************************
<class 'str'>
<class 'str'>
<class 'str'>
這裡主要註意input的輸出類型為str,可以直接使用在input前面增加int,float進行數據類型轉換
print(type(int(age)))
<class 'int'>
2 格式化輸出
這裡三種占位符,format和f-string
還是用上面的例子來打比方
2.1 占位符
name = input("name<<<")
age = input('age<<<')
height = input('height<<<')
print("my name is %s, my age is %s, my height is %s."%(name,age,height))
name<<<xiaocao
age<<<18
height<<<170
my name is xiaocao, my age is 18, my height is 170.
name = input("name<<<")
age = int(input('age<<<'))
height = int(input('height<<<'))
print("my name is %s, my age is %d, my height is %d."%(name,age,height))
name<<<xiaocao
age<<<18
height<<<170
my name is xiaocao, my age is 18, my height is 170.
%d占位符表示的是數字,如果是字元串類型則會報錯
2.2 format形式
這個就是將占位符更換成大括弧了
與占位符的區別:可以調整順序
print("my name is {}, my age is {}, my height is {}.".format(name,age,height))
print("my age is {1}, my name is {0}, my height is {2}.".format(name,age,height))
print("my name is {}, my age is {}, my height is {:.2f}.".format(name,age,height))
my name is xiaocao, my age is 18, my height is 170.
my age is 18, my name is xiaocao, my height is 170.
my name is xiaocao, my age is 18, my height is 170.00.
以前:format的形式好難呀,看到就頭疼
現在:?怎麼可以這麼清晰,而且還可以用markdown來寫文檔了
2.3 f-string的形式
哇,這個簡直就是絕,直接在大括弧內部添加內容
name = input("name<<<")
age = input('age<<<')
height = input('height<<<')
print(f"my name is {name}, my age is {age}, my height is {height}.")
name<<<ciaocao
age<<<18
height<<<170
my name is ciaocao, my age is 18, my height is 170.
print(f"my name is {name}, my age is {int(age)+1}, my height is {height * 10}.")
my name is ciaocao, my age is 19, my height is 170170170170170170170170170170.
print(f"my name is {name}, my age is {age+1 :.2f}, my height is {height * 10}.")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[15], line 1
----> 1 print(f"my name is {name}, my age is {age+1 :.2f}, my height is {height * 10}.")
TypeError: can only concatenate str (not "int") to str
print(f"my name is {name}, my age is {int(age)+1 :.2f}, my height is {height * 10}.")
my name is ciaocao, my age is 19.00, my height is 170170170170170170170170170170.
雖然這個例子有些不合理吧,但是表達的意思倒是聽清楚的
哈哈哈哈哈哈哈哈哈哈哈哈
保留報錯的原因,是因為這個很經典,忘記將其轉換為數字類型報錯啦
3 基本運算符
3.1 算術運算符
顧名思義,用於計算數值間的值的運算符
1+2
3
1-2
-1
1/2
0.5
1//2
0
1%2
1
3.2 比較運算符
用於比較數值大小用的
1>2
False
2>1
True
7>=2
True
7<=2
False
8==8
True
1 != 2
True
3.3 賦值運算符
就是將數值賦值給一個變數
y =5
print(y)
5
3.4 邏輯運算符
三種形式 and or not
1>1 and 1>2
False
1>1 or 1<2
True
#0表示的就是錯的意思
not 0
True
3.5 身份運算符
一般就是比較兩個id是否相同,用 is 來表示
x = 300
y = 300
print(x == y)
print(id(x)==id(y))
print (x is y)
True
False
False
4 賦值方式
這一塊主要說明這些方法較一些C語言而言要簡單
鏈式賦值
交叉賦值
4.1 鏈式賦值
這個好理解,跟一條鏈子一樣,左邊有幾個變數右邊就有幾個數值
#這樣會很麻煩
x = 10
y = 10
z = 10
print(x,y,z)
a ,b =10,20
print(a,b)
10 10 10
10 20
4.2 交叉賦值
這個就是說白了,更換變數
a = 10
b = 20
print(a,b)
10 20
a = 10
b = 20
c = a
a = b
b = c
print(a,b)
20 10
a,b =b,a
print(a,b)
10 20
5 解壓縮
解壓縮的目的就是在列表裡面中間不要的信息過多,需要進行忽略,只留下對我們而言重要的信息
方法1:日常使用過程,如果想給列表裡的每個值進行取出來,則可以使用
hobby = ['piao','666']
x ,y =hobby
print(x,y)
piao 666
如果取出來有我們不想要的值,則可以用下劃線來代替
hobby_list = ['piao','666',2333,999,'handsome']
_,hobby1,_,_,hobby2 = hobby_list
print(hobby1,hobby2)
666 handsome
如果在使用過程中取中間兩個數,其他數字進行省略可以用星號加下劃線
hobby_list = ['piao','666',2333,999,'handsome','read','run','music','swimming','football','basketball','baterminten']
_,hobby1,_,hobby2,*_ = hobby_list
print(hobby1,hobby2)
666 999
6 作業
6.1 作業1
用三種格式化方式進行輸出
name = 'xiaocao'
height = 180
weight = 100
print("My name is %s, my height is %d, my weight is %d"%(name,height,weight))
print("My name is {}, my height is {}, my weight is {}".format(name,height,weight))
print(f"My name is {name}, my height is {int(height)}, my weight is {weight}")
My name is xiaocao, my height is 180, my weight is 100
My name is xiaocao, my height is 180, my weight is 100
My name is xiaocao, my height is 180, my weight is 100
6.2 一行代碼實現交互
x = 10
y = 10
z = 10
x,y,z =10,10,10
print(x,y,z)
10 10 10
6.3 交換數值
x = 10
y = 10
x = 10
y = 20
x,y =y,x
print(x,y)
20 10
6.4 取出想要的愛好
hobby_list = ['read','run','music','code']
hobby_list = ['read','run','music','code']
print(hobby_list[1:3])
_,hobby1,hobby2,_ = hobby_list
print(hobby1,hobby2)
['run', 'music']
run music
總結
1.首先是markdown語法的學習,成功開始開啟我的博客生涯
2.關於電腦的系統,有了一個更清晰的認識
3.使用jupyter開始寫代碼
4.常量和變數的定義,記憶體的存儲方式,數據類型
1.記憶體存儲會計數,有的id會相同有的不會
2.數字類型(整數和浮點數),字元串類型,列表類型,字典類型,布爾類型
5.關於編程的輸入輸出以及基本運算符
1.互動式輸入,即可以讓用戶看到,用input函數
2.輸出三種類型,占位符,format和f-string,掌握的是f-string
3.基本運算符五種,算數,比較,賦值,邏輯和地址
6.數值賦值的兩種方式:鏈式和交叉
7.解壓縮,我所掌握的一個點就是下劃線在這裡是表示省略內部的一些數據的意思