Python学习笔记一

数据类型:

  • Python字符串很灵活,不像C语言有很多限制,做个简单的字符串函数也需要很多代码,举个简单的例子:
  • >>>hacker = “hacker”
    >>>”hack” in myjob
    True

    很简单就可以测试某个字符串中是否包含另一个字符串。

  • 而分隔字符串就更简单了:(图片是分隔的字符串的解释)
  • >>>s = “sliceofspam”
    >>> s[-4:]
    ‘spam’
    >>> s[7:]
    ‘spam’
    >>> s[7:11]
    ‘spam’

    python_string

  • 字符串是不可变的数据类型,不可以使用下面的赋值:
  • >>> s = “cocobear”
    >>> s[0] = s

  • 字符串与列表的相互连接:
  • >>> `[1, 2]` + “34″ # same as “[1, 2]” + “34″
    ‘[1, 2]34′
    >>> [1, 2] + list(“34″) # same as [1, 2] + ["3", "4"]
    [1, 2, '3', '4']

This entry was posted in Python, 编程相关. Bookmark the permalink.

6 Responses to Python学习笔记一

  1. luguo says:

    Python的list是很有用的~~
    p.s. 你那是用的Learning Python里的图片?

  2. cocobear says:

    嗯,就是那里面的图片,被我加了个淡蓝色的框:-)

  3. dream says:

    我有个Python short reference card, 要不要? :)

  4. Amankwah says:

    这个好,娃好好学python,到时候交我,哈哈

  5. cocobear says:

    TO:dream
    是电子书吗?如果不太大的话给我发一份,呵呵。

  6. kongove says:

    ”hack” in myjob
    应该为
    ”hack” in hacker

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>