推荐直接安装Anaconda和Pycharm。
2 基本使用 2.1 print功能在Pycharm中Python Console练习:
print(1)
1
print('we are going to do sth')
we are going to do sth
print("we are going to do sth")
we are going to do sth
print("I'm Li Yuxin")
I'm Li Yuxin
print('I\'m Li Yuxin')
I'm Li Yuxin
print('apple'+'pen')
applepen
print('iphone'+'8')
iphone8
print('iphone'+str(8))
iphone8
print(1+2)
3
print('1+2')
1+2
print(int('1')+2)
3
print(float('1.2')+2)
3.2
2.2 基础数学运算
在Pycharm中Python Console练习:
1+1
Out[2]: 2
3-2
Out[3]: 1
4*5
Out[4]: 20
2**4
Out[5]: 16
8%3
Out[6]: 2
9//4
Out[7]: 2
9//3
Out[8]: 3
8%2
Out[9]: 0
2.3 变量variable
新建Python文件,输入如下代码:
apple = 10
apple_egg = 20
APPLE = 30
APPLE_EGG = 40
appleEgg = 50
x = 5 + 7
print(apple)
print(apple_egg, APPLE, APPLE_EGG, appleEgg)
print(x)
a, b, c = 1, 2, 3
print(a, b, c)
d = a*2
e = b*c
print(d, e)
得到
10
20 30 40 50
12
1 2 3
2 6
3 while和for循环
3.1 while循环
新建Python文件,输入如下代码:
condition = 1
while condition < 10:
print(condition)
condition = condition + 1
得到
1
2
3
4
5
6
7
8
9
Ps:当陷入死循环时,可按快捷键ctrl+c中止死循环
3.2 for循环 新建Python文件,输入如下代码:example_list = [2, 5, 8]
for i in example_list:
print(i)
print('inner of for')
print('outer of for')
得到
2
inner of for
5
inner of for
8
inner of for
outer of for
当出现如下情况(tab结构出现问题):
example_list = [2, 5, 8]
for i in example_list:
print(i)
print('inner of for')
print('outer of for')
全选要调整的两行,按shift+tab键进行调整。
输入for i in range(1, 3):
print(i)
得到
1
2
输入
for i in range(1, 6, 2):
print(i)
得到
1
3
5
4 if语句
4.1 if判断
新建Python文件,输入如下代码:
x = 1
y = 2
z = 3
k = 2
if x < y:
print('x is less than y')
if x < y < z:
print('x is less than y, and y is less than z')
if x y:
print('x is less than z, and z is greater than y')
if k <= y:
print('k is less or equal to y')
if k == y:
print('k is equal to y')
if x != y:
print('x is not equal to y')
得到
x is less than y
x is less than y, and y is less than z
x is less than z, and z is greater than y
k is less or equal to y
k is equal to y
x is not equal to y
4.2 if else判断
新建Python文件,输入如下代码:
x = 3
y = 2
if x < y:
print('x is less than y')
else:
print('x is greater or equal to y')
得到
x is greater or equal to y
4.3 if elif else判断
新建Python文件,输入如下代码:
x = 3
if x < 3:
print('x 3:
print('x > 3')
else:
print('x = 3')
得到
x = 3
输入
x = -2
if x > 1:
print('x > 1')
elif x < -1:
print('x < -1')
elif x < 1:
print('x < 1')
else:
print('x = 1')
print('finish running')
得到
x < -1
finish running