1. print 'Hello World'
a = 10
print a
print type(a)
a = 1.3
print a
print type(a)
basic type:
a = 10 int a =1.3 float a = True True/False a = 'Hello' string
type() - return data type
2. Sequence:
Tuple : s1 = (2, 1.3, 'love',9,12,false) , element can not be change
List: s2 = [True, 5, 'smile'] , element can change
print s1[0]
print s2[2]
s2[1] = 3.0
print s2
print s1[:5] -- from start to element 4
print s1[2:] -- from element 2 to last
print s1[0:5:2] -- from index 0 to element 4 , every 2 to get a element, such as element 0,2,4
print s1[2:0:-1] -- from element 2 to element 1
print s1[-1] -- last element
print s1[-3] -- the last third element
3. If else
i = 1
if i>0:
print 'positive i'
i = i+1
elif i ==0:
print 'i is 0'
i = i *10
else:
print 'negative i'
i = i -1
print 'new i:',i
4. for, while, continue, break
for a in [3,4.4, 'lift']:
print a
while i< 10:
print i
i = i+1
5. function
def square_sum(a,b)
c = a**2 + b ** 2
return c
print square_sum(3,4)
6. class , object
class Bird(object)
have_feather = True
way_of_reproduction = 'egg'
def move(self,dx,dy):
position =[0,0]
position[0] = position[0]+dx
position[1] = position[1] +dy
return position
summer = Bird()
print 'after move:', summer.move(5,8)
class Chicken(Bird):
way_of_move = 'walk'
possible_in_KFC = True
summer = Chicken()
print summer.have_feather
print summer.move(5,6)
7. dir(), help()
print dir(list)
print help(list)
n1 = [1,2,5,3,5]
n1.count(5), how many 5
n1.index(3), index of the first 3
n1.append(6), add 6 to the list
n1.sort(), sort
n1.pop(), remove the last element and return it
n1.remove(2), remove the first 2
n1.insert(0,9), insert 9 to position 0
No comments:
Post a Comment