python的learn_python_learn1
1、python在命令行獲取當前的路徑。
import os
os.getcwd()
os.chdir(r"C:\Users\szlon\Desktop\pythonexer")
2、在Python中\是轉義符,\u表示其后是UNICODE編碼,因此\User在這里會報錯,在字符串前面加個r表示就可以了。
Note:python中的字符串定義不是單引號嗎?
python安裝過程不敘述,記得勾選將python路徑放入Windows運行環境。
上面用到了兩個Windows命令,CD轉換運行路徑。DIR顯示當前路徑下的所有文件。
import 文件名即可。
6、教程中最開始的$符號是Linux命令行默認開始符號。
內置len函數。
S1 = [1,2,3]
S2 = [0,[1,2,3]]
len(S1) = 3
len(S2) = 2
也就是list可以當任意類型當做一個元素來對待,即使它本身也是個list類型。
元組合list都是序列。
直接input就好,是系統內置函數,不需要import。
例:
1 >>> mac_addr = input('請輸入MAC地址:')2 請輸入MAC地址:10:23:45:67:89:10
3 >>> print(mac_addr)4 10:23:45:67:89:10
View Code
9、練習一早上
1 Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64bit (AMD64)] on win322 Type "help", "copyright", "credits" or "license()" formore information.3 >>> print('Hello World!')4 Hello World!5 >>>$python hello.py6 SyntaxError: invalid syntax7 >>> importos8 >>>os.getcwd()9 'C:\\Users\\szlon\\AppData\\Local\\Programs\\Python\\Python37'
10 >>> os.chdir("C:\Users\szlon\Desktop\pythonexer")11 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
12 >>> os.chdir('C:\Users\szlon\Desktop\pythonexer')13 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
14 >>> os.chdir(r"C:\Users\szlon\Desktop\pythonexer")15 >>>os.getcwd16
17 >>>os.getcwd()18 'C:\\Users\\szlon\\Desktop\\pythonexer'
19 >>>hello.py20 Traceback (most recent call last):21 File "", line 1, in
22 hello.py23 NameError: name 'hello' is notdefined24 >>> importhello25 Hello World!26 >>> a = 10
27 >>> print(a)28 10
29 >>> print(type(a))30
31 >>> a = 1.3
32 >>> print(a,type(a))33 1.3
34 >>> a =True35 >>> print(a,type(a))36 True
37 >>> a = 'Hello!'
38 >>> print(a,type(a))39 Hello!
40 >>> s1 = (2,1.3,'love',5.6,9,12,False)41 >>> print(s1,type(s1))42 (2, 1.3, 'love', 5.6, 9, 12, False)
43 >>> s2 = [true,5,'simle']44 Traceback (most recent call last):45 File "", line 1, in
46 s2 = [true,5,'simle']47 NameError: name 'true' is notdefined48 >>> s2 = [True,5,'simle']49 >>> print(s2,type(s2))50 [True, 5, 'simle']
51 >>> s3 = [1,[3,4,5]]52 >>> print(s3,type(s3))53 [1, [3, 4, 5]]
54 >>> print(s3.count)55
56 >>> print(s3.count())57 Traceback (most recent call last):58 File "", line 1, in
59 print(s3.count())60 TypeError: count() takes exactly one argument (0 given)61 >>> print(len(s3)62 )63 2
64 >>> print(len(s3))65 2
66 >>> print(s1[0])67 2
68 >>> print(s2[2])69 simle70 >>> print(s3[1][2])71 5
72 >>> mac_addr = input('請輸入MAC地址:')73 請輸入MAC地址:10:23:45:67:89:10
74 >>> print(mac_addr)75 10:23:45:67:89:10
76 >>> print 1+9
77 SyntaxError: Missing parentheses in call to 'print'. Did you mean print(1+9)?78 >>> print (1+9)79 10
80 >>> print (1.3 - 4)81 -2.7
82 >>> print (3*5)83 15
84 >>> print (4.5/1.5)85 3.0
86 >>> print(3**2)87 9
88 >>> print(10%3)89 1
90 >>> print( 5 == 6)91 False92 >>> print (8.0 != 8.0)93 False94 >>> print( 3 < 3, 3 <= 3)95 False True96 >>> print(4 > 5, 4 >=0)97 False True98 >>> print(5 in [1,3,5])99 True100 >>> print(True and True, True andFalse)101 True False102 >>> print(True orFalse)103 True104 >>> print(notTrue)105 False106 >>> print( 5==6 or 3 >=3)107 True108 >>>
View Code
10、input輸入的是字符串,怎么輸入整數或者基本類型的數。
a = int(input('請輸入一個整數'))
11、一個求絕對值的小模塊
1 a = int(input('請輸入一個數:'))2
3 if a >=0:4 a =a5 else:6 a = -a7
8 print('a的絕對值為:',a)
View Code
12、1-100求和
1 sum =0;2
3 for i in range(101):4 sum +=i5
6 print(sum)
View Code
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python的learn_python_learn1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python修改数据库_python m
- 下一篇: python如何让用户输入文件名并打开文