python

Python | Python笔记(基础)

2019-09-25  本文已影响0人  琼脂糖

python基础,编辑器配置,函数,控制流。

1. 基础

变量命名

string:triple quotes 多行文本。格式文本 大括号

first = "Mosh"
last = "Hamedani"
full = f"{first} {last}"
print(full)

得到结果$Mosh Hamedani

string methods

course = "  python Programming"
print(course.upper())
print(course.lower())
print(course.title())
print(course.rstrip())
print(course.find("Pro"))
print(course.replace("p", "j")
print("pro" in course)
print("swift" not in course)

falsy elements

""
0
None

2. control flow

**比较string **

"bag" > "apple" #True
"bag" == "BAG" #False
ord("b") #98
ord("B") #66

if :注意缩进和冒号

temperature = 15
if temperature > 30:
    print("It's warm")
    print("Drink water")
elif temperature > 20:
    print("It's nice")
else:
    print("It's cold")
print("Done")

ternary operator
message = "Eligible" if age ≥ 18 else "Not eligible

logical
and or not
short-circuit evaluation:就像电路,一旦遇到决定最终true or false的就停止了

if high_income and good_credit and not studuent:
    print("Eligible")

chaining comparison

if 18 <= age < 65:
    print("Eligible")

for循环

for number in range(1, 10, 2):
    print("Attempt", number, number*2)

for else
如果for循环没有提前出来,则执行else。如果提前出来了,不执行else。

for number in range(3):
    print("Attempt")
    if successful:
        print("Successful")
        break
else:
    print("Attempted 3 times and failed")

可迭代对象
包括:range(5),string,list, tuple

3. function

两类函数如下:

写函数的时候:默认参数要在required参数后面。

*args将传入所有数字打包成一个元祖

def multiply(*number):
    for number in numbers:
        print(number)
        
multiply(2, 3, 4, 5)

结果如下

2
3
4
5

**args:传入key-value pairs被打包成一个字典

def save_user(**user):
    print(user["name"])
    
    save_user(id=1, name="John", age=22)

全局变量尽量避免,如果实在需要一个全局变量,也不要在函数内部修改它。

message = "a"

def greet(name):
    global message
    message = "b"
    
greet("Mosh")
print(message)

无需else和elif

def fizz_buzz(input):
    if (input % 3 == 0) and (input % 5 == 0):
        return "FizzBuzz"
    if input % 3 == 0:
        return "Fizz"
    if input % 5 == 0:
        return "Buzz"
    return input
    
print(fizz_buzz(7))

4.ipython

image
参考

5. 编辑器

  1. editor :VScode sublime
    IDE(集成开发环境):pycham

  2. 通过安装扩展将VScode转换成python IDE
    Python扩展:安装后重启。显示pylint没有安装成功,(用于预判错误),左下角更改python坏境到3,然后安装lint

  3. linting:shift+cmd+M 查看问题

  4. cmd pallete:shift+cmd+P

  5. PEPs:python enhancement proposals
    PEP8:编码风格指南
    扩展:cmd+shfit+p 搜索format document;安装formatter autopep8
    如何在保存的自动修改:format on save check

  6. code runner插件:黄色的run
    ctrl+alt+N
    如果是run 默认的python2,设置:code-runner.executorMap
    右侧的user settings:最后加上逗号 “code-runner.executorMap" 将全部线管设置复制到user setting,修改python列将python改为python3.7

    image
  7. alias python='python3.7' 加在~/.bash_profile

  8. bash打开vscode code .

  9. debug :vscode左侧的虫子图标。第一次debug需要点击齿轮,生成一个launch.json
    添加break point:cmd+F9
    运行function到断点:F5
    一次一行:F10
    进入函数内部:F11
    跳出函数:shift+F11

  10. mac tricks:Fn+箭头 到行末
    alt+箭头:移动

6.补充

  1. print 重复调用 ,会在最后输出一个none。比如函数内部已经print,调用时再次print函数结果。
  2. 一个项目最好建立一个环境,pipenv来安装依赖包。这样在新的电脑运行的时候,可以迅速安装所需要的包。
  3. kaggle:home for data science
上一篇下一篇

猜你喜欢

热点阅读