Python 基础学习 --- Note 01 --- 基础语法

非常尴尬有些部分用英文写的,现在就先这样吧,以后再改回来

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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值