Python要點學習

Python學習要點:

1、基本數據類型

2、如何自定義數據結構

3、if  for  while 語句

4、系統調用如何使用

5、多線程如何操作


一、python數據類型

1.1、字符串

a、使用單引號(')
用單引號括起來表示字符串,例如:
str='this is string';
print str;

b、使用雙引號(")
雙引號中的字符串與單引號中的字符串用法完全相同,例如:
str="this is string";
print str;

c、使用三引號(''')
利用三引號,表示多行的字符串,可以在三引號中自由的使用單引號和雙引號,例如:
str='''this is string
this is pythod string
this is string'''
print str;

1.2、布爾類型
bool=False;
print bool;
bool=True;
print bool;

1.3、整數
int=20;
print int;

1.4、浮點數
float=2.3;
print float;

1.5列表

list=['physics', 'chemistry', 1997, 2000];
nums=[1, 3, 5, 7, 8, 13, 20];

1.5.1 遍歷列表

for  element  in list:

print element

1.5.2獲得列表長度

nlen = len(list)

1.5.3自動生成數字列表

numList = range(len(list))

print numList

結果:[0,1,2,3]


1.6元組

Python的元組與列表類似,不同之處在於元組的元素不能修改;元組使用小括號(),列表使用方括號[];元組創建很簡單,只需要在括號中添加元素,並使用逗號(,)隔開即可,例如:

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
1.7字典

字典(dictionary)是除列表之外python中最靈活的內置數據結構類型。列表是有序的對象結合,字典是無序的對象集合。兩者之間的區別在於:字典當中的元素是通過鍵來存取的,而不是通過偏移存取。

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'};

1.8文件操作
例子:

f = open(fileName, "r")
while True:

line = f.readline()
  if line:
  if len(line) > 2:
  print line
  filelist.append(line)
else:
print "null..........."
  else:
break

打開文件:f = open(filename, flag)

讀取行:   line = f.readline() 讀取完畢收,返回空




5、如何自定義數據結構
發佈了29 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章