python 基本语法
多行语句
Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句,例如:
total = item_one + \
item_two + \
item_three
在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(),例如:
total = ['item_one', 'item_two', 'item_three',
'item_four', 'item_five']
同一行多条语句
Python可以在同一行中使用多条语句,语句之间使用分号(;)分割,以下是一个简单的实例:
import sys; x = 'runoob'; sys.stdout.write(x + '\n')
run code--------------------------------------------
runoob
7 #此处7表示字符数
if 语句结构
#第一种,只有if
if expression:
pass
-----------------
#第二种,if else
if expression:
pass
else:
pass
-----------------
#第三种,if elif else
if expression:
pass
elif expression:
pass
else:
pass
for 语句结构
for target_list in expression_list:
pass
while语句结构
# 第一种,while
while expression:
pass
-------------------
# 第二种 while,else
while expression:
pass
else:
pass
print输出
Print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":
x="a"
y="b"
# 换行输出
print( x )
print( y )
# 不换行输出
print( x, end=" " )
print( y, end=" " )
run code-------------------
a
b
a b
import 与 from import
在 python 用 import 或者 from...import 来导入相应的模块。
1.将 整个模块 (somemodule)导入,格式为:
import somemodule
2.从某个模块中导入 某个函数 ,格式为:
from somemodule import somefunction
3.从某个模块中导入 多个函数 ,格式为:
from somemodule import firstfunc, secondfunc, thirdfunc
4.将某个模块中的 全部函数 导入,格式为:
from somemodule import *

浙公网安备 33010602011771号