python基础教程 First Program in Python Data Types Variables Comments Expression in Python String

  • Clear Screen

    1
    2
    3
    imoprt os
    clear = lambda: os.system('cls')
    clear()
  • Keywords

    1
    2
    import keyword
    keyword.kwlist

First Program in Python

1
print("welcome")

Data Types

1
2
3
4
5
type(2)
type(2.2)
type(2000000)
type(True)
type('a')

Variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
number = 2
real = 2.2
word = "word"
pint(word)
type(word)
a = b = c = 1.5
print(a)
print(b)
print(c)
one, two, three = 1, 'two', 'three'
print(one)
print(two)
print(three)
number = 1
str = 'string'
number = str

Comments

  • Single-line

    1
  • Multi-line

    1
    2
    3
    '''
    多行注释
    '''

Expression in Python

1
2
3
4
print(2.0+5)
print(10.0-5)
print(40*3*0.5)
print(2**3) # 乘方

String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
string = '012345'
print(string[2:5])
print(string[:5])
print(string[3])
string2 = ('con' + 'sole') * 2
print(string2)
i = "I"
can = "can"
print(i + can);
word = "Ford"
word = "L" + word[1:]
print(word)
word([2]) = "f" # 会报错,字符串不允许修改
# 这种写法表示内部的字符不转义
print(r'c:numbernan')
# 如果这里没有会多一个空行
print('''
hello:
world
world
''');
print("今天星期{0},天气{1}".format(3,"晴天"))
print('prices:({x}, {y}, {z})'.format(x = 2.0, y = 1.5, z = 5))
print("the {vehicle} had {0} crashes in {1} months".format(5,6,vehicle = 'car'))
print('{:<20}'.format("text"))
print('{:>20}'.format("text"))
print('{:b}'.format(21)) # 转二进制
print('{:x}'.format(21)) # 转八进制
print('{:o}'.format(21)) # 转十六进制

(未完待续)