python基础(四)

一、list循环

import copy
# l = [1,1,1,2,3,4,5]
# #1 1,2,3,4,5
# #0 1 2 3 4 5 6
# # l2=[1,1,1,2,3,4,5]
# # l2 = copy.deepcopy(l)# 深拷贝
#
# l2 = l #浅拷贝
# #l2 = l.copy() #浅拷贝
#
# print(id(l))
# print(id(l2))
# for i in l2:
# if i%2!=0:
# l.remove(i)
# print(l)
#循环删list的时候,会导致下标错位,结果是不对的

a='tanailing'
b = a
a='niuniu'

print(a)
print(b)

2、非空即真、非零即真(重要)
#非空即真,非零即真

name = input('请输入名称:').strip()
#name='abc'
#a=''
#l=[]
#d={}
#t=()
#b=None
name = int(name)
if name:
print('输入正确')
else:
print('name不能为空')

三、函数
def my(name,sex):
#位置参数,必填
#默认值参数
#函数体
return name

def db_connect(ip,port=3306):
print(ip,port)

# db_connect('118.24.3.40',3307)
# db_connect('118.24.3.40')
import json
def op_file_tojson(file_name,dic=None):
if dic:
with open(file_name,'w',encoding='utf-8') as fw:
json.dump(dic,fw)
else:
f = open(file_name,encoding='utf-8')
content = f.read()
if content:
res = json.loads(content)
else:
res = {}
f.close()
return res


res = db_connect('118.24.3.40',3307)

def my2():
for i in range(50):
return i

def my3():
a = 1
b = 2
c = 3
return a,b,c

b,c,d = my3()
s = my3()
# print(b,c,d)
# print(s)

# a,b,c = 1,2,3
#
# a = b = c = 1

# def my4(s:str,d:dict):
# print(s)
# print(d)
#
# my4(134,'abcd')


#return 有2个作用
#1、结束函数,只要函数里面遇到return,函数立即结束运行
#2、返回函数处理的结果

四、校验小数类型
def check_float(s):
'''
这个函数的作用就是判断传入的字符串是否是合法的小数
:param s: 传入一个字符串
:return: True/false
'''
s = str(s)
if s.count('.')==1:
s_split = s.split('.')
left,right = s_split
if left.isdigit() and right.isdigit():
return True
elif left.startswith('-') and left[1:].isdigit()
and right.isdigit():
return True
return False
print(check_float(1.3))
print(check_float(-1.3))
print(check_float('01.3'))
print(check_float('-1.3'))
print(check_float('-a.3'))
print(check_float('a.3'))
print(check_float('1.3a3'))
print(check_float('---.3a3'))

#1.5 1.34
#-0.5 -8.4
# 1.5
# a.3
# -----1.3
#正小数
#1、小数点个数为1
#2、小数点左边和右边都是整数
#负小数
# 1、小数点个数为1
# 2、小数点左边和右边都是整数
# 3、负号开头,并且只有一个负号

五、全局变量
import datetime

def get_today():
print(datetime.datetime.today())
name = 'wangcan'#全局变量
names = []
def get_name():
# global name
names.append('hahaha')
name = 'hailong'
print('1、函数里面的name',name) #2 wangcan hailong hailong
def get_name2():
global name

print('2、get_name2',name)#1 hailong wangcan wangcan
get_name2()
get_name()
print(names)

print('3、函数外面的name',name) # 3、hailong wangcan hailong



money = 500
def test(consume):
return money - consume

def test1(money):
return test(money) + money

# money = test1(money)
# print(money)


def test():
global a
a = 5

def test1():
c = a + 5
return c
# test()
res = test1()
print(res)

六、递归
#递归的意思函数自己调用自己
#递归最多递归999
count = 0
# def say():
# global count
# count+=1
# print('say')
# print(count)
# say()
# say()#
# def test1():
# num = int(input('please enter a number:'))
# if num%2==0:#判断输入的数字是不是偶数
# return True #如果是偶数的话,程序就退出了,返回true
# print('不是偶数请重新输入!')
# return test1()#如果不是偶数的话继续调用自己,输入值
# print(test1())#调用test

def db_connect(ip,user,password,db,port):
print(ip)
print(user)
print(password)
print(db)
print(port)

db_connect(user='abc',port=3306,db=1,
ip='sdfsd',password='sdfsd')
db_connect('192','root',
db=2,password='123456',port=123)

# db_connect(password='123456',user='192.468',
# 'sdfsdf','sdfsdf','sdfsd')#不对的

七、可变参数、关键字参数
def my(name,sex='女'):
#name 必填参数、位置参数
#sex 默认值参数
#可变参数
#关键字参数
pass

def send_sms(*args):
#可变参数,参数组
#1、不是必传的
#2、它把传入的元素全部都放到了一个元组里面
#3、不限制参数个数
#4、它用在参数比较多的情况下
for p in args:
print(p)

#关键字参数

def send_sms2(**kwargs):
#1、不是必传的
#2、不限制参数个数
print(kwargs)

send_sms2()
send_sms2(name='xiaohei',sex='nan')
send_sms2(addr='北京',country='中国',c='abc',f='kkk')

#1、不传参数
#2、传1个
#3、传N个

#1、是不是必传的
#2、传入多个参数的时候,它把参数保存到了哪里
# send_sms()
# send_sms(1861231231)
# send_sms(1861231231,121342,1231231,1232342,42342342)

def my(name,country='China',*args,**kwargs):
#1、位置参数 2、默认值参数 3、可变参数 4、关键字
print(name)
print(country)
print(args)
print(kwargs)
# my('xiaojun','Japan','beijing','天通苑',color='红色',
# age=32)

# my('xiaojun','beijing','天通苑',
# color='红色',age=32)

八、集合

# int float list str dict tuple bool set

#集合,
# 1、天生可以去重
# 2、集合是无序
l=[1,1,2,2,3,3]
res = set(l)
l = list(res)
print(res)
{1, 2, 3}

jihe = set() #定义一个空的集合


xingneng =['tanailing','杨帆','liurongxin','小黑']
zdh = ['tanailing','杨帆','liurongxin','小军','海龙']
xingneng = set(xingneng)
zdh = set(zdh)
# res = xingneng.intersection(zdh)#取交集
# res = xingneng & zdh #取交集
# res = xingneng.union(zdh) #取并集,把2个集合合并到一起,然后去重
# res = xingneng | zdh
#差集
# res = xingneng.difference(zdh) #取差集,在a里面有,在b里面没有的
# res = xingneng - zdh #取差集
#对称差集
# res=xingneng.symmetric_difference(zdh)#两个里不重复的值
# res = xingneng ^ zdh
print(res)
import string
l1 = set(string.ascii_lowercase)
print(l1)
# l2 = {'a','b','c'}
l2={1,2,3}
# print(l2.issubset(l1))
# print(l1.issuperset(l2))
# print(l1.isdisjoint(l2)) #有交集返回false,没有交集返回true
# print(l2)
# print(list3.issubset(list1)) # 判断list3是不是list1的子集
# print(list1.issuperset(list3)) # 判断list1是不是list3的父集
# print(list1.isdisjoint(list3)) # 判断list1和list3是否有交集

l2.add('s')#添加元素
# l2.remove('a')#删除指定的元素
l2.pop()#随机删除一个元素
for l in l1:
print(l)

九、模块
#模块
# 1、标准模块,不需要你单独安装,python自带的模块
# 2、第三方模块
# 3、自己写的python
#一个python文件就是一个模块
import random
print(random.randint(100000,999999)) #随机取一个整数
print(random.uniform(1,900))#取一个小数
stus = ['xiaojun','hailong','yangfan','tanailing','yangyue','cc']
print(random.choice('abcdefg'))#随机取一个元素
print(random.sample(stus,2)) #随机取N个元素

l = list(range(1,101))
print('洗牌之前的',l)
print(random.shuffle(l))#洗牌,这个就只能传list了
print('洗牌之后的',l)






原文地址:https://www.cnblogs.com/mengmeng1011/p/9736197.html