@Channelchan
2018-09-29T15:17:23.000000Z
字数 1684
阅读 78110
交互式:
命令逐条运行
交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。
print("hello world")
hello world
>>> 1+1
2
>>> a=1
>>> b=1
>>> a+b
2
脚本式:
一次性全部运行
通过脚本参数调用解释器开始执行脚本,直到脚本执行完毕。当脚本执行完成后,解释器不再有效。
my_dream='I want to be a quantitative trader.'
print('Hello,world!!! '+my_dream)
print (my_dream + ' What can I do?')
#点击run,显示运行结果:
Hello,world!!! I want to be a quantitative trader.
I want to be a quantitative trader. What can I do?
在 Python 里,标识符由字母、数字、下划线组成。
在 Python 中,所有标识符可以包括英文、数字以及下划线_,但不能以数字开头。
Python 中的标识符是区分大小写的。
Python 可以同一行显示多条语句,方法是用分号 ;
1A_ = 1
print(1A_)
File "<ipython-input-8-837b4d511801>", line 1
1A_ = 1
^
SyntaxError: invalid syntax
A1_ = 1
print(A1_)
1
a1_ = 2
print(a1_)
2
A = 1; a=2
print(A,a)
1 2
下面的列表显示了在Python中的保留字。这些保留字不能用作常数或变数,或任何其他标识符名称。
所有 Python 的关键字只包含小写字母。
and |
assert |
break |
class |
continue |
def |
del |
elif |
else |
except |
缩进替代({})
学习Python与其他语言最大的区别就是,Python的代码块不使用大括号({})来控制类,函数以及其他逻辑判断。
python最具特色的就是用缩进来写模块。
price = 1
if price>0:
print ('True')
print ('price is greater than 0')
# 没有严格缩进,在执行时会报错
else:
print ("False")
price = 1
if price>0:
print ('True')
print ('price is greater than 0')
else:
print ("False")
Python语句中一般以新行作为语句的结束符。
但是我们可以使用斜杠( \)将一行的语句分为多行显示,如下所示:
Price = 1+\
2+\
3
print(Price)
6
Python 可以使用引号( ' )、双引号( " )、三引号( ''' 或 """ ) 来表示字符串,引号的开始与结束必须的相同类型的。
其中三引号可以由多行组成,被当做注释。
Name = 'Channel'
Career = "Quantitative Trader"
Habit = '''Poppin'''
print(Name+" is "+Career+' who like '+Habit)
Channel is Quantitative Trader who like Poppin
def annotation():
"""
Channel is Quantitative
Trader who like Poppin
"""
annotated = """被注释"""
return annotated
print(annotation())
被注释
单行注释用 # 开头
多行注释用三个引号(''')
代码注释有一个功能在于可以在写代码时跳过不想运行的行
# 宽客你好
# print('hello quant')
# 宽客你好
print('hello quant')
hello quant
print('hello quant') # 宽客你好
hello quant
"""
宽客你好
print('hello quant')
"""
print("OK, Let's code")
OK, Let's code