初级函数

def printInfo():

print('--------------------------------')

print('生命苦短,我用python')

print('--------------------------------')

printInfo()

def add2Num(a,b):

c=a+b

print(c)

add2Num(11,33)

def nameinfo(name,age=36):

print("Name",name)

print("Age",age)

nameinfo(name="kiki")

nameinfo("kikii",age=29)

def test(a,b,*args,**kwargs):

print(a)

print(b)

print(args)

print(kwargs)

test(11,222)

#空元组、空字典

test(11,22,33,44,m="sunqicheng",n="nan")

def getTemprature():

return 24

temperature=getTemprature()

print('当前的温度为:',temperature)

def sum(n):

sum=0

    i=1

    #再记不住,就是这种累加和的,要用三个变量啊,一个sum 一个n也就是总数一个也就是递增的i

    while i<=n:

sum=sum+i

i+=1

    return sum

print(sum(10))

#Llocal函数内的区域,包括局部变量和形参

#Enclosing 外面嵌套函数的区域,常见的是闭包函数外的函数

#Gglobal 全局作用域

#B内建作用域

----------------------------------------------------------------------------------------------------

import random

#这下面都是浮点数,也就是小数

print("random():",random.random())

print(random.uniform(1,10))

print(random.uniform(10,1))

#从这里开始是整数

print(random.randint(12,20))

print(random.randint(13,15))

print(random.randrange(10,100,5))

print(random.choice(['sunqicheng','is','handsome','boy','saynothing']))

print(random.choice(("tuple","list","dict","sequence")))

-----------------------------------------------------------------------------------------------------------

import random

p=['sunqicheng','is','handsome','boy','saynothing']

random.shuffle(p)

print(p)

#['boy', 'is', 'handsome', 'saynothing', 'sunqicheng']

import random

p=['sunqicheng','is','handsome','boy','saynothing']

slice=random.sample(p,3)

#random.shuffle(p)

print(slice)

你可能感兴趣的:(初级函数)