• python变量中的数据(一),数字和字符串


    先回顾一下上一小节中的内容,变量的概念。

    变量就是标识符,代表了Memory(内存)中的某个存储值的位置。标识符的命名要符合python的语法规则。

    python中标识符的命名规则是,标识符只可以以字母('A'..'Z'或者'a'..'z')或者'_'开头,后面可以跟多个字母或者数字(0..9)或者'_',python的保留字不可以作为标识符。

    我们在通过下面的例子来理解定义一个变量,或者给一个变量赋值在python中的意义。

    >>>age = 12

    >>>id(age)

    输出 10416140

    以图表示为

    图1

    id(object)是python中内置的函数用来输出object在内存中地址(object所代表的位置)。

    我们把值12赋值给变量age,12存放在Memoy第10416140的位置。(不同的机器这个值输出可能不一样)。

    我们再回顾下我们第一个程序welcome.py中的代码

    1 #welcome.py
    2 #Bad boy and Beautiful girl are python story role
    3 #Welcome to Python world,enjoying yourself
    4 print("Hi, I'm Bad boy!")
    5 
    6 print("Hello, I'm Beautiful girl!")
    7 
    8 print("Welcome to Python world")

    比较一下,age  = 12 和 “Welcome to Python world”的区别?

    age年龄我们很自然的给予数字值,而“Welcome to Python world”是使用双引号括起来的一句由英语单词组成的一句话。

    我们的大脑很自然的给我们要描述的数据做了分类处理(简单分出数字和“语句”),在python中使用“数据类型”的概念来实现这种分类方法。

    python中支持的数字类型有,整数,浮点数(带小数的数)和复数。“语句”被理解为字符串类型。

    python提供了一个type(object)函数来检测对象的类型。

    >>>value =  42

    >>>type(value)

    输出,<type 'int'>

    说明,int表示整数类型。

    >>>id(value)

    在内存中的地址为,10415780

    >>>value = 42.2

    >>>type(value)

    输出,<type 'float'>

    说明,42.2是浮点数

    >>>id(value)

    在内存中的地址为,11380160

    >>>value = 3 + 4j

    >>>type(value)

    输出,<type 'complex'>

    说明,3 + 4j是复数

    >>>id(value)

    在内存中的地址为,12288584

    >>>welcome = 'Welcome to Python world'

    >>>type(welcome)

    输出,<type 'str'>

    说明,welcome是字符串类型。

    >>>id(welcome)

    在内存中的地址为, 12277504

    python是动态类型的语言也就说不要我们在编程中显示的指定数据的类型,数据的类型由python解释器在运行时根据程序上下文推定。

    数据类型不仅定义了数据是什么同时也规定了可以在数据上执行的操作。

    计算一个矩形的面积(整数运算)。

    #rect_area.py
    #version 1.0
    
    width = 12
    heigh = 13
    area = width * heigh
    
    print("width is ")
    print(width)
    
    print("heigh is ")
    print(heigh)
    
    print("area is ")  
    print(area)  

    计算一个矩形的面积(浮点运算)

    #rect_area2.py
    #version 1.1
    
    width = 12.2
    heigh = 13.3
    area = width * heigh
    
    print("width is ")
    print(width)
    
    print("heigh is ")
    print(heigh)
    
    print("area is ")  
    print(area) 

    >>>value1 = 2

    >>>value2 = 3

    >>>sum = value1 + value2

    >>>type(value1)

    <type 'int'>

    >>>type(value2)

    <type 'int'>

    >>>type(sum)

    <type 'int'>

    说明, 整数和整数的运算结果是整数。

    >>>value1 = 2.2

    >>>value2 = 3.3

    >>>sum = value1 + value2

    >>>type(value1)

    <type 'float'>

    >>>type(value2)

    <type 'float'>

    >>>type(sum)

    <type 'float'>

    说明,浮点数和浮点数的运算结果是浮点数

    >>>value1 = 2

    >>>value2 = 3.3

    >>>sum = value1 + value2

    >>>type(value1)

    <type 'int'>

    >>>type(value2)

    <type 'float'>

    >>>type(sum)

    <type 'float'>

    说明,整数和浮点数的运算结果是浮点数。整数类型转化为浮点数类型。

    在python中字符串是使用双引号或者单引号括起来的内容。

     >>>print("welcome to python world")

    >>>print('welcome to python world')

    >>>print("welcome to 'python' world")

    >>>print('welcome to "python" world')

    >>>print('welcome to 'python' world')

    SyntaxError,语法错误

    >>>print("welcome to "python" world")

    SyntaxError,语法错误

    说明,双引号“”中可以嵌套单引号‘’反之也可以。但是双引号中不能嵌套双引号,单引号中不能嵌套单引号。

    (补20130603)

    20130606关于图1所示内存中数据的补充说明:

    操作系统以字节为单位对Memory(内存)进行编址管理,也就是说每个字节给予一个唯一的值。

    以32位计算机(可以理解为CPU一次最多可以处理32个二进制位)为例,内存地址编址范围为0-2的32次方。

    我们就可以使用类似图1的方式形象的表示内存。

    在计算机中8个二进制位为1个字节(byte)。字节是存储管理的最小单位。关于数据存储单位说明如下:

    1 byte = 8 bit (一个二进制位称为1bit)

    1K = 1024 byte (2的10次方 byte)

    1M = 1024 K (2的20次方 byte)

    1G = 1024 M (2的30次方 byte)

    1T = 1024 G (2的40次方byte)

    这也说明了为什么我们的32位操作系统只能使用4G内存的原因。

    图来源于维基百科

     http://zh.wikipedia.org/zh-cn/%E5%AD%97%E8%8A%82

  • 相关阅读:
    jQuery选择器大全
    MVC自定义数据验证(两个时间的比较)
    SQLServer开发总结
    疯狂的订餐系统软件需求分析挑战之旅1
    疯狂的订餐系统软件需求分析挑战之旅2
    net中C#自动化调用Word的实例总结
    软件开发中代码自动化的一点浅见
    代码自动化(1)开篇
    代码自动化(2)程序设计
    Excel 手机号码、身份证 等信息 导入到SQL2005 中,转换成字符是自动变成 科学计数法 的解决方法
  • 原文地址:https://www.cnblogs.com/mengfanhao/p/3116800.html
Copyright © 2020-2023  润新知