4.從輸入輸出到基本運算子的學習

小方呀0524發表於2024-09-02

主要是輸入輸出的內容

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.解壓縮,我所掌握的一個點就是下劃線在這裡是表示省略內部的一些資料的意思

相關文章