python的使用(不定期更新)

这是本人在日常使用时做的笔记,不喜勿喷

识别数据的类型

def is_string(s):  # 判断是否为字符串
    if isinstance(s, str):
        return True
    else:
        return False


def is_int(s):  # 判断是否为整数
    if isinstance(s, int):
        return True
    else:
        return False


def is_list(s):  # 判断是否为列表
    if isinstance(s, list):
        return True
    else:
        return False


def is_dict(s):  # 判断是否为字典
    if isinstance(s, dict):
        return True
    else:
        return False


def is_tuple(s):  # 判断是否为数组
    if isinstance(s, tuple):
        return True
    else:
        return False


def is_num(num):  # 判断是否为数字
	s = num.isdigit()
	if s:
		return True
    else:
        return False


def is_word(data):  # 判断是否为字母
	s = data.isalpha()
	if s:
		return True
    else:
        return False


def is_data(data):  # 判断是否为字母和数字混合
	s = data.isalnum()
	if s:
		s = num.isdigit()
		if s:
			return '只是数字'
	    else:
	    	s = data.isalpha()
			if s:
				return '只是字母'
		return '是字母和数字混合体'
    else:
        return False

判断字符串长度

def data_length(data):
	return len(data)

python的类型转换(常见的)

int(x, base=10)

参数

  • x – 字符串或
  • 数字。 base – 可选,进制数,默认十进制。

用法

def change_data(data):
	data = int(data) # 转换为一个整数(data需是数字,默认为10进制)

实例

>>>int()               # 不传入参数时,得到结果0
0
>>> int(3)
3
>>> int(3.6)
3
>>> int('12',16)        # 如果是带参数base的话,12要以字符串的形式进行输入,12 为 16进制
18
>>> int('0xa',16)  
10  
>>> int('10',8)  
8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值