python3学习——基础1

编码:
源码UTF-8编码,字符串为unicode字符串
标识符
第一个字符必须是字母表中字母或下划线 _
标识符的其他的部分由字幕、数字和下划线组成。
标识符对大小写敏感
在python3中,中文也可以作为变量名,非ascii标识符也是可以的
python保留字

>>>import keyword
>>>keyword.kwlist

可以输出版本的所有关键字ep:3.6.3
[‘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’
]

注释
单行多行注释 #
在pycharm中可以用 ctrl+/ 来快速给多行添加#
另外可以用””” “”” ‘’’ ‘’’
#第一个注释
#第二个注释

‘’’
第三注释
第四注释
‘’’

“””
第五注释
第六注释
“””
行与缩进
python最具特色的就是使用缩进来表示代码块,不需要使用大括号 {} 。
缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。实例如下:

if True:
    print ("True")
else:
    print ("False")

缩进空格不一致会运行错误:

if True:
	print('answer')
	print("True")
else:
	print('answer')
  print("false")

报错提示如下
IndentationError: unindent does not match any outer indentation level

多行语句
可以用” \ “来进行换行

total = i_one +\
		i_two

等价于

total = i_one + i_two

但是在{}中的多行语句不需要使用\

total = ['item_one', 'item_two', 'item_three',
        'item_four', 'item_five']

数据类型
数字(number)类型
python中有四种数字类型:整数,布尔型,浮点数,复数
int 表示为长整型
bool ep:true
complex (复数): 1+ 2j , 1.1+2.2J

字符串(string)
单引号双引号无差别
三引号’’’ ‘’’可以指定多行字符串
转义符号
r可以让反斜杠不发生转义 r”this is a line with\n”
按字面意思连接字符 “this” “is” “python”
用 + 运算符连接 用 * 运算符重复
字符串不可以改变
无单独字符类型,每个字符就是字符长度为一的字符串
字符串的截取语法如下:变量[头下标:尾下标:步长]

word = '字符串'
sentence = "这是一个句子。"
paragraph = """这是一个段落,
可以由多行组成"""

实例

str = “air_fly”
print(str)        #输出字符串
print(str[0:-1])  #输出第一个到倒数第二个的所有
printstr[0]#输出第一个字符
print(str[2:5])   #输出第三个到第五个字符
printstr[2:]#输出第三个之后的所有字符
printstr*2#输出str字符串两次
printstr+ 'hello'#连接字符串
print'hello\nfly'#转义
print(r'hello\nfly')#不转义

r == raw 即为raw string

空行
空行对于日后维护非常重要

等待用户输入

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

显示多条语句
用分号

import sys;x="qqqq"

多个语句构成代码组
缩进相同的一组语句构成一个代码块,我们称之为代码组。
像if,whlie,del,或者class的符合语句,首行以关键字开始,一冒号(:)结束,改行之后的一行或者多行代码构成代码组,首行及之后的代码组成为一个子句(clause)

if expression :
	suite
elif epression :
	suite
else:
	suite

print输出
默认换行输出,如果不要,在结尾加上end=””

print(x,end="")

import 和 from … import
用import或者from…import 来导入相应的模块
引入整个模块,格式为 import somemodule
从摸个模块中引入某个函数 from somemodule import somefunction
多个函数from somemodule import somefunction,secondfunction,thirdfunction
将某个模块的全部函数导入 import somemodule import*

import sys
print('==========Python import module=============')
print ('命令行参数:')
for i in sys.argv:
	print(i)
print('\n python is goood')

命令行参数
很多程序可以执行一些操作来查看一些基本信息,Python可以使用-h参数查看各参数帮助信息:

$ python -h
usage: python [option] … [-c cmd | -m mod | file | -] [arg] …
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser (also PYTHONDEBUG=x)
-E : ignore environment variables (such as PYTHONPATH)
-h : print this help message and exit

[ etc. ]