2.0 注釋
python的注釋方法
"""
多行注釋 可實現換行
"""
#單行注釋
2.1 變量
- 問:為什么要有變量?
- 為某個值創建一個“外號”,以后在使用時候通過此外號就可以直接調用。
- 創建一個變量
name = "gkf" #name是變量名 等號就是聲明(或賦值) "gkf"是變量的值
age = 18 #age是標量名 等號就是聲明(或賦值) 18是變量的值
2.2 變量名命名規范
-
可以使用字母數字下滑線組合 如: name ="gkf" num_1 = 318 或 _hobby = "美女"
-
不能以數字不能開頭
-
不能使用python關鍵字
#python的關鍵字有 [False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
- 建議 見名知意,盡量使用下划線連接,不要使用拼音,避免大小寫交替(駝峰體)
- 正確示范: user_name = "gkf666"
- 全局變量全部大寫
### 2.3 常量
- 不允許修改的值,Python中執行約定。(不常使用)
### 2.4 輸入 input
- input (input默認輸入是字符串,如要需要可以進行轉換)
```python
name = input("請輸入姓名") #python3
name = raw_input("請輸入姓名") #python2
- 執行結果:
- 注意 v = input("輸入") v的類型是字符串,當我們在使用變量v的時候,要考慮是否要進行類型轉換
v = input("輸入一個數字")
print(v,type(v)) #type(v) 查看v的類型 #注意代碼中所有的字符必須是英文格式,不然會報錯
#執行結果
輸入一個數字6
6 <class 'str'> #6是str(字符串類型)
Process finished with exit code 0
- 強行翻譯一波input源碼注釋
"""
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
"""
從標准輸入中讀取字符串。刪除尾隨換行符。如果給出了提示字符串,則在讀取輸入之前將其打印到標准輸出而沒有尾隨換行符。
如果用戶點擊EOF(* nix:Ctrl-D,Windows:Ctrl-Z + Return),則引發EOFError。在* nix系統上,如果可用,則使用readline。
2.5 輸出 print
-
print (輸出/打印 你要的東西) 在print2版本里面 print "你好"中間加空格。
print("hello word") # py版本 3 print "hello word" # py版本 2 #結合input一起使用 name = input("請輸入姓名") sex = input("請輸入性別") print("%s,%s"%(name,sex)) #%s是字符串占位符,用來拼接內容
-
執行結果:
- 在Python中print默認是換行的,end='\n'',默認有一個空格sep=' '
#在Python中print默認是換行的
n = '你'
m = '好'
print(n,m)
#你 好 執行結果
print(n,m,sep='')#默認有個空格 sep=' ',sep=''去掉空格
#你好 執行結果
print(n,end='')#end=""去除默認的換行符
print(m) #print 默認有一個 end="\n"換行
#你好 執行結果
print(value,...,sep ='',end ='\ n',file = sys.stdout,flush = False)默認情況下,將值打印到流或sys.stdout。
可選的關鍵字參數:file:類文件對象(stream); 默認為當前的sys.stdout。sep:在值之間插入的字符串,默認為空格。
end:在最后一個值后附加的字符串,默認為換行符。flush:是否強制刷新流。
st = """支持換行
我是行
"""
st1 ='''支持換行
我是行
'''
print(st)
print(st1)
# 執行結果
支持換行
我是行
支持換行
我是行
- 強行翻譯一波print源碼注釋
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
2.6 關於開發工具
- python開發工具我這里使用的是pycharm
- 大家可以參考這篇文章,選擇自己喜歡的開發工具點擊查看