Python基础知识

一、初识基本数据类型

类型:
int(整型)     在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647     在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807   long(长整型)     跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。     注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。   float(浮点型)     浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。   complex(复数)     复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。 注:Python中存在小数字池:-5 ~ 257

1、数字

2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子

更多

 

2、布尔值

 真或假
 1 或 0

3、字符串

"hello world"
万恶的字符串拼接:
  python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
字符串格式化
name = "alex"
print "i am %s " % name
 
#输出: i am alex

PS: 字符串是 %s;整数 %d;浮点数%f

字符串常用功能:
  • 移除空白
  • 分割 
  • 格式化
  • 长度
  • 索引
  • 切片
  • 查找
  • 其他
移除空白:
# 默认去除前后空格,()中可指定
username.strip()

分割:

# 分割
a='abc,ddc,adc'
b=a.split(',')
# 按符号合并
print('|'.join(a))

格式化:

# 格式化
msg='Hello,{name},I age is{age}'
msg_print=msg.format(name='luke',age=101)
print(msg_print)
------------------------------------------------
msg2='ha{0},ddd{1}'
print(msg2.format('luke',33))
#简单格式化
a='yy'
b=12
print('你好 我是%s,我的年龄是%d'%(a,b))

长度:

#长度
a='yy ddd hh'
num=a.count('y')
print(num)
-----------------------
num=len(a)
print(num)

索引:

# 索引
a='yy ddd hh'
print(a[1])

切片:

# 切片
a='yy ddd hh'
print(a[0:4])

查找:

# 查找 -1代表没找到
a='yy ddd hh'
b=a.find('h')
print(b)

其他:

# 填充字符
print(a.center(40,'-'))
# 判断是否为数字
age=12
age.isdigit()
# 判断是否有特殊字符,有为(False),没有为(True)
a='yy!sdf'
print(a.isalnum())
#判断以什么字符结尾
a='yy!sdf'
print(a.endswith('df'))
# 大小写转换
a='yysdf'
b='YYYDIS'
print(a.upper())
print(b.lower())
print(a.upper().lower())
更多

4、列表

创建列表:
name_list = ['tlh', 'hello','seven', 'eric']
或
name_list = list(['tlh','hello','seven', 'eric'])
注:操作前必须创建列表

基本操作:

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 循环
  • 包含
  • 其他

  索引

# 修改
name_list[0]='n'
# 获取
name=name_list[3]

切片

# 直接取元素
print(name_list[0])
# 取列表中的一段
print(name_list[0:])# 表示取全部
print(name_list[1:3])# 取第一到二个元素
# 步长
print(name_list[0::2])

追加

# 在最后面追加
name_list.append('dd')
print(name_list)
#插入
name_list.insert(3,'dd')
print(name_list)

删除

# 删除列表多个值(删除内存中的值)
del name_list[0:2]
# 删除所有
del name_list
# 删除列表单个值
name_list.remove('tlh')

长度

#值为tlh的有几个
a=name_list.count('tlh')

包含

# in用于包含
if 'tlh' in name_list:
    print('正确')
name_list = ['tlh', 'hello','seven', 'eric']
name_new_list=['cc','dd']
#包含另一个列表
name_list.extend(name_new_list)
print(name_list)

循环

for i in range(name_list.count('tlh')):
    name_tag=name_list.index('tlh')
    name_list[name_tag]=999999999
print(name_list)
其他:
直接获取元素
# 直接从列表中获取
name_list.pop(2)
#获取最后一个
name_list.pop()
反转
#反转
name_list.reverse()
print(name_list)
排序
# 排序
name_list.sort()
复制
# 复制是共享一份数据,而嵌套的数据是内存的地址。分为浅cope和深cope
#浅cope,最外面一层,
name_list.copy()
#深cope,完全复制一份,独立
import copy
copy.deepcopy()
更多

5、元组(有序且只能传数字)

创建元组:
ages = (11, 22, 33, 44, 55)
或
ages = tuple((11, 22, 33, 44, 55))
基本操作:
lass tuple(object):
    """
    tuple() -> empty tuple
    tuple(iterable) -> tuple initialized from iterable's items
    
    If the argument is a tuple, the return value is the same object.
    """
    def count(self, value): # real signature unknown; restored from __doc__
        """ T.count(value) -> integer -- return number of occurrences of value """
        return 0

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        T.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def __add__(self, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __hash__(self): # real signature unknown; restored from __doc__
        """ x.__hash__() <==> hash(x) """
        pass

    def __init__(self, seq=()): # known special case of tuple.__init__
        """
        tuple() -> empty tuple
        tuple(iterable) -> tuple initialized from iterable's items
        
        If the argument is a tuple, the return value is the same object.
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ T.__sizeof__() -- size of T in memory, in bytes """
        pass
更多参数

相关文章:

  • 2021-10-30
  • 2021-05-05
  • 2021-06-29
  • 2021-08-12
  • 2022-02-12
  • 2021-04-19
  • 2022-12-23
  • 2022-03-05
猜你喜欢
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案