• Python 30分钟入门——数据类型 & 控制结构


    Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候。不得不说的是Python的官方文档相当齐全,假设你是在Windows上学习Python,安装包自带的“Python Manuals”就是一份非常好的学习资料(基本上不用去找其它资料了);尤其是当中的Tutorial,非常适合刚開始学习的人。本文一方面总结了python语言的核心——数据类型和控制结构;还有一方面,通过与其它语言的对照表达了我对Python的一些拙见。


    数据类型

    Python简洁的背后是由于有着强大的类型系统的支持。Python世界的基本数据类型有{int, long, float, complex, str, list, set, tuple, dict},以下通过Python解释器在交互模式下的输入输出实例进行演示(当中有前导符>>>或...的是输入):
    tips: Python世界里,能够用type(x)来输出x的类型.

    int, long, float, str, complex

    >>> type(123)
    <type 'int'>
    >>> type(-234)
    <type 'int'>
    >>> type(123456123456)
    <type 'long'>
    >>> type(-123456123456)
    <type 'long'>
    >>> type(123.456)
    <type 'float'>
    >>> type('abc')
    <type 'str'>
    >>> type("hello, world")
    <type 'str'>
    从最后两次输入能够看到Python的字符串能够用单引號也能够用双引號。另外,大家可能会疑惑的是究竟多大是int和多大是long呢?以下我们来一探究竟:
    >>> type(123456)
    <type 'int'>
    >>> type(123456789)
    <type 'int'>
    >>> type(1234567890)
    <type 'int'>
    >>> type(12345678901)
    <type 'long'>
    能够看到1234567890还是int,12345678901就是long了,说明int是有范围的。记得C/C++的int长度(4B)的同学都知道,C/C++里int的取值范围是:[-2^31, 2^31-1]也就是[-2147483648, 2147483647]。据此,我们能够看看Python的int范围:
    >>> type(2147483647)
    <type 'int'>
    >>> type(2147483648)
    <type 'long'>
    >>> type(-2147483648)
    <type 'int'>
    >>> type(-2147483649)
    <type 'long'>
    这次试验说明,Python的int范围和C/C++一样。
    那么,假设我想指定一个比較小的long怎么办呢?能够通过加L(或小写l)后缀:
    >>> type(1L)
    <type 'long'>
    >>> type(2l)
    <type 'long'>
    另外,Python的浮点数是没有double的:
    >>> type(123.456)
    <type 'float'>
    >>> type(123456123456.123456123456123456123456)
    <type 'float'>

    complex(复数)
    复数类型在非常多语言中是没有的,Python通过整数加J(或小写j)后缀表示复数:
    >>> type(3+4j)
    <type 'complex'>
    >>> type(3+4J)
    <type 'complex'>
    >>> type(4j)
    <type 'complex'>
    >>> type(j)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'j' is not defined
    >>> type(1j)
    <type 'complex'>
    可是1j不同意直接写成j,j会被当做name查找,假设没找到就会报错。

    tuple, list, set, dict

    tuple, list, set, dict各自是元组、列表、集合、字典(有的语言叫映射map)。这些类型才是Python类型系统的过人之处,在多数编译型语言(C、C++、Java、C#等)中,这些类型都要通过库来提供(如C++、Java、C#),有的也许库也没有提供(如C)。
    >>> type([1, 2, 3])
    <type 'list'>
    >>> type({2, 3, 4})
    <type 'set'>
    >>> type((3, 4, 5))
    <type 'tuple'>
    >>> type({'key1': 'value1', 'key2': 'value2'})
    <type 'dict'>
    能够看到(), [], {}和它括起来的一系列元素,各自是表示:元组、列表、集合。而dict则是{key1: value1, [key2: value2, ...]}的形式。
    上面列出的各种集合的元素类型一致,这在编译型语言里一般是必须的,但在Python里不必:
    >>> (1, 'two', 3.0)
    (1, 'two', 3.0)
    >>> [(1, 'two', 3.0), '4', 5]
    [(1, 'two', 3.0), '4', 5]
    >>> {1, 2L, 3.0, 4j}
    set([1, 2L, 3.0, 4j])
    >>> {1: 'one', 'one': 1}
    {1: 'one', 'one': 1}

    控制结构

    结构化程序设计方法提出之时,就有前辈证明了不论什么算法都能够使用:顺序、选择、循环三种结构表达。以下将展示Python的基本的语法,以及选择和循环。

    顺序结构

    顺序结构本身没什么好说的,这里介绍一下Python的其它特性。

    语句

    Python的语句以换行符结尾(不像C家族的分号结尾):
    >>> print "hello, world"
    hello, world
    而且Python程序没有所谓的“入口”,这和多数脚本语言类似。

    弱类型

    Python是弱类型的,也就是变量的类型不是一成不变的。这也和非常多脚本语言类似。
    >>> a = 123
    >>> b = "asdf"
    >>> c = [3, 4, 5]
    >>> a
    123
    >>> b
    'asdf'
    >>> c
    [3, 4, 5]
    >>> a = b
    >>> b
    'asdf'
    
    这段交互中有两点与C语言(C++等)不同:
    1. 使用变量前不用向提前声明变量的类型
    2. 一个变量初始化为一个类型后还能给他赋其它类型的值
    提示:在Python解释器的交互模式下直接输入变量名也能显示变量的值
    “变量”一词在Python里应该叫“名字”(或者符号)更确切,在Python中你能够给一个符号赋予不论什么类型的值。稍后你将会看到能够给原本赋值为int的对象赋值为一个函数,一个类。

    函数

    Python的函数定义以def開始,假设有返回值须要用return传递返回值。
    比方例如以下代码定义了一个名为sayHello的函数,并用'Jack'为參数进行了一次调用:
    def sayHello(name):
    	print 'Hello, ' + name + '!'
    	
    sayHello('Jack')
    这段代码的执行结果为:Hello, Jack!
    (可将这段代码保存为sayHello.py,然后在相应文件夹执行python sayHello.py)

    Python的类定义以class開始,属性能够在class下方声明也能够在__init__方法里通过self.xxx隐式声明。
    先来一段最简单的关于类的代码:
    class Man:	
    	def __init__(self, name):
    		self.name = name
    	
    	def hello(self):
    		print 'Hello, ' + self.name + '!'
    
    m = Man('Jack')
    m.hello()
    这段代码也会输出:Hello, Jack!
    tips: Python方法的第一个參数必须是名为self的參数,用于操作对象的成员。__init__类似其它语言的”构造方法“。
    类的很多其它特性和OOP有关,以后有时间再单独发一篇博文展示。

    顺便看看函数、类以及类的实例在Python解释器眼中都是什么:
    >>> type(sayHello)
    <type 'function'>
    >>> type(Man)
    <type 'classobj'>
    >>> type(m)
    <type 'instance'>
    >>> type(m.hello)
    <type 'instancemethod'>
    >>> type(Man.hello)
    <type 'instancemethod'>
    能够想象,Python世界里的东西都是”灰色“的,解释器对它们”一视同仁“,从来不以貌取人,仅仅看他们如今身上的标签是什么~

    选择结构

    Python的选择结构以if開始。

    bool

    if必定要涉及bool值,Python bool的取值为True和False:
    >>> type(1==1)
    <type 'bool'>
    >>> type(True)
    <type 'bool'>
    >>> type(False)
    <type 'bool'>
    (上面好像忘了列出bool类型)

    对于Number(int, long, float, complex),0在if条件上也是False:
    >>> if 1:
    ...     print "true"
    ...
    true
    >>> if 0:
    ...     print "true"
    ... else:
    ...     print "false"
    ...
    false
    >>> if 0.0:
    ...     print "0.0 is true"
    ...
    >>> if 0j:
    ...     print "0j is true"
    ...
    提示:Python是以代码缩进区分代码块的
    除此之外,空的string和空的集合(tuple, list, set)也是False:
    >>> if '':
    ...     print 'null string is true'
    ...
    >>> if ():
    ...     print 'null tuple is true'
    ...
    >>> if []:
    ...     print 'null list is true'
    ...
    >>> if {}:
    ...     print 'null set is true'
    ...

    if, if-else & if-elif-else

    上面几个if演示样例多是仅仅有一个分支的,当然Python也支持多个分支的if:
    >>> x = int(raw_input("Please enter an integer: "))
    Please enter an integer: 42
    >>> if x < 0:
    ...      x = 0
    ...      print 'Negative changed to zero'
    ... elif x == 0:
    ...      print 'Zero'
    ... elif x == 1:
    ...      print 'Single'
    ... else:
    ...      print 'More'
    ...
    More
    

    循环结构

    Python的循环有for和while两种,没有do-while,也没有loop-until。

    for

    Python的for循环和C的不同,它更像C++,Java里的新式for循环:
    >>> a = [1, 'two', 3.0]
    >>> for i in a:
    ...     print i
    ...
    1
    two
    3.0
    这样的for迭代集合非常方便。

    可是要想像典型C语言的for循环那样迭代一个整数区间怎么办?别怕,Python提供了内置(built-in)函数range(),它能返回整数区间列表,供你迭代,用起来也非常方便:
    >>> for i in range(1, 6):
    ...     print i
    ...
    1
    2
    3
    4
    5
    >>> for i in range(10, 65, 10):
    ...     print i
    ...
    10
    20
    30
    40
    50
    60
    这里展示了range的两种调用形式,一种是range(a, b),它将返回一个从a(包括a)到b(不包括)的整数列表(list),还有一种range(a, b, s),将返回一个a~b,以s为步长的list:
    >>> range(1, 6)
    [1, 2, 3, 4, 5]
    >>> range(10, 65, 10)
    [10, 20, 30, 40, 50, 60]

    while

    Python的while循环和C的while差点儿相同:
    >>> i = 1
    >>>
    >>> while i < 5:
    ...     i = i+1
    ...     print i
    ...
    2
    3
    4
    5

    顺便一提,Python里 i=i+1 不能写成i++,Python不支持这样的语法;但能够写成 i += 1:
    >>> i
    5
    >>> i += 1
    >>> i
    6
    >>> i++
      File "<stdin>", line 1
        i++
          ^
    SyntaxError: invalid syntax
    >>> ++i
    6
    >>> i
    6
    各位可能会疑惑,为什么++i能够?由于pyhon支持前置的+(正负号)运算,++被当做两次正运算了;同理,+++i,++++i都是一样的;我们能够顺便測一下负号运算:
    >>> i
    6
    >>> +++i
    6
    >>> ++++i
    6
    >>> -i
    -6
    >>> --i
    6
    >>> ---i
    -6
    和想象的结果一致,Great!

    输入输出(IO)

    当你想动手写点”更有意思“的小程序的时候,你会发现除了三大基本控制结构和数据类型之外,你最须要的可能就是IO功能。
    Q: 输出能够用print,那么输入呢?是input吗?
    A:input能够,但很多其它时候须要用的可能是raw_input和readline这两个built-in function,但readline仅适用于Unix平台.
    Q:那么input和raw_input有什么差别呢?
    A:来看看我和解释器的以下这段交互,看看你能不能自己发现它们的不同。
    >>> varA = raw_input('please input:')
    please input:Life is too short, you need Python!
    >>> varA
    'Life is too short, you need Python!'
    >>> type(raw_input('input something:'))
    input something:asdf
    <type 'str'>
    >>> type(raw_input('input something:'))
    input something:123
    <type 'str'>
    
    A:你看到了,raw_input不论你输入什么都会返回str类型,这也是为什么叫做raw_input的原因。
    A: 继续往下,你会看到input:
    >>> type(input('input sth:'))
    input sth:123
    <type 'int'>
    >>> type(input('input sth:'))
    input sth:asdf
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'asdf' is not defined
    >>> type(input('input sth:'))
    input sth:varA
    <type 'str'>
    >>> input('sth:')
    sth:varA
    'Life is too short, you need Python!'
    >>> input('try some input like your code:')
    try some input like your code:[1, 'two', 3.0]
    [1, 'two', 3.0]
    >>> input('try again:')
    try again:'Oh!!!'
    'Oh!!!'
    Q: Oh, I know! input会按代码的形式解释输入!
    A: 嗯,你提到了”解释“,看来你已经悟出了Python的真谛,你能够下山了!
    Q: Master, 这样就能够了么?我知道的好像太少了吧?
    A: 为师这里有一本《Python秘籍》,你拿去吧,全部问题你都能找到答案,但答案未必在这本书里
    (徒儿A就此辞别师父Q,開始了他的Python之旅)

    后记(我的拙见)

    Python是解释型语言,它的类型系统很强大!作为一个学了几门C家族语言(C、C++、Java、C#)的我来说,tuple,list,set,dict都是内置类型,简直是太美好了!
    在我看来:
    1.因为有语言级的tuple, list, set, dict,Python很适合用来写算法,并且Python写出的算法必定要比其它语言简洁得多!
    2.Python的语法简洁易懂,默认提供了文件管理等功能,能够替代多数其它脚本的工作。
    3.Python的弱类型(约束少)以及解释器交互模式的趣味性和便捷性,很适合作为”第一门程序设计语言“,教授少年儿童。
  • 相关阅读:
    notepad++的下载与安装
    Redis和RedisDesktopManager的下载与安装
    Jdk的下载与安装
    JavaBean中对象的复制:BeanUtils和Dozer
    JAXB:java对象和xml之间转换
    mysql存储过程
    mysql索引优化
    索引优化案例
    存储优化:MyISAM和Innodb区别
    索引优化:如何避免索引失效?
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4509604.html
Copyright © 2020-2023  润新知