数据类型#

import sys

数字#

a = 3
b = 4
c = 5.66
d = 8.0
e = complex(c, d)
f = complex(float(a), float(b))
print ("a is type" , type(a))
print ("b is type" , type(b))
print ("c is type" , type(c))
print ("d is type" , type(d))
print ("e is type" , type(e))
print ("f is type" , type(f))
a is type <class 'int'>
b is type <class 'int'>
c is type <class 'float'>
d is type <class 'float'>
e is type <class 'complex'>
f is type <class 'complex'>
print(a + b)
print(d / c)
print (b / a)
print (b // a)
print (e)
print (e + f)
7
1.4134275618374559
1.3333333333333333
1
(5.66+8j)
(8.66+12j)
print ("e's real part is: " , e.real)
print ("e's imaginary part is: " , e.imag)
print (sys.float_info)
e's real part is:  5.66
e's imaginary part is:  8.0
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

字符串#

age = 3
name = "Tom"
print("{0} was {1} years old".format(name, age))
print(name + " was " + str(age) + " years old")
print("What's your name? \nTom")
Tom was 3 years old
Tom was 3 years old
What's your name? 
Tom

字面值#

6, 2.24, 3.45e-3, "This is a string"
(6, 2.24, 0.00345, 'This is a string')

变量#

identifier = 8

列表#

number_list = [1, 3, 5, 7, 9]
string_list = ["abc", "bbc", "python"]
mixed_list = ['python', 'java', 3, 12]
second_num = number_list[1]
third_string = string_list[2]
fourth_mix = mixed_list[3]
print("second_num: {0} third_string: {1} fourth_mix: {2}".format(second_num, third_string, fourth_mix))
second_num: 3 third_string: python fourth_mix: 12
print("number_list before: " + str(number_list))
number_list[1] = 30
print("number_list after: " + str(number_list))
number_list before: [1, 3, 5, 7, 9]
number_list after: [1, 30, 5, 7, 9]
print("mixed_list before delete: " + str(mixed_list))
del mixed_list[2]
print("mixed_list after delete: " + str(mixed_list))
mixed_list before delete: ['python', 'java', 3, 12]
mixed_list after delete: ['python', 'java', 12]
print(len([1,2,3])) #长度
print([1,2,3] + [4,5,6]) #组合
print(['Hello'] * 4) #重复
print(3 in [1,2,3]) #某元素是否在列表中
3
[1, 2, 3, 4, 5, 6]
['Hello', 'Hello', 'Hello', 'Hello']
True
abcd_list =['a', 'b', 'c', 'd'] 
print(abcd_list[1])
print(abcd_list[-2])
print(abcd_list[1:])
print(abcd_list[:-1])
b
c
['b', 'c', 'd']
['a', 'b', 'c']
a_list = [1, 3, 5, 7, 9]

for i in a_list:
    print(i)
1
3
5
7
9

列表操作包含以下函数:

  1. cmp(list1, list2):比较两个列表的元素

  2. len(list):列表元素个数

  3. max(list):返回列表元素最大值

  4. min(list):返回列表元素最小值

  5. list(seq):将元组转换为列表

列表操作包含以下方法:

  1. list.append(obj):在列表末尾添加新的对象

  2. list.count(obj):统计某个元素在列表中出现的次数

  3. list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

  4. list.index(obj):从列表中找出某个值第一个匹配项的索引位置

  5. list.insert(index, obj):将对象插入列表

  6. list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

  7. list.remove(obj):移除列表中某个值的第一个匹配项

  8. list.reverse():反向列表中元素

  9. list.sort([func]):对原列表进行排序

元组#

a_tuple = (2,) #创建只有一个元素的tuple,需要用逗号结尾消除歧义
mixed_tuple = (1, 2, ['a', 'b'])
print(a_tuple)
print("mixed_tuple: " + str(mixed_tuple))
(2,)
mixed_tuple: (1, 2, ['a', 'b'])
mixed_tuple[2][0] = 'c'
mixed_tuple[2][1] = 'd'
print("mixed_tuple: " + str(mixed_tuple))
mixed_tuple: (1, 2, ['c', 'd'])
v = ('a', 'b', 'e')
(x, y, z) = v
print(v)
print(x, y, z)
('a', 'b', 'e')
a b e
a_tuple = (1, 3, 5, 7, 9)

for i in a_tuple:
    print(i)
1
3
5
7
9

字典#

phone_book = {'Tom': 123, "Jerry": 456, 'Kim': 789}
mixed_dict = {"Tom": 'boy', 11: 23.5}
print("Tom's number is " + str(phone_book['Tom']))
print('Tom is a ' + mixed_dict['Tom'])
Tom's number is 123
Tom is a boy
phone_book['Tom'] = 999
phone_book['Heath'] = 888
print("phone_book: " + str(phone_book))
phone_book: {'Tom': 999, 'Jerry': 456, 'Kim': 789, 'Heath': 888}
phone_book.update({'Ling':159, 'Lili':247})
print("updated phone_book: " + str(phone_book))
updated phone_book: {'Tom': 999, 'Jerry': 456, 'Kim': 789, 'Heath': 888, 'Ling': 159, 'Lili': 247}
del phone_book['Tom']
print("phone_book after deleting Tom: " + str(phone_book)) 
phone_book after deleting Tom: {'Jerry': 456, 'Kim': 789, 'Heath': 888, 'Ling': 159, 'Lili': 247}
phone_book.clear()
print("after clear: " + str(phone_book))
after clear: {}
del phone_book
# print("after del: " + str(phone_book)) # Error
list_dict = {('Name'): 'John', 'Age':13} # OK
# list_dict = {['Name']: 'John', 'Age':13} # Error
a_dict = {'Tom':'111', 'Jerry':'222', 'Cathy':'333'}

for ele in a_dict:
    print(ele)
    print(a_dict[ele])
    
for key, elem in a_dict.items():
    print(key, elem)
Tom
111
Jerry
222
Cathy
333
Tom 111
Jerry 222
Cathy 333

Python字典包含了以下内置函数:

  1. cmp(dict1, dict2):比较两个字典元素。

  2. len(dict):计算字典元素个数,即键的总数。

  3. str(dict):输出字典可打印的字符串表示。

  4. type(variable):返回输入的变量类型,如果变量是字典就返回字典类型。

Python字典包含了以下内置方法:

  1. radiansdict.clear():删除字典内所有元素

  2. radiansdict.copy():返回一个字典的浅复制

  3. radiansdict.fromkeys():创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值

  4. radiansdict.get(key, default=None):返回指定键的值,如果值不在字典中返回 default 值

  5. radiansdict.has_key(key):如果键在字典 dict 里返回 true,否则返回 false

  6. radiansdict.items():以列表返回可遍历的(键, 值) 元组数组

  7. radiansdict.keys():以列表返回一个字典所有的键

  8. radiansdict.setdefault(key, default=None):和 get() 类似, 但如果键不已经存在于字典中,将会添加键并将值设为 default

  9. radiansdict.update(dict2):把字典 dict2 的键/值对更新到 dict 里

  10. radiansdict.values():以列表返回字典中的所有值