Python day1

paython

1: Type : print(type(name)) # view the data type

    Integer

    String

    Tuple

    List

    Dict

  • String :

 .strip()      -->.rstrip() -->.lstrip() # remove the space (right ,left)

 .split     :eg obj.split() ;obj.split(-) #logon,pwd=name_list.strip().split()

len(obj)  :#calculate the length of objective

obj[1]

obj[1:] ,name="alex" , obj=name[1:]  print(obj) ->lex

obj[1:3] , print(obj)=le

  • List

L1=[1,2,3,4,5]

index ,split,

append ,l1.append('alex')  -- >L1=[1,2,3,4,5,'alex']

extend, L1.extend('alex')   L1=[1,2,3,4,5,,'a','l','e','x'}

insert , L1.insert(1,'alex')   Print(L1)=[1,'alex',2,3,4,5]

pop, Li.pop() defalut to remove the last element. Li.pop() = [1,2,3,4]

if you want to remove a certain element,pls ,use the index, ret=li.pop(0)

remove, delete a element, syntax: li.remove(2) ,print (li) = [1,3,4,5]

reverse ,li.reserver() = [4,3,2,1]

  • Tuple

tu=(11,22,33)

Li=list(tu) ,print(list)= [11,22,33]

  • Dic

#how to create Dic ,2 method to do it as below :

phonebook={' ':'123','bech':'456','ceil':'789}

dic=dict(k1='bech',k2='v2')  print(dic)={'k1':'bech','k2':'v2'}

items=[('name','yuan'),('age',42)]

d=dict(items)      print(d) = {'name":'yuan','age'=42}

x.get(key) # to get a value .  d.get('name')  =>'yuan'

x.items() # to get all elements in this dic .   d.itmes() = dict_items([('age',40),('name','alex')])

x.pop(key) # delete key :value ,        d.pop('name')= yuan ,print(d)={'age':42}

x.keys() # to get all keys              d.keys()=['name','age']

x.valus() # to get all valus           d.values()=['yuan',40]

x.update(y)  # use dic y to update dic x

info=dict(name='cold',blog='jinjunzmx')

info.update(name='alex',blog='jinjunzmx')

print(info)  {'name':'alex','blog':'jinjunzmx'}

#To get key  value and run loop

#info = dict(name='clod',blog='jinjun')

#for key , value in info.items():

 #print key, ':', value

  #name : cold

  # blog : jinjun

 

  • If

format :

   if (conditions)

        statement

   execute :

  •   for , else

for  variables in obj

  • while , else

while condition:

    till condition is false ,

>>>a = 0

>>>while a<3:

   >>>print('yuan')

   >>>#continue

   >>> a+=1

   >>>#break

>>>else:

>>>print('alex")

  • open File

file.obj=open('path',+'mode')  # mode = write ,read ,execute

obj.read

obj.write()

obj.readlines()  # return to a list with \n

>>>f=open(r'd:\abc.txt','a')  # a ,mean keep the old data ,

>>>mes=['good\n','great\n','well\n']

>>>f.writelines(mes)

>>>f.close

>>>with open(r'd:\abc.txt','a') as f :

 >>> line_list=f.readlines()

 >>> for line in line_list:

  >>>     print(line)

 

                   

 

posted @ 2016-04-14 17:01  jinjunzmx  阅读(132)  评论(0编辑  收藏  举报