Python介绍(1)

1.Python介绍

1.1Python介绍(一) 1.2Python介绍(二)

打印

#print 打印一行字符串

In [1]: print("hello,world")

Out[1]: hello,world

print:是打印函数;()内可以是任意的参数,任意的对象;引号单、双均可,默认是引号开始,引号结束,如想输出单引号或双引号要用到转义字符\,如下:

In [2]: print("hello,\"world")

Out[2]: hello,"world

加转义字符丑陋通常可如下表示:print("hello,'world")和print('hello,"world')

In [3]:

import this

Out[3]: The Zen ofPython, by Tim Peters

 

Beautiful is better thanugly.

Explicit is better thanimplicit.

Simple is better thancomplex.

Complex is better thancomplicated.

Flat is better thannested.

Sparse is better thandense.

Readability counts.

Special cases aren'tspecial enough to break the rules.

Although practicalitybeats purity.

Errors should never passsilently.

Unless explicitlysilenced.

In the face ofambiguity, refuse the temptation to guess.

There should be one--and preferably only one --obvious way to do it.

Although that way maynot be obvious at first unless you're Dutch.

Now is better thannever.

Although never is oftenbetter than *right* now.

If the implementation ishard to explain, it's a bad idea.

If the implementation iseasy to explain, it may be a good idea.

Namespaces are onehonking great idea -- let's do more of those!

 

jupyter notebook 快捷键

Enter : 转入编辑模式

Shift-Enter : 运行本单元,选中下个单元

Ctrl-Enter : 运行本单元

Alt-Enter : 运行本单元,在其下插入新单元

Y : 单元转入代码状态

M :单元转入markdown状态

R : 单元转入raw状态

1 : 设定 1 级标题

2 : 设定 2 级标题

3 : 设定 3 级标题

4 : 设定 4 级标题

5 : 设定 5 级标题

6 : 设定 6 级标题

Up : 选中上方单元

K : 选中上方单元

Down : 选中下方单元

J : 选中下方单元

Shift-K : 扩大选中上方单元

Shift-J : 扩大选中下方单元

A : 在上方插入新单元

B : 在下方插入新单元

X : 剪切选中的单元

C : 复制选中的单元

Shift-V : 粘贴到上方单元

V : 粘贴到下方单元

Z : 恢复删除的最后一个单元

D,D : 删除选中的单元

Shift-M : 合并选中的单元

Ctrl-S : 文件存盘

S : 文件存盘

L : 转换行号

O : 转换输出

Shift-O : 转换输出滚动

Esc : 关闭页面

Q : 关闭页面

H : 显示快捷键帮助

I,I : 中断Notebook内核

0,0 : 重启Notebook内核

Shift : 忽略

Shift-Space : 向上滚动

Space : 向下滚动

 

编辑模式 ( Enter 键启动)

Tab : 代码补全或缩进

Shift-Tab : 提示

Ctrl-] : 缩进

Ctrl-[ : 解除缩进

Ctrl-A : 全选

Ctrl-Z : 复原

Ctrl-Shift-Z : 再做

Ctrl-Y : 再做

Ctrl-Home : 跳到单元开头

Ctrl-Up : 跳到单元开头

Ctrl-End : 跳到单元末尾

Ctrl-Down : 跳到单元末尾

Ctrl-Left : 跳到左边一个字首

Ctrl-Right : 跳到右边一个字首

Ctrl-Backspace : 删除前面一个字

Ctrl-Delete : 删除后面一个字

Esc : 进入命令模式

Ctrl-M : 进入命令模式

Shift-Enter : 运行本单元,选中下一单元

Ctrl-Enter : 运行本单元

Alt-Enter : 运行本单元,在下面插入一单元

Ctrl-Shift-- : 分割单元

Ctrl-Shift-Subtract: 分割单元

Ctrl-S : 文件存盘

Shift : 忽略

Up : 光标上移或转入上一单元

Down :光标下移或转入下一单元

 

 

计算器

In [13]: 5+100

Out[13]: 105

 

In [16]:100/10

Out[16]: 10.0

 

In [17]: 100-99

Out[17]: 1

 

In [15]: 100*10

Out[15]: 1000

 

取模

In [18]: 10%3

Out[18]: 1

 

乘方

In [19]: 10**3

Out[19]: 1000

 

10开3次方

In [20]: 10**(1/3)

Out[20]: 2.154434690031884

 

python中的数学模块:importmath

In [21]: import math

In [22]: math.pi

Out[22]: 3.141592653589793

 

In [23]: math.sin(math.pi/2)

Out[23]: 1.0

 

In [26]: math.floor(9.23432)

Out[26]: 9

 

In [27]: math.floor(9.897523)

Out[27]: 9

floor向下去整(小数点后的就不要了) ceil向上取整

 

In [25]: math.ceil(9.23432)

Out[25]:10

 

In [28]: math.ceil(9.897523)

Out[28]: 10

点住框格,点字母A,上面可生成多行

点住框格,点字母B,下面可生成多行

点住框格,点字母M,框格可Markdown的格式

在Markdown中,想选用第几标题,就打几个“#”符号

In [32]:

#苹果的花费(在Code中,#和文字表示注释,不影响代码的执行)

print(5*2)

#葡萄的花费

print(15*1.5)

#总花费

print(5*2+15*1.5)

10

22.5

32.5

上面的题存在3个问题:

1.无法脱离注释

2.计算总价时重复计算苹果和葡萄的花费

3.输出仅是一个数字,表达不清

In [42]:

apple_price=5

apple_weight=2

apple_cost=apple_price *apple_weight

grape_price=15

grape_weight=1.5

grape_cost=grape_price*grape_weight

total_cost=apple_cost+grape_cost

print(apple_cost,grape_cost,total_cost)

10 22.5 32.5

apple-price=5是一个赋值语句,把5赋值给apple-price,下面用apple-price代替5 输出的是1022.5 32.5,表达仍然不清晰,下面使用format函数解决

format增强的格式化字符串函数 作用:优化函数的输出

应用题引出变量

In [43]: "苹果的花费为:{};葡萄的花费为:{};总花费为:{}".format(apple_cost,grape_cost,total_cost)

#apple_cost替换第一个大括号——苹果的花费{},依次向后推

Out[43]:  '苹果的花费为:10;葡萄的花费为:22.5;总花费为:32.5'

 

猜你喜欢

转载自blog.csdn.net/zxqjinhu/article/details/80629934