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 二丶运算符 ...
随机推荐
- cmd非运行完再保存,结果显示&保存同时进行
#coding:utf-8 """ fps信息获取到文件 """ import sys import subprocess class Lo ...
- Matlab-7:偏微分方程数值解法-李荣华-有限元解导数边界值的常微分(Galerkin方法)
p47.(实习题-李荣华)用线性元求下列边值问题的数值解 tic; % this method is transform from Galerkin method %also call it as f ...
- Android 工程越来越大,运行变卡解决方法
打开AS的安装目录 android-studio/bin/studio.exe.vmoptions studio64.exe.vmoptions -Xms2048m-Xmx2048m-XX:Reser ...
- 利用itext将html页面转成pdf(不模糊)
1.maven项目进入依赖 <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId> ...
- IntelliJ IDEA激活
以前一直使用eclipse,直到后来发现了IntelliJ IDEA,就爱上了它. 不过可惜的是,community版本虽然是免费的,不过功能相对较少,而ultimate版本的又需要花钱.但是我穷啊, ...
- DHCP协议分析(Wireshark)
一.说明 一是很多时候IP都是设置成通过dhcp动态获取的,但一直不太清楚dhcp的具体交互过程:二是加上前几天有同事问知不知道DHCP具体交互过程:三是这两天正好在分析协议.所以就顺道来看一下. 如 ...
- linux—epoll
一.epoll服务端实现中需要的3个函数: epoll_create:创建保存epoll文件描述符的空间. epoll_ctl:向空间注册并注销文件描述符. epoll_wait:与select函数类 ...
- redis最全配置讲解
#redis.conf# Redis configuration file example.# ./redis-server /path/to/redis.conf ################# ...
- 保存cookie状态封装
from urllib import request, parsefrom urllib.error import HTTPError,URLError#保存cookiefrom http impor ...
- bootstrap全局样式二
加form-grope是为了以后更好的管理,一组form写一个form-grope 显示如下: 并排显示的话,给用户名前面再加一个div,再加horizontal,如下,并且加上control-lab ...