非程式設計師的 Python 3 教程/誰在那裡?
現在我覺得是時候編寫一個非常複雜的程式了。它就在這裡
print("Halt!")
user_input = input("Who goes there? ")
print("You may pass,", user_input)
當我執行它時,我的螢幕顯示了以下內容
Halt! Who goes there? Josh You may pass, Josh
注意:在透過按下 F5 執行程式碼後,python shell 僅提供輸出
Halt! Who goes there?
您需要在 python shell 中輸入您的姓名,然後按回車鍵以獲得其餘的輸出。
當然,當您執行程式時,由於input()語句,您的螢幕看起來會不同。當您執行程式時,您可能注意到了(您確實運行了程式,對吧?)您必須輸入您的姓名,然後按回車鍵。然後程式打印出更多文字以及您的姓名。這是一個輸入的例子。程式到達某個點,然後等待使用者輸入一些資料,程式以後可以使用這些資料。
當然,如果我們沒有地方存放從使用者那裡獲取的資訊,那麼獲取使用者的資訊將毫無用處,這就是變數發揮作用的地方。在前面的程式中,user_input是一個變數。變數就像一個可以儲存一些資料的盒子。以下是一個顯示變數示例的程式
a = 123.4
b23 = 'Spam'
first_name = "Bill"
b = 432
c = a + b
print("a + b is",c)
print("first_name is",first_name)
print("Sorted Parts, After Midnight or",b23)
這是輸出
a + b is 555.4 first_name is Bill Sorted Parts, After Midnight or Spam
變數儲存資料。上面程式中的變數是a、b23、first_name、b和c。兩種基本型別是字串和數字。字串是字母、數字和其他字元的序列。在這個例子中,b23和first_name是儲存字串的變數。Spam、Bill、a + b is、first_name is和Sorted Parts, After Midnight or是這個程式中的字串。這些字元用"或'包圍。另一種型別的變數是數字。請記住,變數用於儲存值,它們不使用引號(" 和 ')。如果您想使用實際的值,您必須使用引號。
value1 == Pim
value2 == "Pim"
兩者看起來相同,但在第一個中,Python 檢查變數value1中儲存的值是否與變數Pim中儲存的值相同。在第二個中,Python 檢查字串(實際字母P、i和m)是否與value2中的字串相同(繼續閱讀本教程以獲取有關字串和==的更多解釋)。
好的,所以我們有這些叫做變數的盒子,還有可以放入變數中的資料。計算機將看到類似first_name = "Bill"這樣的行,它將它解讀為“將字串Bill放入盒子(或變數)first_name中”。之後,它將看到語句c = a + b,它將它解讀為“將a + b或123.4 + 432的總和(等於555.4)放入c中”。語句的右側(a + b)被評估,結果儲存在左側的變數(c)中。這被稱為賦值,您不應該將賦值等號(=)與這裡的數學意義上的“等式”混淆(那是==將在以後用到的)。
以下是一個關於變數用法的另一個例子
a = 1
print(a)
a = a + 1
print(a)
a = a * 2
print(a)
當然,這是輸出
1 2 4
即使同一個變量出現在等號的兩側(例如,spam = spam),計算機仍然將它解讀為,“首先找出要儲存的資料,然後找出資料儲存的位置”。
在我結束本章之前,再寫一個程式
number = float(input("Type in a number: "))
integer = int(input("Type in an integer: "))
text = input("Type in a string: ")
print("number =", number)
print("number is a", type(number))
print("number * 2 =", number * 2)
print("integer =", integer)
print("integer is a", type(integer))
print("integer * 2 =", integer * 2)
print("text =", text)
print("text is a", type(text))
print("text * 2 =", text * 2)
我得到的輸出是
Type in a number: 12.34 Type in an integer: -3 Type in a string: Hello number = 12.34 number is a <class 'float'> number * 2 = 24.68 integer = -3 integer is a <class 'int'> integer * 2 = -6 text = Hello text is a <class 'str'> text * 2 = HelloHello
請注意,number是用float(input())建立的,int(input())返回一個整數,一個沒有小數點的數字,而text是用input()建立的,返回一個字串(也可以寫成str(input()))。當您希望使用者輸入一個十進位制數時,請使用float(input()),如果您希望使用者輸入一個整數,請使用int(input()),但如果您希望使用者輸入一個字串,請使用input()。
程式的後半部分使用type()函式,該函式告訴變數是什麼型別。數字的型別為int或float,分別代表整數和浮點數(主要用於十進位制數)。文字字串的型別為str,代表字串。整數和浮點數可以由數學函式運算,字串則不行。注意,當 Python 將一個數字乘以一個整數時,會發生預期的事情。但是,當一個字串乘以一個整數時,結果是產生字串的多個副本(即,text * 2 = HelloHello)。
字串的運算與數字的運算不同。此外,某些運算只適用於數字(整數和浮點數),如果使用字串,則會報錯。以下是一些互動模式示例,以進一步說明這一點。
>>> print("This" + " " + "is" + " joined.")
This is joined.
>>> print("Ha, " * 5)
Ha, Ha, Ha, Ha, Ha,
>>> print("Ha, " * 5 + "ha!")
Ha, Ha, Ha, Ha, Ha, ha!
>>> print(3 - 1)
2
>>> print("3" - "1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>
以下是關於一些字串運算的列表
| 運算 | 符號 | 示例 |
|---|---|---|
| 重複 | *
|
"i" * 5 == "iiiii"
|
| 連線 | +
|
"Hello, " + "World!" == "Hello, World!"
|
Rate_times.py
# This program calculates rate and distance problems
print("Input a rate and a distance")
rate = float(input("Rate: "))
distance = float(input("Distance: "))
time=(distance/ rate)
print("Time:", time)
示例執行
Input a rate and a distance Rate: 5 Distance: 10 Time: 2.0
Input a rate and a distance Rate: 3.52 Distance: 45.6 Time: 12.9545454545
Area.py
# This program calculates the perimeter and area of a rectangle
print("Calculate information about a rectangle")
length = float(input("Length: "))
width = float(input("Width: "))
Perimeter=(2 * length + 2 * width)
print("Area:", length * width)
print("Perimeter:",Perimeter)
示例執行
Calculate information about a rectangle Length: 4 Width: 3 Area: 12.0 Perimeter: 14.0
Calculate information about a rectangle Length: 2.53 Width: 5.2 Area: 13.156 Perimeter: 15.46
Temperature.py
# This program converts Fahrenheit to Celsius
fahr_temp = float(input("Fahrenheit temperature: "))
celc_temp = (fahr_temp - 32.0) *( 5.0 / 9.0)
print("Celsius temperature:", celc_temp)
示例執行
Fahrenheit temperature: 32 Celsius temperature: 0.0
Fahrenheit temperature: -40 Celsius temperature: -40.0
Fahrenheit temperature: 212 Celsius temperature: 100.0
Fahrenheit temperature: 98.6 Celsius temperature: 37.0
編寫一個程式,從使用者那裡獲取 2 個字串變數和 2 個數字變數,將它們連線在一起(無空格),並顯示字串,然後在新行上將兩個數字相乘。
編寫一個程式,從使用者那裡獲取 2 個字串變數和 2 個數字變數,將它們連線在一起(無空格),並顯示字串,然後在新行上將兩個數字相乘。
string1 = input('String 1: ')
string2 = input('String 2: ')
float1 = float(input('Number 1: '))
float2 = float(input('Number 2: '))
print(string1 + string2)
print(float1 * float2)