【Python】python基础语法 编码

  • 编码

默认情况下,python以UTF-8编码,所有的字符串都是Unicode字符串,可以为代码定义不同的的编码。

#coding:UTF-8
#OR
#-*- coding:UTF-8 -*-
  •  python保留字

保留字及为关键字,不能作为任何标识符名称。查看当前版本所有关键字:keyword模块

1 import keyword    #导入keyword模块
2 keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'la
  • 注释

单行注释:代码中以#开头的行即为注释,程序在执行时不会执行该行

多行注释:"""多行注释"""   or   '''多行注释''

#这是一个注释


'''
这是第二个注释
'''

"""
这是第3个注释
"""
  • 数据类型

python中有数据类型:布尔型、整型、长整型、浮点型、字符串、列表、元组、字典、日期

  1. 布尔型(bool): True / False,任何数值的0,空字符串,空列表[],空元组(),空字典{}都是False,函数或方法返回值为空的同为False,其他数据都为True。
In [4]: bool(True)
Out[4]: True

In [5]: bool(False)
Out[5]: False

In [6]: bool(0)
Out[6]: False

In [8]: bool([])       #空列表 
Out[8]: False

In [9]: bool(())       #空元组
Out[9]: False

In [10]: bool({})   #空字典
Out[10]: False

In [12]: bool(1)
Out[12]: True

.................
  1. 整数(int):范围-2**31 到2**31-1,超出这个范围的为长整型
  2. 字符串(str):被引号括起来的都是字符串
#type():查看数据类型
In [13]: type("hello world")
Out[13]: str

In [14]: type("123")
Out[14]: str
  • print()输出

标准输出,print()默认输出是换行的,如果要实现不换行,需在末尾加上 end=""

>>>print("hello world!",end="","hello python!")       #不换行输出
>>>hello world! hello python!
posted @ 2017-09-28 14:55  杨浪  阅读(356)  评论(0编辑  收藏  举报