python | Basics


Common global introduction

1.1 At the same time export multi-line results

In a code block, a plurality of results may be output at the same time

from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = 'all'

1.2 Code autocomplete

After running the following code in the output code Tabkey, can be obtained code completion tips

%config IPCompleter.greedy=True

1.3 Auto show image

After you run the code below, do not call plt.show, you can automatically show images

%matplotlib inline

1.4 ignore warnings

Version for ignoring warnings annoying, but sometimes ignore an exception in

import warnings
warnings.filterwarnings('ignore')

1.5 normal display Chinese and negative sign

And automatic image often associated with the show, remember to run before the introduction ofmatplotlib

plt.rcParams['font.sans-serif'] = ['Simhei']  # 正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False    # 正常显示负号


Get and set the path

Sometimes for convenience, will change the working path

import os 
os.getcwd()             # 获取当前工作路径
import os 
os.chdir('D:\\data')    # 以指定目录为当前工作路径
# 等价于os.chdir(r'D:\data'),r是变成原始字符,忽略转义字符


Upload file

Upload files to jupyter notebook there are two common ways:

  • Ipynb files into the working directory python
  • With uploaduploading files


Coding specification

In order to achieve greater readability, python writing code must follow some principles

  • Indent: Use spaces and Tabindent control
  • Continued line: to achieve continued line by line continuation character \ and parentheses (), () the most common
# 严格缩进关系
PM = eval(input("请输入PM2.5数值: "))  # input指创建一个输出框
if 0 <= PM < 35:
    print("空气优质,快去户外运动!")
if 35 <= PM < 75:
    print("空气良好,适度户外活动!")
if 75 <= PM:
    print("空气污染,请小心!")

# 续行符\
print('avnjdfldjflf\
fffffffffffffffffffff')

# 用()实现续行——最常用
PM, Temp = (eval(input('请输入PM2.5值')),
            eval(input('请输入气温值')))
if 0 <= PM < 35:
    print('空气优质')
if 35 <= PM < 75:
    print('空气良好')
if 75 <= PM:
    print('空气污染')


See Help and version number

When a function or method unclear, you can view the python built-in help information

  • View the version number
    • np.version(), Remember to import the numpy package
  • View Help
    • help
    • Behind the increase?
    • With shift + tabopen Helpful Hints
    s='中国'
    help(s.startswith)
    s.startswith?
    s.startswith     # shift + tab打开帮助提示


Functions and Methods

Functions and methods are significant differences in the way call

  • Function: function name (the object), is the whole object into them, such aslen(str)
  • Method: Object () method is invoked by forms "method.", Such asstr.find('x')
# 函数
a = 'sauhgsoidgu'
len(a)
 
# 方法
a.index('a')


cascade

Cascade can significantly increase the python code readability and maintainability

  • Definition: multiple calls at the same time called the cascade method, by running from left to right
  • Principle: a result of the method is a data type, then the backward call other methods may then direct the data type
# 一次一次调用:代码冗余
x = 'APPLE'
y = x.lower()
y.capitalize()

# 级联:代码简洁
x = 'APPLE'
x.lower().capitalize()


Calculated run time

timeModule, start time 1970, a program used for calculating the running time

import time
time.time()          # 1970年到现在,经过了多少秒

import time
result = []
start = time.time()  # 刚开始时间
for i in range(10000):
    result = result + [i]
print(len(result), time.time() - start)  # 计算耗时

Guess you like

Origin www.cnblogs.com/1k-yang/p/12040825.html