輸入和輸出是python的基本,畢竟,編寫python程序不就是為了執行一個特定的任務嗎?
輸入,是為了告訴計算機所需信息;輸出,是為了返回給客戶相應結果。
就好比告訴QQ程序你的QQ賬號密碼,而QQ返回給你你的個人QQ操作界面。
1.輸出(print)
為什么先說輸出呢?
因為輸出是python程序必要的,而輸入卻不是。
沒有輸出,程序就無法做出響應,而沒有輸入,在特定情況下,程序是可以做出反饋的。
輸出的基本語法:print()
print(' '),print(" "):在引號內加入指定字符串,即可輸出指定文字。
區別只是單引號內特定字符需要使用轉義字符"\",而雙引號則不需要。
當然,雙引號中包含雙引號需要用轉義字符,單引號中則不需要。
例如print('hello world!'),運行后輸出的便是hello world!
print('I\'m a student')------------->I'm a student
print("I'm a student")------------->I'm a student
print("I'm a \"student\"")--------->I'm a "student"
print('I\'m a "student"')----------->I'm a "student"
print函數也可以接受多個字符串,用逗號“,”隔開,就可以連成一串輸出。
使用逗號的地方輸出后會變成空格。
例如print('hello world!','100+200','花Q!'),輸出后便是hello world! 100+200 花Q!
print也可以打印整數,或者計算結果。
例如print(2019),print(100+200),結果分別是2019,300
最后,print也可以進行多行輸出。(三重單引號或三重雙引號‘’‘,“”“)
例如print('''I'm a student----------->I'm a student
I'm very lazy 輸出 I'm very lazy
the lazy dog''') the lazy dog
2.輸入(input)
說完了輸出,接下來就是輸入。
python提供了一個input(),讓用戶輸入字符串,並存放到一個變量里。
例如
1 name=input() 2 print(name)
運行后會讓你輸入字符串,然后回車,即可看到自己輸入的結果。
就這么簡單。
當然,感覺這樣太簡陋的話也可以這樣。
1 name=input('please enter your name:') 2 print('hello',name)
這樣輸出后的結果便是在"please enter your name:"后面輸入字符串,在hello,后顯示輸入的內容。
please enter your name:xxx
hello,xxx
這樣是不是好看了一點?
END