python 基础语法

2017-11-30  本文已影响0人  脏脏包盛

编码

指定文件编码

# -*- coding: cp-1252 -*-

标识符

python保留字

import keyword
keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

注释

'#' 开头的行注释,或者三个单引号或双引号的块注释

# 注释
‘’‘
注释
’‘’
“”“
注释
”“”

行缩进

缩进表示代码块,同一层次代码缩进相同

多行语句

使用反斜杠实现

string = item_one + \
        item_two + \
        item_three

在[],{},()中的多行语句不需要用反斜杠

数据类型

字符串

空行

用于函数之间或类的方法之间分隔

用户输入字符

input("\n\n按下 enter 退出")

同一行显示多条语句

用分号隔开

print 输出

print 默认输出是换行的。 如果不换行可以在末尾加上end=""

x = "a"
 print(x,end=" ")
print (x)

import 与from import

导入模块

import sys
from numpy import *
上一篇 下一篇

猜你喜欢

热点阅读