afox123 发表于 2018-8-7 09:39:30

Python基础语法介绍(3)

元组
  基本概念、特性


[*]顺序存储相同/不同类型的元素
[*]定义:使用()将元素括起来,元素之间用“,”括开
[*]特性:不可变,不支持添加,修改,删除等操作
[*]查询:通过下标查询元组指定位置的元素
[*]其他

[*]空元组定义:non_tuple = ()
[*]只包含一个元素的元组:one_tuple = ("one",)

  顺序存储相同/不同类型的元素
  

user_info = ("Wukong",100, "male", "13834928470")  

  元组不同于列表,它不支持增,删,改。
  

#不支持增删改操作,例如删除一个元组元素  
del user_info
  

  
输出结果:
  del user_info
  
TypeError: 'tuple' object doesn't support item deletion
  

  通过下标查询元素
  

#查询下标1的元素  
age = user_info
  
print("Age is %d"%age)
  

  遍历元组
  

for item in user_info:  print (item, end = "")
  

  
输出结果:
  
Wukong
  
100
  
male
  
13834928470
  

字典
  基本概念、特性


[*]存储key-value键值对类型的数据
[*]定义:{key:value, key:value, ...};字典里不能出现相同的键名
[*]特性:具有增删改操作
[*]查询:根据key查找value
[*]内置方法:get,keys,values,items,clear
[*]循环遍历字典
  内置方法keys的用法
  

user_info_dict = {"name":"zhangsan", "age":"30", "tel":"13523464219"}  
for key in user_info_dict.keys(): #key值组成的列表
  print(user_info_dict)
  

  
输出结果:
  
zhangsan
  
30
  
13523464219
  

  内置方法items的用法
  

#用法(1)  
for item in user_info_dict.items():
  print(type(item))
  print(item) #key
  print(item) #value
  

  
输出结果:
  
<class 'tuple'>
  
name
  
zhangsan
  
<class 'tuple'>
  
age
  
30
  
<class 'tuple'>
  
tel
  
13523464219
  

#用法(二)  
for key, value in user_info_dict.items():
  print(key)
  print(value)
  

  
输出结果:
  
zhangsan
  
age
  
30
  
tel
  
13523464219
  

集合
  基本概念、特性


[*]无序存储不同数据类型不重复元素的序列
[*]定义:{“element1”,“element2”,element3“}
[*]特性:无序存储,可以对元素列表去重
[*]方法:add,update(序列),remove,pop
[*]集合操作:

[*]交集:intersection
[*]并集:union
[*]差集:difference
[*]对称差集:symmetric_difference

  集合对列表去重
  

id_list = ["id1", "id2", "id3", "id1", "id2"]  
distinct_set = set(id_list) #去重
  
print(distinct_set)
  

  
输出结果:
  
{'id1', 'id2', 'id3'}
  

  集合对字符去重
  

string_set = set("hello")  
print(string_set) #字符串看成是带下标的列表,字符串会拆开然后列表去重
  

  
输出结果:
  
{'h', 'o', 'e', 'l'}
  

  
Note:set是无序的。所以你再运行一次,列表的元素顺序是变化的。
  

  空集合
  

none_dict = {} #创建一个空字典  
none_set = set() #创建一个空集合
  
print(none_set)
  

  
输出结果:
  
set()
  

  in 和not in
  

user_id = "id1"  
if user_id in distinct_set:
  print(user_id)
  
else:
  print("not find")
  

  
输出结果:
  
id1
  

  add:添加元素到集合
  

name_set = {"zhangsan", "lisi"}  
name_set.add("wangwu")
  
print(name_set)
  

  
输出结果:
  
{'lisi', 'wangwu', 'zhangsan'}
  

  update(序列)
  

name_set.update(["wukong", "lisi", "bajie"]) #列表中的每个元素去重后添加到set里  
print(name_set)
  

  
输出结果:
  
{'wukong', 'bajie', 'lisi', 'zhangsan', 'wangwu'}
  

函数
  函数定义
  

def FunName (parameter_list)  function block
  return value
  

  例子一:有参数有返回
  

def Sum(x, y):  sum = x + y
  return sum
  

  
#函数调用
  
sum = Sum(1, 2)
  
print(sum)
  

  
输出结果:
  
3
  

  例子二:有多个返回
  

def x_y_comp_tuple(x, y):  res1 = x + y
  res2 = x * y
  return res1, res2
  

  
a, b = x_y_comp_tuple(2, 3)
  
print("a = {}, b = {}".format(a, b))
  

  
输出结果:
  
a = 5, b = 6
  

  例子三:列表作为返回值
  

稍后填充  

字符串 :常用内置方法
  find(str[, start, end])
  

line = "hello world hello python"  
print(line.find("world"))
  
print(line.find("hello"))
  
print(line.find("hello", 6)) #查找范围从索引”6“开始
  

  
输出结果:
  
6
  
0
  
12
  

  count(str[, start, end])
  

print(line.count("hello")) #查找文本的某个字段或者某个字符串中某个单词  
输出结果:
  
2
  

  replace(old, new[, count])
  

new_line = line.replace("hello", "hi", 1) #count不指定就全部替换  
print(line)
  
print(new_line)
  
输出结果:
  
hello world hello python
  
hi world hello python
  

  split(sep[, maxsplit])
  

line.split(" ") #以空格作为分隔符,以列表方式返回  
输出结果:
  
['hello', 'world', 'hello', 'python']
  

  
#指定分隔的个数
  
line.split(" ", 1)
  
输出结果:
  
['hello', 'world hello python']
  

  startswith(prefix[, start, end])
  endswith(suffix[, start, end])
  upper:字符串所有字符大写
  lower:字符串所有字符小写
页: [1]
查看完整版本: Python基础语法介绍(3)