python学习3-python变量以及数据类型

1、变量python

变量格式:linux

变量名 = 变量值ide

 

例子:[root@localhost~]# python函数

Python2.7 (r27:82500, Jul 28 2016, 02:42:00) spa

[GCC4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2命令行

Type"help", "copyright", "credits" or "license"for more information.orm

>>>a = 2       #定义变量等号两边有空格字符串

>>> a            #查看变量的值方法1it

2io

>>> print a     #查看变量的值方法2

2

 

1种查看变量值的方法在python的命令行中是可行的。

可是写在脚本中,执行脚本是不能打印出变量a的值的。

 

注意:变量名称不能以数字或是Python的关键字开头。

 

2、数据类型

python常见的数据类型有:

    查看数据类型的函数

    type (变量名称)    这样就能够查看出变量的数据类型

    1. 数字:

            (a)整型(int):

                >>> a = 123

                >>> type (a)

                    <type 'int'>

           

            (b) 长整型(ling):

                    >>> b =12345678901234567890

                    >>> type (b)

                        <type 'long'>

               

            (c) 浮点型 (float)

            浮点型就是小数

                    >>> a = 1.2

                    >>> type (a)

                        <type 'float'>

            (d) 布尔型(FalseTrue):  引用的时候,首字母大写

                 >>> 2 > 1

                        True

                 >>> 2 < 1

                         False

>>> 

    3. 字符串:

        当变量的值  #须要注意的是当字符串做为变量的值的时候,须要用引号引发来

        >>> a = 'abc'

        >>> type (a)

            <type 'str'>

 

、类型转换

   1 str(),repr()或是format()用这个3个内置函数能够将非字符串类型转换为字符串。

        例子:>>> a = 1

             >>> type(a)

                <type 'int'>

             >>> a=str(a)  

             >>> type(a)

                <type 'str'>

     2)强制转换为整型

          int()

     3)转为为浮点型

          float()

     4)将字符串转换为列表

          list()

      5)将字符转转换为元组

          tuples()

      6)将字符串转换为集合

          set()

相关文章
相关标签/搜索