python集合,for循环,break,continue,函数定义以及return

python的集合看着是真的乱,啥都可以放;
break,continue和Java用法一样;
for循环用的看着也有些乱;

# python 中的集合使用[]中括号,里面基本数据类型和复杂数据类型都可以混合存储
# 需要特别注意的是python中存在正向和反向索引 Java中和其他大部分语言是不存在的
L1 = [1, 2, 3, ]
L2 = [1, "奥特曼", 2]
print(L1);
# 输出的值为3
print(L1[-1]);

# range(start,end,step) start 开始数字,end 结束数字,step 每次增加多少,默认为1;
a = list(range(1, 6))
print(a)

for i in a:
    print(i)

b = 1;
while b < 10:
    b += 1
    print(b)

python函数的定义和调用

# 函数定义 def关键字 后面是函数的名字
def funcName():
    print("python语言的第一个函数");

文件结构如下
在这里插入图片描述

我要在另一个包调用的话如下图
(好乱,引入的地址尽然可以在代码下面直接写,看得我头皮发麻);

# from 来自哪里,import 引入哪个方法
from pythonTestFunction.testFunction import funcName

funcName();

在这里插入图片描述

再定义一个有参函数

def testFunction(name):
    print(name);

在这里插入图片描述

我发现尽然还要再引用进来这个对应的方法…
在这里插入图片描述

return的用法和Java类似;
但是在Django中会python的renturn我感觉是属于万物皆可return;
你可以定义了return,但是不返回任何东西,因为默认返回None;
也可以不定义return,还是因为默认返回None;
python可以返回多个参数而只需要使用一个参数进行接收,python会自动把数据打包为tuple类型;
python可以返回多个参数你也使用对应数量的多个参数进行接收;
python可以返回多个参数你也使用不对应数量的多个参数进行接收,这个时候就报错了,python不支持,代码在下方;
在使用了一段时间Java之后学python发现这个语言的结构真的是神奇…

python中可以忽略参数顺序,指定参数传递,再次吐槽python的规则好乱;
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

当传递复杂的数据类型时,比如一个集合,写法也很特殊
在这里插入图片描述
只需要在集合前面加个*号就可以,而且后面的函数接收到了集合之后,尽然不需要使用下标或者是其他方式拿出来,自己对号入座了,但是集合的长度大小必须符合函数的入参数量,否则会报错
,在这里插入图片描述

看到字典集合(Java中的map)的时候我感觉我要崩溃了,再次吐槽python;
python中的键值对如下

L4 = {"a":1,"b":2,"c":3};

只需要一个大括号就可以了,吐血中,万物皆可集合,python这个语言真的是神奇;
字典集合传递的时候加两个**

还可以定义星号元组参数函数,这个类似于Java中的字符串的…
定义

# 定义星号元组参数函数
def testFunction4(*arg):
    print("进入星号元祖函数");

调用

#不传递参数,默认传递空列表
testFunction4();
#传递了参数,将参数打包进列表进行传递
testFunction4(1);

定义双星号字典函数,再次吐槽python
定义

# 定义星号元组参数函数
def testFunction5(**arg):
    print("进入双星号元祖函数");
    print(arg);

调用

testFunction5();
testFunction5(a=1);

下面是完整的代码和python测试工程
在这里插入图片描述

# python 中的集合使用[]中括号,里面基本数据类型和复杂数据类型都可以混合存储
L1 = [1, 2, 3, 4]
L2 = [1, "奥特曼", 2]
print(L1)

# range(start,end,step) start 开始数字,end 结束数字,step 每次增加多少,默认为1;
a = list(range(1, 6))
print(a)

for i in a:
    print(i)

b = 1;
while b < 10:
    b += 1
    print(b)


# from 来自哪里,import 引入哪个方法
from pythonTestFunction.testFunction import funcName
from pythonTestFunction.testFunction import testFunction
from pythonTestFunction.testFunction import testFunction2
from pythonTestFunction.testFunction import testFunction3
from pythonTestFunction.testFunction import testFunction4
from pythonTestFunction.testFunction import testFunction5
from pythonTestFunction.testFunction import testFunction6
from pythonTestFunction.testFunction import testFunction7

funcName();
b = testFunction(age=1,name='张三');
print(b)
# testFunction2(1,2,3,4)
L3 = [1, 2, 3, 4]
testFunction2(*L3)

testFunction3(L3)

L4 = {"a":1,"b":2,"c":3};

testFunction4();
testFunction4(1);
testFunction5();
testFunction5(a=1);
result = testFunction6(1);
print(result)
print(type(result));

result0,result1,result2 = testFunction6(1);
print(result0,result1,result2)
print(type(result0));
print(type(result1));
print(type(result2));
# 函数定义 def关键字 后面是函数的名字
def funcName():
    print("python语言的第一个函数");


def testFunction(name, age):
    print('姓名是:' + name, "年龄是:" + str(age));
    return 3;


def testFunction2(arg0, arg1, arg2, arg3):
    print(arg0, arg1, arg2, arg3);


def testFunction3(arg0):
    print(arg0);
    print(arg0[0]);


# 定义星号元组参数函数
def testFunction4(*arg):
    print("进入星号元祖函数");


# 定义星号元组参数函数
def testFunction5(**arg):
    print("进入双星号元祖函数");
    print(arg);


def testFunction6(arg):
    arg0 = arg + 1;
    arg1 = arg + 2;
    return (arg, arg0, arg1);


def testFunction7(arg):
    arg0 = arg + 1;
    arg1 = arg + 2;
    return (arg, arg0, arg1);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值