讓我們討論一下 Python what is the console in. 控制台(也稱為 Shell)Basically a command line interpreter,it takes input from the user,i.e. one command at a time and interpret it. 如果它沒有錯誤,then it will run the command and give the desired output,否則會顯示錯誤消息. Python The console looks like this.
在這裡,We write a command and execute that command,只需按 Enter 鍵,your command will be interpreted. 要使用 Python 進行編碼,您必須了解 Python Used in the basic knowledge of the console. python The main console prompt is three greater than signs:
>>>
Only if these prompts appear after executing the first command,您才可以在 shell This write a command. Python The console accepts what you write after the prompt Python 命令.
Accept input from console The user to enter a value in the console,Then use that value in your program as needed. In order to get input from the user,我們使用內置函數 input().
例子
# input
input1 = input()
# output
print(input1)
輸出:
>>>haiyong.site
'haiyong.site'
We also can be specified in the type input() function to convert this input type to an integer、浮點數或字符串.
將輸入類型轉換為整數:There may be a need from the user/The console integer input,The following code takes two inputs from the console(整數/浮點數)and typecast them to integers,Then print the sum of the.
# input
num1 = int(input())
num2 = int(input())
# print the sum as an integer
print(num1 + num2)
輸出:
>>>10
>>>20
30
convert input type to float: To convert input to float,可以使用以下代碼.
# input
num1 = float(input())
num2 = float(input())
# print the sum as a float
print(num1 + num2)
輸出:
>>>10
>>>20
30.0
Converts input type string: All types of input can be converted to string type,Either float or integer.我們使用關鍵字 str 進行類型轉換.
# input
string = str(input())
# output
print(string)
輸出:
>>>20.0
'20.0'
How to enter multiple values from user in one line:例如,在 C 中我們可以這樣做:
// Read the two values in a row
scanf("%d %d", &x, &y)
One solution is to use twice input() 函數.
x, y = input(), input()
print("x=",x,"y=",y)
輸出:
>>>haiyong
>>>.site
x= haiyong y= .site
另一種解決方案是使用 split() 函數.
x, y = input().split()
print("x=",x,"y=",y)
輸出:
>>>10 20
x= 10 y= 20
請注意,We don't have to explicitly specify split(' '),因為 split() By default whitespace is used as delimiter.在上面的 Python One thing to note in the code is,x 和 y 都是字符串.We can use another line to convert them to int.
>>>x, y = [int(x), int(y)]
>>>print(x,y)
10 20
# We can also use list comprehension
>>>x, y = [int(x) for x in [x, y]]
>>>print(x,y)
10 20
The following is a complete code line,它使用 split 和 list comprehension Read two integer variables from standard input.
# Read two numbers from the input and use list comprehension to typecast them to int
x, y = [int(x) for x in input().split()]
# Read two numbers from the input and use map function to convert them to int
x, y = map(int, input().split())
END 參數: 默認情況下,python 的 print() function ends with a newline.具有 C/C++ Programmers from a background may wonder how to print without newlines.Python 的 print() function with a“end”的參數.默認情況下,該參數的值為'\n',即換行符.You can use this parameter with any character/String end print statement.
#此 Python 程序必須使用 Python 3 運行,because it doesn't apply to 2.7.
# 以 <space> 結束輸出
print("Welcome to" , end = ' ')
print("haiyong.site", end = ' ')
輸出:
Welcome to haiyong.site
Another program to demonstrate the work of the end parameter.
#此 Python 程序必須使用 Python 3 運行,because it doesn't apply to 2.7.
# 以'@'結束輸出
print("Python" , end = '@')
print("haiyong.site")
輸出:
[email protected]
感謝大家的閱讀,有什麼問題的話可以在評論中告訴我.希望大家能夠給我來個點贊+收藏+評論 ,你的支持是海海更新的動力!後面我會持續分享前端 & 後端相關的專業知識.