forked from lhui2010/python_learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson1.py
189 lines (133 loc) · 2.63 KB
/
Lesson1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Lesson1
print("Hello world")
# Calculation
2 + 2
50 - 5 * 6
"""
division always returns a floating point number
"""
(50 - 5 * 6) / 4
8 / 5
"""
floor division discards the fractional part
"""
17 // 3
"""
the % operator returns the remainder of the division
"""
17 % 3
5 ** 3
# Compare operator
5 == 3
5 != 3
5>=3
5 <= 3
not (5 < 3)
5>=3 and 5 <3
5>=3 or 5 <3
#Variables and Assignment (变量与赋值)
width = 20
width
height = 5 * 9
#Print area size
width * height
#Print perimeter (打印周长)
print ( 2 * (width + height))
#Change variable values (变量值的操作)
width = 20 * width
width *= 20
width += 20
#Which is correct?
name=“ABC”
name_class=123
name_class_3=0x12213F
3_name=“BGH”
int nameA=“BCE”
#Reserved words
import keyword
keyword.kwlist
print('{0: <80}'.format(str(keyword.kwlist), width=80))
import builtins
dir(builtins)
"""
String variables: '', "", and '''
"""
'spam eggs'
# use \' to escape the single quote...
'doesn\'t'
# ...or use double quotes instead
"doesn't"
'"Yes," they said.'
"\"Yes,\" they said."
'"Isn\'t," they said.'
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
"""
Escape Character (转义字符)
"""
# here \n means newline!
print('C:\some\name')
# note the r before the quote
print(r'C:\some\name')
"""
Operator for string object
"""
'Py'+'thon'
'Py' 'thon'
# 3 times 'un', followed by 'ium'
3 * 'un' + 'ium'
s = 'supercalifragilisticexpialidocious'
len(s)
"""
Exercise:
Coin a SSR sequence with length of 30 nt and repetitive unit of "ATG"
合成一段长度为30nt、重复单元为ATG的微卫星序列:
ATGATGATGATG。。。
"""
word = 'Python'
# character in position 0
word[0]
# character in position 5
word[5]
# slice(切片). character in position 2, 3, 4. Left closed, right open
word[2:5]
"""
Exercise:
print last 6 characters of variable danwei
打印 danwei 变量的最后6个字符
danwei='Kunming Institute of Botany'
"""
a='abc'
b=123
print(a+b)
print(a+str(b))
danwei='Kunming Institute of Botany'
print(danwei.title())
print(danwei.upper())
print(danwei.lower())
danwei='\tKunming Institute of Botany\n'
print(danwei.rstrip() + danwei.rstrip())
print(danwei.lstrip() + danwei.lstrip() )
print(danwei.strip() + danwei.strip() )
#替换掉字符内的空格
print(danwei.replace(' ', ''))
"""
Get Help
"""
import builtins
help(builtins)
"""
Exercise
danwei=‘Kunming Institute of Botany’
>>>type(danwei)
str
>>>help(str)
class str(object)
| Create a new string object from the given object. If encoding or
>>>dir(str)
'capitalize',
'casefold',
"""