《Python数据分析》第二版.第二章.[学习笔记][Jupyter notebook]

本文深入探讨Python的基础语法,包括变量操作、数据类型转换、循环控制结构以及字符串处理。同时,介绍了IPython的高级特性,如魔术命令、代码执行和调试技巧,以及JupyterNotebooks的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第2章 Python语法基础,IPython和Jupyter Notebooks

2.2 IPython 基础

1. 变量前后使⽤问号?,可以显示对象的信息

2. ??会显示函数的源码

3. %run 运行.py 文件

4. %load 将脚本导⼊到 ⼀个代码格中

5. Ctrl-C 中断运⾏的代码

6. %paste 可以直接运⾏剪贴板中的代码

7. %cpaste可以粘贴任意多的代码再运⾏。如果粘贴了错误的代码,可以⽤Ctrl-C中断。

8. %timeit 测量任何Python语句的执⾏时间

9. 常⽤的IPython魔术命令

10. %matplotlib inline Jupyter⾏内matplotlib作图

11. for 循环

for x in array:
    if x < pivot:
        less.append(x)
    else:
        greater.append(x)
#冒号标志着缩进代码块的开始,冒号之后的所有代码的缩进量必须相同,直到代码块结束。

 #用作单行注释

12. 在Python中创建变量(或名字),你就在等号左边创建了⼀个对这个变量的引⽤

In : a = [1, 2, 3] 
In : b = a        #python中此处b和a都是对[1, 2, 3]的引用
In : a.append(4)  #a中添加一个元素
In : b            #b也会发生改变
Out: [1, 2, 3, 4]

13. 将对象作为参数传递给函数时,新的局域变量创建了对原始对象的引⽤,⽽不是复制。

14. 假设有以下模块:

#some_module.py
PI = 3.14159
def f(x):
    return x + 2
def g(a, b):
    return a + b

从同目录下的另一个文件访问some_module.py中定义的变量和函数:

import some_module
result = some_module.f(5)
pi = some_module.PI

使用as关键词,可以给引入起不同的变量名:

mport some_module as sm
from some_module import PI as pi, g as gf
r1 = sm.f(pi)
r2 = gf(6, pi)

15.二元运算符

16.对于有换行的字符串,可以使用三引号,'''或"""。

17.c.count('\n')

18.字符串不可更改

19.str()转换为字符串

20.s[:3]

21.s = r ’ ' 表明字符就是其本身

22.a + b 合并字符串

23.访问属性和方法

In : a = 'foo'
In : a.<Press Tab>
a.capitalize  a.format      a.isupper     a.rindex      a.strip
a.center      a.index       a.join        a.rjust       a.swapcase
a.count       a.isalnum     a.ljust       a.rpartition  a.title
a.decode      a.isalpha     a.lower       a.rsplit      a.translate
a.encode      a.isdigit     a.lstrip      a.rstrip      a.upper
a.endswith    a.islower     a.partition   a.split       a.zfill
a.expandtabs  a.isspace     a.replace     a.splitlines
a.find        a.istitle     a.rfind       a.startswith

24.format方法

In : template = '{0:.2f} {1:s} are worth US${2:d}'
#{0:.2f}两位小数的浮点数,{1:s}字符串,{2:d}整数
In : template.format(4.5560, 'Argentine Pesos', 1)
Out: '4.56 Argentine Pesos are worth US$1'

25.encode编码,decode解码

26.str、bool、int、float

27.datetime模块

In : from datetime import datetime, date, time
In : dt = datetime(2011, 10, 29, 20, 30, 21)
In : dt.day
Out: 29
In : dt.minute
Out: 30

28.trftime方法可以将datetime格式化为字符串:

In : dt.strftime('%m/%d/%Y %H:%M')
Out: '10/29/2011 20:30'

29.strptime可以将字符串转换成datetime对象:

In : datetime.strptime('20091031', '%Y%m%d')
Out: datetime.datetime(2009, 10, 31, 0, 0)

30.dt.replace(minute=0, second=0)

31.两个datetime对象的差会产生一个datetime.timedelta类型:delta

32.elif = c语言中else if

if x < 0:
    print('It's negative')

33.or、and

34.continue

sequence = [1, 2, None, 4, None, 5]
total = 0
for value in sequence:
    if value is None:
        continue
    total += value

35.break

sequence = [1, 2, 0, 4, 6, 5, 2, 1]
total_until_5 = 0
for value in sequence:
    if value == 5:
        break
    total_until_5 += value

36.while

x = 256
total = 0
while x > 0:
    if total > 500:
        break
    total += x
    x = x // 2

37.pass 不执行任何动作

38.range(起点,终点,步长),产生一个均匀分布的不包括终点的整数序列

sum = 0
for i in range(100000):#对0到99999中3或5的倍数求和
    # % is the modulo operator
    if i % 3 == 0 or i % 5 == 0:
        sum += i

39.三元表达式

value = true-expr if condition else false-expr
#例
In : x = 5
In : 'Non-negative' if x >= 0 else 'Negative'
Out: 'Non-negative'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值