python基础语法三
集合:
1.不同元素组成
2.无序
3.集合中的元素必须是不可变类型
s = {1, 2, 3 }
#定义集合
s = set('hello')
print(s) s.pop()
#指定删除
s.remove("")
s.remove("sss") #删除元素不存在会报错
s.discard('sbb') #删除元素不存在,,不会报错 print(s)
集合的运算关系:
python_1 = ['lcg', "szw", "zjw"]
linux_1 = ["lcg", "szw"]
#取公共部分
python_and_linux_1 = []
for p_name in python_1:
if p_name in linux_1:
python_and_linux_1.append(p_name) print(python_and_linux_1) p_s = set(python_1)
l_s = set(linux_1) #取交集的部分
print(p_s.intersection(l_s))
print(p_s&l_s) # 去并集的部分
print(p_s.union(l_s))
print(p_s|l_s) #差集
print(p_s-l_s)
print(p_s.difference(l_s)) #字符串格式化 msg = "i am hobby is alex", %"lhf"
msg = "i am hobby is %s ", %("lhf", "alex") \
函数:
python中函数的定义方法:
def test(x):
"The function definitiens"
x+=1
return x
test : 函数名
() : 内可定义形参
"":文档描述
x+=1: 泛指代码块或程序逻辑
return : 定义返回值
函数名()
#改成用函数写
def calc(x, y): # x, y, 形参
res = x**y
return res c = calc(a, b)# a, b 实参
print(c)
默认参数:
def handle(x, type = "mysql"):
print(x)
print(type)
handle('hello' , type='sqlite')
#参数组 : **字典 , *列表
def test(x, *args)
print(x)
print(args)
print(args[2])
test(1,2,3,4,5,6)
test(1, {"name":"alex}) def test(x, **kwargs):
print(x)
print(kwargs)
test(1, y=2, y=3) def test(x, *args, **kw args):
print(x)
print(args)
print(kwargs)
局部变量,和全局变量
name = lhf # 全局变量 def chang():
name = lhf #局部变量
print(name) def chang():
global name = lhf #全局变量
print(name) 函数就是变量!!!
函数递归:
def calc(n):
print(n)
calc(n) calc(10) #自己调用自己! def calc(n):
print(n)
if int(n/2)==0
return n
return calc(int(n/2)) calc(10) person_list = ['alex', 'wupeiqi', 'yuanhao', 'linhaifeng', 'zsc']
def ask_way(person_list):
if len(person_list) == 0:
return '根本没人知道'
person = person_list.pop(0)#取出一个值。
if person == 'linhaifeng':
return '%说: 我知道,老男孩就在沙河汇德商厦下地铁就是' %person
ask_way(person_list) ask_way(person_list)
递归特性:
1.必须有一个明确的结束条件
2.每次进入更深一层的递归时,问题规模相比上一次递归都应有减少
3.递归效率不高, 递归层次会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入
一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧,由于栈的大小不是无限的,所以,递归调用次
数过多会导致栈溢出)
python基础语法三的更多相关文章
- Python 基础语法(三)
Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...
- Python 基础语法(四)
Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...
- Python 基础语法(二)
Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...
- python基础语法(四)
--------------------------------------------接 Python 基础语法(三)---------------------------------------- ...
- Python基础语法(三)
Python基础语法(三) 1. 数值型数据结构 1.1 要点 在之前的博客也有提到,数值型数据结构在这里就不过多介绍了.在这里提及一些需要知道的知识点. int.float.complex.bool ...
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- Python 基础语法
Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...
- python学习第五讲,python基础语法之函数语法,与Import导入模块.
目录 python学习第五讲,python基础语法之函数语法,与Import导入模块. 一丶函数简介 1.函数语法定义 2.函数的调用 3.函数的文档注释 4.函数的参数 5.函数的形参跟实参 6.函 ...
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
随机推荐
- linux 7 关闭防火墙 开启sshd服务
启动一个服务:systemctl start firewalld.service关闭一个服务:systemctl stop firewalld.service重启一个服务:systemctl rest ...
- DS博客作业03——栈和队列
1.本周学习总结 谈谈你对栈和队列结构的认识及学习体会. 栈和队列的本质就是线性表.所以,相应的栈跟队列都有两种存储结构:顺序存储结构.链式存储结构. 栈的特点是后进先出,根据栈时进时出的规则,出栈的 ...
- linux 一键安装lnmp环境
①,登陆后运行:screen -S lnmp ②.如果提示screen: command not found 命令不存在可以执行: yum install wget ③. 执行命令:wget ...
- 获取上一页面url
console.log(window.location.host); #js获取当前域名 console.log(window.location.href); #js获取当前url console.l ...
- Web基础学习
Servlet和Servlet容器.Web服务器概念:https://blog.csdn.net/lz233333/article/details/68065749 <初学 Java Web 开 ...
- mysql <=> null 问题
SELECT * FROM cms_user WHERE age=NULL; Empty set (0.03 sec) 查询表中记录age值为null mysql> SELECT * FROM ...
- SSH框架分页
DAO层 /** * 分页查询全部员工,获取总记录数 */ public int totalPage(String className); /** * 分页查看,查看首页 */ public List ...
- Gulp 之图片压缩合并
同事需要处理很多的图片,由于UI那边提供图片比较大,为了性能好一点,程序包小一点,因此希望我帮忙做成小程序来完成此工作. 其实之前做过一个grunt写的图片压缩合并工具,当时是为了处理270多个国家/ ...
- 颜色表 及 p em fr
#000000 #2F0000 #600030 #460046 #28004D #272727 #4D0000 #820041 #5E005E #3A006F ...
- 5ci