非常尴尬有些部分用英文写的,现在就先这样吧,以后再改回来
1. Run .py
file (seems that can only be used in Jupyter Notebook)
%run
+ File Address
%run ./hello.py (hello.py 是相同文件夹下的另一个文件)
hello.py:
print('hello world')
# output: hello world
2. print
print('hello', end = ' ') # default '\n'
print('world!!!')
# output: hello world!!!
3. Format
Type1: "{}".format()
print("hello {}".format('saxon'))
# output: hello saxon
Type2: "%s" % ()
print("hello %s" % ('saxon'))
# output: hello saxon
4. For loop
for i in range(1,3): # left end included, right end excluded
print(i)
# output:
# 1
# 2
下面这个是我后来遇到的,觉得也挺典型就放在这里了
import numpy as np
arr1 = np.arange(10)
# arr1: [0, 1, 2...7, 8, 9]
# 把 arr1 数组中的元素分别 ×2 后放到 arr2 中
for _ in range(10):
arr2 = arr1 * 2
# arr2: [0, 2, 4...14, 16, 18]
# 因为这里只需要循环次数,不需要变量,所以用 '_'来表示没有变量就可以了
5. Input (Note: your input will be translated into string type)
a = input('please input something:')
print(type(a)) # string
# if you need an integer type
b = int(input('please input another number'))
print(type(b)) # int
6. Identifier
The first character must be a letter or an underline('`_`'). The other part can be `letter`, `number`, or `underline`
_a = 2
print(_a)
7. Python keyword
help('keywords')
#
# Here is a list of the Python keywords.
# Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
8. Comment
Type 1:
# comment for code inline
Type 2:
def add(a, b):
'''add two number
a, b : input number
return value : sum(a + b)'''
return a + b
help(add) # print function explanation for add(a, b)
9. Line concept
a = 1; b = 2 # Add one semicolon to split two sentence
print("{} {}".format(a, b))
a = 1 + 2 + 3\
+ 4
print(a)