从代码中学Python语法一(持续更新)

# -*- coding:utf-8 -*-
__author__ = 'hunterhug'
print("你好")
#打印

hello = "This 'is' \"a rather long string containing\n\
    several lines of text just as you would do in C.\n\
        Note            that whitespace at the beginning of the line is\n\
     significant."

print(hello)
#字符串转义

print("""
    Usage: thingy \n[OPTIONS]
         -h                        Display this usage message
         -H hostname               Hostname to connect to
    """)
#不需要行属转义

print(r"""
    Usage: thingy [OPTIONS]\n
         -h                        Display this usage message
         -H hostname               Hostname to connect to
    """)
#生成原始字符串

word='3'+"5"
word1='str' 'ing' #两个字符串可以
word2='str   '.strip()+ 'ing' #字符串表达式不可以
print(word*3)
print(word1)
print(word2)

#Python 字符串不可变,按0索引,最后一个不拿
word3='123456789'
print(word3[:])
print(word3[1:])
print(word3[:1])
#123456789
#23456789
#1
print(word3[:2])
print(word3[2:])
#12
#3456789
print(word3[2:4])
print(word3[2:2])
#34
#
print(word3[2:100])#下边界大时默认长度为字符长度
print(word3[50:200])#
print(word3[6:5])#
#倒数
print(word3[-1:])#9
print(word3[:-2])#1234567
print(word3[-2:-3])#
print(word3[-3:-1])#78
print(word3[-100:])#123456789被截断

#+---+---+---+---+---+---+---+---+---+
# | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
# +---+---+---+---+---+---+---+---+---+
# 0   1   2   3   4   5   6   7   8   9
#-9  -8  -7  -6  -5  -4  -3  -2  -1

print(word3[-7:-5])
print(len(word3))

# Unicode 的先进之处在于为每一种现代或古代使用的文字系统中出现的每一个字符都提供了统一的序列号。
# 之前,文字系统中的字符只能有 256 种可能的顺序。通过代码页分界映射。
# 文本绑定到映射文字系统的代码页。这在软件国际化的时候尤其麻烦 (
# 通常写作 i18n —— 'i' + 18 个字符 + 'n' )。
# Unicode 解决了为所有的文字系统设置一个独立代码页的难题。
print('Hello\u0020World !')
china="你好hi".encode('utf-8')
print(china)
bchina=china.decode('utf-8')
print(bchina)

#列表
list=['spam', 'eggs', 100, 1234]
print(list[:])#切片操作返回新的列表,切片与字符串相似
list[1]='2'
print(list[:])#可变['spam', '2', 100, 1234]
list[0:3]=[]
print(list)#清空
#插入
list=['spam', 'eggs', 100, 1234]
list[2:2]=[1,1] #插入
print(list)
print(list.__len__())
print(len(list))
list[3]=list#列表中的列表
print(list)
print(list[3][3])
print(list[3][3][3])#循环咯

#
a, b = 0, 1
while b < 10:
    #print(b)
    print(b,end=",")#禁止换行
    a, b = b, a+b
    # 任何非零整数都是 true;0 是 false 条件也可以是字符串或列表,
    # 实际上可以是任何序列;所有长度不为零的是 true ,空序列是 false。
    # 示例中的测试是一个简单的比较。标准比较操作符与 C 相同: < (小于), > (大于), == (等于),
    #  <= (小于等于), >= (大于等于)和 != (不等于)。
    
#接下来 http://docs.pythontab.com/python/python3.4/controlflow.html

 

你可能感兴趣的:(从代码中学Python语法一(持续更新))