007-Python语言开发PythonComputation(三)

 

 

 

Function方法

 

方法其实就是

我们把一段代码写在一个方法里面

当我们调用这个方法的时候,那么就执行这个方法里面的代码

 

举个例子

先来写一个简单的方法

def printHelloworld():
    print('helloworld')

printHelloworld()
printHelloworld()
printHelloworld()

先写一个def, 然后写方法的名字

我们这里的方法的名字叫做printHelloworld

然后后面加()括号,再加一个:冒号

 

然后下面的三句话不属于方法,

而是我们在调用方法

我们调用了三次printHelloworld()

所以结果是

 

 

 

然后我们再写一个简单的方法

def introduceMyself():
    print('My name is Eddie')
    print('I am 20 years old')
    print('I like playing football')

introduceMyself()
introduceMyself()
introduceMyself()

这个方法里面写了三句话

上面4行就是这个方法的整体

先是定义方法名字,然后是方法里面的结构

 

然后我们调用了这个方法三次

结果就是

 

 

 

然后我们再来写一个方法

我们想要传入两个数字,然后获得这两个数相加的结果

def add(x, y):
    return x + y


a = add(2, 3)
print(a)

看我们这个方法

还是开头一个def

然后add是方法名字

然后括号里面是参数,也就是我们需要往这个方法里面传入什么东西

比如下面的

a = add(2,3)

就是传入了2和3这两个数字

我们的方法中return了x+y,也就是返回了x+y的结果

 

 

 

 

再来写一个稍微复杂一点的方法

假设我们现在要写一个方法

当我们传入一个列表的时候

就可以求出这个列表的总和

那么我们的参数就是一个list,返回的就是一个数字

def getSumOfList(list):
    sum = 0
    for item in list:
        sum = sum + item
    return sum


l = [1, 2, 3, 4]
s = getSumOfList(l)
print(s)

 

先看方法

方法名字叫 getSumOfList,也就是获取list的总和,参数是一个list

然后先定义一个总和sum=0

然后遍历这个list,把数字加起来

 

 

 

 

 

全局与局部Global and Local

什么是全局和局部

什么是全局变量,什么是局部变量

我们举个例子

a = 'I am Global'

def test():
    print(a)

test()

此时,我们可以在test方法中,获得a

 

然后我们修改一下代码

def test():
    a='I am Local'
    print(a)

test()
print(a)

这里,我们在方法中定义了a=" I am Local",然后打印

然后在外面也用print打印a,

结果是

结果发生了错误, name a is not defined

也就是说,在方法的外面,我们不能使用方法里面的a

 

 

修改一下代码

def test():
    a='I am Local'
    print(a)

a='I am Global'
test()
print(a)

先定义方法,此时方法还没有被调用

然后a=' I am Global'

此时,方法内的a被修改了吗,没有

因为外面是全局变量,里面是局部变量

 

 

 

再修改一下

def test():
    a='I am Local'

a='I am Global'
test()
print(a)

a=Global

然后调用test方法,

然后打印a

打印出来的结果当然是

 

 

所以

方法中可以访问全局变量

但是局部不能修改全局变量

全局也不能修改局部变量

 

 

 

 

 

 

 

list与tuple

list是[]中括号

tuple是()小括号

他们的区别是,list是可变的,tuple是不可变的

举个例子

list=[1,2,3]
list[1]=99
print(list)

tuple=(1,2,3)
tuple[1]=99
print(tuple)

 

list的第二个数字被改成了99

但是tuple does not support item assignment

tuple不支持被修改

 

 

 

注意,当tuple当中只有一个东西的时候

如果不打冒号,会自动变成这个东西

举个例子

tuple01=(1)
print(type(tuple01))

tuple02=(1,)
print(type(tuple02))

tuple03=('a')
print(type(tuple03))

我们看到

第一个变成了int

第二个加了冒号,所以还是tuple

第三个变成了str

 

 

 

另外,当不用括号的时候,会自动变成tuple

举个例子

a=1,2,3
b=(1,2,3)
print(type(a))
print(type(b))

我们看到两个都是tuple

 

 

 

 

 

切片

list=['a','b','c','d','e']
print(list[2:4])
print(list[:3])
print(list[-4:-2])
print(list[-4:])


 

 

 

多重列表

list01 = ['a', 'b', 'c', 'd', 'e']
list02 = ['a', 'b', 'c', 'd', 'e']
list03 = ['a', 'b', 'c', 'd', 'e']
list04 = [1, 2, 3]
list05 = [1, 2, 3, 4, 5]
list = [list01, list02, list03, list04, list05]
print(list)

也就是列表里面可以放列表

很简单的嵌套

 

 

 

交换元素

a = 1
b = 2
a, b = b, a
print(a, b)

list=[1,2,3,4,5]
list[0],list[1]=list[1],list[0]
print(list)

 

前面是交换a和b,

后面是交换列表list的第一个和第二个,

都是一样的道理

 

 

 

删除声明

a = 1
del a
print(a)

 

用del就删除了这个变量

所以就找不到了

 

 

 

删除元素

list=[1,2,3,4,5,6,7]
print(list)

del list[0]
print(list)

del list[2:4]
print(list)

 

 

 

重复repetition

list=[1,2,3]
list=list*3
print(list)

str='abc'
str=str*3
print(str)

 

 

 

 

列表list的方法

1.append()

2.insert()

3.remove()

4.pop()

5.extend()

list=[1,2,3,4]
list.append(5)
print(list)

list.insert(0,99)
print(list)

list.remove(1)
print(list)

list.pop(0)
print(list)

list.extend([1,2])
print(list)

 

 

排序和反序

list=[1,2,3,4]

list.reverse()
print(list)

list.sort()
print(list)

 

 

 

 

 

 

sort和sorted

list = [4, 3, 2, 1]
list.sort()
print(id(list))

list = [4, 3, 2, 1]
list2 = sorted(list)
print(list)
print(list2)
print(id(list))
print(id(list2))

 

 

 

 

 

Sequence内置方法

1.len

2.max

3.min

list = [4, 3, 2, 1]
print(len(list))
print(max(list))
print(min(list))

 

 

 

 

 

 

Conversion类型转换

l = [4, 3, 2, 1]
print(tuple(l))
print(str(l))

s = 'abcdef'
print(tuple(s))
print(list(s))

t = (1, 2, 3, 4, 5)
print(list(t))
print(str(t))

 

 

 

 

 

loop循环遍历序列

l = [4, 3, 2, 1]
for item in l:
    print(item)

s = 'abcdef'
for item in s:
    print(item)

t = (1, 2, 3, 4, 5)
for item in t:
    print(item)

 

 

 

 

 

二维序列遍历

l = [4, 3, 2, 1]
s = 'abcdef'
t = (1, 2, 3, 4, 5)
a=[l,s,t]

for sequence in a:
    for item in sequence:
        print(item)

 

 

 

 

练习题

我们来输出一个

[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]]

 

list = []
for i in range(1, 7):
    son = []
    for j in range(1, i + 1):
        son.append(i)
    list.append(son)

print(list)

 

 

 

 

 

 

再来一个练习

输出

6 6 6 6 6 6

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

for i in range(6, 0, -1):
    for j in range(1, i + 1):
        print(i, end=" ")
    print("")

 

 

 

总结string,tuple,list

 

 

 

第三节课

好好学习

天天向上

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值