python基础之01数据类型-变量-运算浅解

python的数据类型

   1  数字

    数字分为整型(int),长整型(long),浮点型(float),复数(complex)

  整型较为常用的功能:

>>> a=-4
>>> a.__abs__()
4
取绝对值
>>> a=95
>>> a.__divmod__(10)
(9, 5)
地板除取余
>>> a=4
>>> a.__add__(3)
7
加法

   2  字符串(str)

      字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"123"等等,并且,单引号和双引号并没有实质上的区别(同bash不一样)。

      python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空间,并且一旦需要修改字符串的话,就需要再次开辟空间。

      转义字符可以转义很多字符,如 表示换行,\则表示。

      多行字符串'''...''',三个引号也可以表示注释。

   字符串有许多功能:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', 

'__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__',

'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',

'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center',

'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal',

'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower',

'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',

'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

字符串常用功能:

'sdsdsd'.count('s')
3
计数
>>> a='liming'
>>> a.__len__()
6
>>> len(a)
6
长度len
>>> a='liming'
>>> a.index('l')
0
#只显示第一个匹配到的索引值
索引
>>> a
'liming'
>>> '_'.join(a)
'l_i_m_i_n_g'
>>> ' '.join(a)
'l i m i n g'
拼接join
>>> a='    liming   '
>>> a
'    liming   '
>>> a.strip()
'liming'
去除空格
>>> a='liming'
>>> a.split('i')
['l', 'm', 'ng']
分割并将结果转化为列表
>>> a
'liming'
>>> a.upper()
'LIMING'
>>> b='XIAOHONG'
>>> b.lower()
'xiaohong'
大写和小写
>>> a
'liming'
>>> a.replace('l','a')
'aiming'
替换

   3  布尔值

      True(0)/False(1)

      布尔值可以用and、or和not运算

   4  空值

      空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。

python中的变量

   1  变量命名规则

      必须为字母或者数字或者下划线,并且数字不能开头

   2  给变量赋值的注意事项:

      1  数字/布尔/空值可以直接赋值,但是字符串类型必须加引号。

     

>>> a=1            #数字1
>>> type(a)
<class 'int'>
>>> a='1'    #字符串1,必须加引号
>>> type(a)
<class 'str'>
>>> a=True    #布尔a
>>> type(a)
<class 'bool'> 
>>> a=None    #a是空值
>>> type(a)
<class 'NoneType'>

     

   2  在Python中,同一个变量可以反复赋值,而且可以是不同类型的变量。

        例如上例,a被赋值为数字后又赋值为字符……这种变量本身类型不固定的语言称之为动态语言,与之对应的是静态语言。Java就是静态语言。

        int a = 123; // a是整数类型变量

        a = "ABC"; // 错误:不能把字符串赋给整型变量

      3  理解变量在计算机内存中的表示也非常重要。当我们写:

        a = 'ABC'  

      Python解释器干了两件事情:

        在内存中创建了一个'ABC'的字符串;

        在内存中创建了一个名为a的变量,并把它指向'ABC'。

      也可以把一个变量a赋值给另一个变量b,这个操作实际上是把变量b指向变量a所指向的数据

python中的常量

   所谓常量就是不能变的变量,在python中并没有实际的常量,当我们定义

   PI=3.14时,实际上PI还是一个变量,python中并没有任何机制能保证PI的值不会被修改。

python中的运算符

   数字运算

      **  幂运算

      //  整除(地板除)

      %   取余

      /   除法

   位运算

      &  与运算,1和1得1,其余得0

      |  或运算,有一个1就得1

      ^  异或,一真一假为1

   逻辑运算符

      and

      or

      not

   成员运算符

      is

      is not

>>> 3  is 3
True
>>> type(3) is int
True

       

 

no copyright !! 个人原创博客欢迎转载,可以不保留出处。
原文地址:https://www.cnblogs.com/MnCu8261/p/5418564.html