基礎語法
運算子1.不用事先宣告型別,隨時可以賦值為其他型別
2. 程式設計時不知道是什麼型別,很難推斷(編譯時無法檢查,只有執行時才能檢查)
由符號#標註的文字
浮點數
列表list用來定義。裡面的元素可以修改。list是乙個類,支援很多該類定義的方法,這些方法可以用來對list進行操作。num =
"1"# string type
num =
"let's go"
# string type
num =
"he's \"old\""
# string type
mail =
"xiaoyi: \n hello \n i am you!"
mail =
"""xiaoyi:
hello
i am you!
"""# special string format
string =
'xiaoyi'
# get value by index
copy = string[0]
+ string[1]
+ string[2:
6]# note: [2:6] means [2 5] or[2 6)
copy = string[:4
]# start from 1
copy = string[2:
]# to end
copy = string[::
1]# step is 1, from start to end
copy = string[::
2]# step is 2
copy = string[-1
]# means 'i', the last one
元組tuple用()來定義。相當於乙個可以儲存不同型別資料的乙個陣列。可以用索引來訪問,但需要注意的一點是,裡面的元素不能被修改。userlist =
["xiaoyi",25
,"male"
]name = userlist[0]
age = userlist[1]
gender = userlist[2]
userlist[3]
=88888
# error! access out of range
8888
)# add new elements
"male"
in userlist # search
userlist[2]
='female'
# can modify the element (the memory address not change)
userlist.remove(
8888
)# remove element
userlist.remove(userlist[2]
)# remove element
字典dictionary用{}來定義。它的優點是定義像key-value這種鍵值對的結構,就像struct結構體的功能一樣。它也支援字典類支援的方法進行建立和操作。user =
("xiaoyi",25
,"male"
)name = user[0]
age = user[1]
gender = user[2]
t1 =()
# empty tuple
t2 =(2
,)# when tuple has only one element, we should add a extra comma
user[1]
=26# error!! the elements can not be changed
name, age, gender = user # can get three element respectively
運算子item =
['name'
,'age'
,'gender'
]value =
['xiaoyi'
,'25'
,'male'
]zip
(item, value)
# zip() will produce a new list:
# [('name', 'xiaoyi'), ('age', '25'), ('gender', 'male')]
# but we can not define their corresponding relationship
# and we can define this relationship use dictionary type
# this can be defined as a key-value manner
# dic = , key and value can be any type
dic =
dic =
# and we access it like this: dic[key1], the key as a index
print dic[
'name'
]print dic[1]
# another methods create dictionary
fdict =
dict([
'x',1]
,['y',2]
)# factory mode
ddict =
.fromkeys(
('x'
,'y'),
-1)# built-in mode, default value is the same which is none
# access by for circle
for key in dic
print key
print dic[key]
# add key or elements to dictionary, because dictionary is out of sequence,
# so we can directly and a key-value pair like this:
dic[
'tel']=
88888
# update or delete the elements
del dic[1]
# delete this key
dic.pop(
'tel'
)# show and delete this key
dic.clear(
)# clear the dictionary
del dic # delete the dictionary
dic.get(1)
# get the value of key
dic.get(1,
'error'
)# return a user-define message if the dictionary do not contain the key
dic.keys(
)dic.values(
)dic.has_key(key)
含義例子+加
1 + 2 = 3-減
2 - 1 = 1*乘
3 * 3 = 9/除
9 / 3 = 3.0
%取餘數
4 % 3 = 1
**冪指運算
2 ** 3 = 8
單目運算子》雙目運算子算數運算子》位運算子》身份運算子》成員運算子》邏輯運算子
長表示式,多用括號,易讀性
python中,賦值即定義,如果乙個變數已經定義,賦值相當於重新定義
Python學習筆記(一)基礎入門
給大家推薦mooc上北理工嵩天老師講的python課。1.用縮進來區分模組,所以嚴格注意空格和tab。2.一般用新行作為語句結束。但是可用 分為多行顯示 有,可以不需要連線符 4.python可以在同一行中使用多條語句,語句之間使用分號 分割 5.print 預設輸出是換行的,如果要實現不換行需要在...
python基礎入門一
python的變數是不用生宣告的,資料型別是系統自動判斷.print函式可以連續輸出中間逗號隔開.a 10 a 1.3 print a,type a sequence 序列 是一組有順序的元素的集合 序列有兩種 tuple 定值表 也有翻譯為元組 list 列表 s 1 2.1 print s可以對...
python基礎入門 一
位運算 python中的注釋符號是 而有些語言的注釋符號為 python的運算子和其它語言的相差無幾,主要有 順便提一下,因為python是動態性解釋性語言,所以不用事先定義引數的資料型別.資料結構 python的資料結構主要有 位運算是乙個轉為二進位制的乙個運算 其中需要主要的是 原碼 反碼 補碼...