首页 > 编程语言 > 详细

Python Basic 01.Basic

时间:2019-02-14 17:04:57      阅读:378      评论:0      收藏:0      [点我收藏+]

step01_variable

 1 ‘‘‘
 2 ??(variable)
 3  - ??(data)? ??(???) ???? ?? 
 4  - ?? ??? ?? ??? ??? ????.(????)
 5 ‘‘‘
 6 
 7 # 1. ??? ?? 
 8 print(??? ??) # ?? ?? 
 9 var1 = "hello python" # or ‘hello python‘
10 print(var1) # ?? ?? ?? 
11 
12 print(type(var1)) # ??? ?? - <class ‘str‘>
13 
14 # ??? = ??
15 var1 = 150 # ??? -> ?? 
16 print(var1) # 150
17 print(type(var1)) # <class ‘int‘>
18 
19 ‘‘‘
20 python ?? ?? 
21  - ??? ???? ?? 
22  - ?? ?? ?? ?? 
23  - ???? ??? type ??
24 ‘‘‘
25 
26 # 2. ??? ?? ??
27 ‘‘‘
28  - ??? ???, ???[??, ????(_)]
29  - ???, ??? ??(Num != num) 
30  - ??, ???(???) ????(?? ???) 
31  - ??? : ? ??? ?? ??
32     ex) korScore = 90
33 ‘‘‘ 
34 
35 Num = 10; num = 100
36 print(Num); print(num)
37 
38 num_10 = 10
39 print(num_10)
40 
41 korScore = 90
42 matScore = 85
43 engScore = 75
44 
45 totScore = korScore + matScore + engScore
46 print(?? :, totScore) # ?? : 250
47 
48 # python ??? ?? 
49 import keyword # ?? ?? 
50 
51 print(??? :, keyword.kwlist)
52 # ??? : [‘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‘]
53 print(??? ? =, len(keyword.kwlist))
54 # ??? ? = 33
55 
56 
57 # 3. ???? : ??? ??? ?? ?? 
58 a = 250
59 b = 35.12
60 c = b
61 d = 250
62 
63 print(a, b, c, d) # ?? ?? 
64 print(id(a), id(b), id(c), id(d)) # ?? ??
65 ‘‘‘
66 250 35.12 35.12 250
67 1742766080 1511904 1511904 1742766080
68 ‘‘‘
69 
70 # ?? ?? ?? 
71 print(a=, a) # a= 250
72 print(b=, b) # b= 35.12
73 
74 # ?? : ?? ??? ?? ??(????)
75 # ?? ?? ?? 
76 print(id(a)) # 1742766080
77 print(id(250)) # 1742766080
78  
79 print(id(b)) # 30282208
80 print(id(35.12)) # 30282208
81 
82 # ?? ?? 
83 print(a == 250) # True
84 
85 # ?? ?? 
86 print(a is 250) # True

 

 

step02_operator

  1 ‘‘‘
  2 ???(operator)
  3  1. ??? ? ??(????? =)
  4  2. print ?? ??
  5  3. ??,??,?? ???
  6  4. ??? ??? ??? 
  7 ‘‘‘
  8 
  9 # 1. ??? ? ??(????? =)
 10 v1 = v2 = v3 = 100
 11 print(v1, v2, v3) # 100 100 100
 12 
 13 # ?? ?? ?? ?? 
 14 print(??1, end = , ) # ??1, ??2
 15 print(??2)
 16 
 17 v1, v2 = 100, 200
 18 print(v1, v2)
 19 
 20 # ??(packing) ?? 
 21 v1, *v2 = [1,2,3,4]
 22 print(v1, v2) # 1 [2, 3, 4]
 23 
 24 *v1, v2 = [1,2,3,4]
 25 print(v1, v2) # [1, 2, 3] 4
 26 
 27 
 28 # 2. print ?? ??
 29 
 30 # 1) format(value, ??)
 31 print(???=,format(3.14159, 8.3f))#???=    3.142
 32 ‘‘‘
 33 ‘8.3f‘ : ??8???? ??? 3?? ?? 
 34 ‘‘‘
 35 print(format(123456, 3,d))#123,456
 36 
 37 #help(format)
 38 
 39 # 2) ????? ? ?? 
 40 # print(‘%????‘%?)
 41 num1 = 10
 42 num2 = 20
 43 add = num1 + num2
 44 print(%d + %d = %d%(num1,num2,add)) #10 + 20 = 30
 45 
 46 name = "???"
 47 age = 35
 48 
 49 print(??? %s??, ??? %d ???.%(name, age))
 50 # ??? ?????, ??? 35 ???.
 51 
 52 pi = 3.14159
 53 print(??? = %.3f%pi) # ??? = 3.142
 54 
 55 
 56 # 3) ?? ?? ??
 57 print(??? {}??, ??? {}???..format(???, 35))
 58 # ??? ?????, ??? 35???.
 59 print(??? {1}??, ??? {0}???..format(35,???))
 60 
 61 
 62 # 3. ??,??,?? ???
 63 
 64 # ?????
 65 print(?? ??? : , 10+3, 10-3, 10*3, 10/3, 10//3, 10%3, 10**3) 
 66 # ?? ??? : 13 7 30 3.3333333333333335 3 1 1000
 67 
 68 # 2? ?? : y = 2.5 + x**2 + 3.3 + x + 6
 69 x = 2
 70 y = 2.5 + x**2 + 3.3 + x + 6
 71 print(y=, y) # y= 17.8
 72 
 73 # ????? 
 74 print(????? : , 5 > 3, 5 >= 2, 5 != 4, 5 == 4)
 75 # ????? :  True True True False
 76 
 77 # ?????
 78 print(????? : , 5>4 and 5!=4, 5<4 or 5>=3, not(5<=2))
 79 # ????? :  True True True
 80 
 81 # ?????/???? 
 82 cnt = tot = 0 # ???, ?? ??? 
 83 cnt = cnt + 1 # ??? ?? 
 84 tot = tot + cnt # ???? 
 85 cnt += 1 # ??? ??(??) 
 86 tot += cnt # ????
 87 print(cnt=, cnt) # cnt= 2
 88 print(tot=, tot) # tot= 3
 89 
 90 # 4. ??? ??? ???
 91 
 92 # 1) ??? ?? 
 93 a = int(input("?? ?? : ")) # ??(??) -> ??(??) ?? 
 94 #print(type(a)) # <class ‘int‘>
 95 b = float(input("?? ?? : ")) # ?? -> ??(??) ?? 
 96 print(%d + %.3f = %.3f%(a, b, a+b)) # 30.5
 97 print(%d + %.3f = %.3f%(a, b, a*b)) 
 98 
 99 # 2) ??? 
100 #True=> 1, False => 0
101 print(int(True)) # 1
102 print(int(False)) # 0

 

 

step03_type_string

  1 ‘‘‘
  2 ???? ??? ?? 
  3  1. ???(data type)
  4  2. ???(string) ??
  5  3. escape ??
  6 ‘‘‘
  7   
  8 # 1. ???(data type)
  9 print(10, type(10)) # 10 <class ‘int‘>
 10 ‘‘‘
 11 ?? : Ctrl+S
 12 ?? : Ctrl+F11
 13 ‘‘‘
 14 print(12.1542, type(12.1542)) # 12.1542 <class ‘float‘>
 15 
 16 print(True, type(False)) # True <class ‘bool‘>
 17 
 18 a = 10; b = 5
 19 print(a > b, type(a > b)) # True <class ‘bool‘>
 20 
 21 string = "????" # or ‘????‘
 22 print(string, type(string)) # ???? <class ‘str‘>
 23 
 24 num_str = 1234
 25 print(num_str * 2) # 12341234
 26 
 27 num = int(num_str) # ??? -> ??
 28 print(num * 2) # 2468 
 29 
 30 # ????(10?? -> 2,8,16??)
 31 print(10, bin(10), oct(10), hex(10))
 32 # 10 0b1010 0o12 0xa
 33 
 34 # ????(2,8,16?? -> 10??)
 35 print(10, 0b1010, 0o12, 0xa)
 36 # 10 10 10 10
 37 
 38 
 39 # 2. ???(string) ??
 40 ‘‘‘
 41  ??? ?? ?? 
 42   - ???(string) : ???? ??
 43   - ?? ?? ?? : ?? ??
 44   - indexing/slicing ?? 
 45 ‘‘‘
 46 
 47 # 1) ??? ??(+, *)
 48 st1 = "????"
 49 st2 = "????"
 50 
 51 st3 = "="
 52 print(st3*20) # ?? 
 53 print(st1 + " " + st2) # ?? : ???? ????
 54 print(st3*20) # ?? 
 55 ‘‘‘
 56 ====================
 57 ???? ????
 58 ====================
 59 ‘‘‘
 60 
 61 # 2) ??, ??? ??? 
 62 string = "???? ????"
 63 multi_line ="""????
 64 ????
 65 ?? ??? ???."""
 66 print(string)
 67 print(multi_line)
 68 ‘‘‘
 69 ????
 70 ????
 71 ?? ??? ???.
 72 ‘‘‘
 73 sql=‘‘‘create table test(
 74 id int,
 75 su int,
 76 dan int)‘‘‘
 77 print(sql)
 78 
 79 
 80 # 3) ??? ?? ??(????)
 81 st = "???? ???? ?? ??? ???? ???."
 82 print(st, type(st)) # <class ‘str‘>
 83 
 84 # ??? ?? 
 85 size = len(st)
 86 print(st ?? : , size) # st ?? :  26
 87 
 88 # ?? ?? ?? 
 89 print(? ?? ? :, st.count(?)) # ? ?? ? : 3
 90 
 91 # ??? - T/F
 92 print(st.startswith(??)) # True
 93 print(st.startswith(??)) # False
 94 
 95 # ??? ??(split)? ??(join)
 96 st2 = "?? ??? ???. ??? 35???, ??? ?? ???."
 97 
 98 # 1) ??? ??
 99 
100 # ?? -> ?? 
101 st3 = st2.split(sep=., maxsplit=2)
102 print(st3 ?? : , st3)
103 # st3 ?? :  [‘?? ??? ???‘, ‘ ??? 35???, ??? ?? ???‘, ‘‘]
104 print(len(st3)) # 3
105 
106 # ?? -> ??(??)
107 st4 = st2.split( ) # sep=‘‘
108 print(st4 ?? : , st4)
109 # st4 ?? :  [‘??‘, ‘???‘, ‘???.‘, ‘???‘, ‘35???,‘, ‘???‘, ‘??‘, ‘???.‘]
110 print(st4 ?? ?? :, len(st4)) # st4 ?? ?? : 8
111 ‘‘‘
112 ??? ?? : ?? ?? ??(??, ??, ?? ?)
113 ‘‘‘
114 
115 # 2) ??? ?? 
116 # ??) ‘???‘.join(str)
117 st5 = ,.join(st4)
118 print(st5 ?? : , st5)
119 # st5 ?? :  ?????,???.,???,35???,,???,??,???.
120 print(len(st5)) # 34
121 
122 
123 print(multi_line)
124 ‘‘‘
125 ????
126 ????
127 ?? ??? ???.
128 ‘‘‘
129 
130 doc = multi_line.split(\n) # sep=‘\n‘
131 print(doc)
132 # [‘????‘, ‘????‘, ‘?? ??? ???.‘]
133 print(?? ? :, len(doc)) # ?? ? : 3
134 
135 
136 # indexing/slicing
137 name = "???"
138 
139 # ?? : object[n]
140 print(name[0]) # ? - ? 
141 print(name[1:]) # ?? - ??
142 print(name[1:3]) # [??:???] - ??
143 print(name[:3]) # [~:???] - ???
144 print(name[0:]) # [??:~] - ???
145 
146 # [-n] : ??? ?? 
147 print(name[-1]) # ?
148 print(name[-2:]) # ??
149 
150 
151 # 3. escape ??
152 ‘‘‘
153 escape : ????, ?? ??
154 ???? : ‘‘, "", \n(??), \t(?)
155 ‘‘‘
156 print(?? \n ??? \t ???.)
157 ‘‘‘
158 ?? 
159  ???      ???.
160 ‘‘‘
161 
162 # ?? ?? : c:\Python\test\test.txt
163 print(path = , c:\Python\test\test.txt)
164 # path =  c:\Python    est    est.txt
165 
166 # escape ?? ??1(r) 
167 print(path = , rc:\Python\test\test.txt)
168 # path =  c:\Python\test\test.txt
169 
170 # escape ?? ??2(\)
171 print(path = , c:\\Python\\test\\test.txt)
172 # path =  c:\Python\test\test.txt
173 
174 # ?) c:\‘aa‘\"abc.txt" ?? ???.(??2) 

 

Python Basic 01.Basic

原文:https://www.cnblogs.com/kingboy100/p/10375550.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!