python 1

幂乘用双星号:**3

Decimal 模块
decimal 模块提供了修正十进制浮点数的运算。它的优点如下:
Decimal 模块可以使小数精确表示。
用户可自定义修改精度。
模块设计主要围绕三个概念:十进制数、算术上下文和信号。

from decimal import Decimal, getcontext
getcontext().prec=17 //设置精确度
result = 3 * Decimal(0.1)
print(type(result))
print(3 * Decimal(0.1))
print(3 * 0.1)

<class 'decimal.Decimal'>
0.30000000000000002
0.30000000000000004

print("a + b = ", end='') //不换行标记
o = a+b
print(o)

result = (34)+2
print('The result of the calculation of (3
4)+2', result, sep=' = ')//(‘’单引号双引号都可以,变量,step=’=’)

字符串:
quote = "Linus Torvalds once said, 'Any program is only as good as it is useful.'" //用单/双引号包含字符串,且字符串中可以再包含引号。

多行字符串可用三引号‘’’起止,而不用用//隔开

first_name = 'eric'
print(first_name)
print(first_name.title()) //变量名.title()为首字母变小写为大写

print(first_name.upper()) //变量名.upper()为变小写全为大写
first_name_titled = 'Eric'
print(first_name_titled.lower())//变为小写

连接字符串:
first_name = 'ada'
last_name = 'lovelace'
full_name = first_name + ' ' + last_name

message = full_name.title() + ' ' + \ //字符串1+‘ ’+字符串2 \“字符串3”
"was considered the world's first computer programmer."

print(message)
//字符串为两个及以上个单词组成时每一个单词首字母都会大写

格式化:
string_template = 'The result of the calculation of {calc} is {res}'
print("String Template: ", string_template)
print(string_template.format(calc='(34)+2', res=(34)+2))
Result:
String Template: The result of the calculation of {calc} is {res}
The result of the calculation of (3*4)+2 is 14

去空白符:
lstrip 去除左侧开端的空白符,rstrip 去除右端结尾的空白符,strip 去除两端空白符。
students = ['bernice', 'arron', 'cody']
for student in students:
print("Hello, " + student.title() + "!") // students的每一个元素都输出格式化后的结果

列表:
访问列表中的倒数的元素,可以用索引-n。

用循环语句访问列表中的每一项元素: //for后面的print需要缩进
dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
print("Resultes for the dog show are as follows:\n")
for index, dog in enumerate(dogs):
//要寻找元素的地址需添加一个index变量,使用enumerate()函数
place = str(index)
//地址为字符型变量,使用str函数将index的整型转换成字符型
print("Place: " + place + " Dog: " + dog.title())

用index函数来定位列表中的元素:
print(dogs.index('australian cattle dog'))

关键字 in 用来检测元素是否在列表中

向列表插末尾入元素:
dogs.append(‘pond’)
向列表中插入元素:
Dogs.insert(1,’pond’)

创建空列表可以对最新的元素和最老的元素进行操作。

排序:
students.sort() //对students中的元素进行按字母排列
students.sort(reverse=True) //对students中的元素进行按字母倒序排列
for student in students:
print(student.title())

students = ['bernice', 'aaron', 'cody']
/Display students in alphabetical order, but keep the original order.
print("Here is the list in alphabetical order:")
for student in sorted(students):
print(student.title())

/ Display students in reverse alphabetical order, but keep the original order.
print("\nHere is the list in reverse alphabetical order:")
for student in sorted(students, reverse=True):
print(student.title())

print("\nHere is the list in its original order:")
/ Show that the list is still in its original order.
for student in students:
print(student.title())
数值排序sort为increaseing顺序排

反转顺序:
students.reverse()

len() 函数用来获取列表长度,列表长度是指列表的元素个数。
remove() 函数可以通过元素的值来移除元素。只有找到的第一个元素会被移除,当有多个相同值的元素时,剩下的元素不会被移除。

从列表中切割后,原列表不受影响

range() 函数就是帮助我们生成大量数字的,得到的数字列表中包含开始数字但不包含结束数字
Five Wallets
• 想象5个钱包,每个钱包里有不同数量的不同数字的现金,把5个钱包和其中的现金用列表表示出来。
• 打印出现金最多的钱包的现金数。
• 打印出现金最少的钱包的现金数。
• 打印所有的现金数。

字符串列表:
就像列表中访问元素一样,我们可以利用字符位置访问字符串中的字符
你可以用关键字 in 查询某字串是否在字符串中

如果你想知道字符串出现的位置,可以使用 find() 方法。它会告诉你字串在字符串中的开始位置。如果你想知道最后一个出现的字串的初始位置,可以使用 rfind() 方法。
可以使用 replace() 函数用指定字符串替代字符串中的子串,所有该子串全部被替换。

想计算某一子串在字符串中出现了多少次,可以用 count() 函数来实现
split() 函数分裂字符串

元组可以被看成是不能改变的列表
我们需要用到 str() 函数将数字转为字符串
可以混合使用字符串%s和数字占位符%d

集合对象是一系列无序的,离散的哈希对象的集合。常用于成员测试,移除重复元素和一些算术运算例如交,并,差和对称差等,格式:set(列)
shapes = ['circle', 'square', 'triangle', 'circle']
set_of_shapes = set(shapes)
set_of_shapes
shapes = {'circle', 'square', 'triangle', 'circle'}
for shape in shapes:
print(shape)
运行出来shapes经过前面的set函数就可以有去除重复的功能
circle
square
triangle
{'polygon', 'circle', 'square', 'triangle'}

比较运算符两侧应该各有一个空格,比如:5 == 3
任何非零和非空对象都为真true
数字0、空对象和特殊对象None均为假false

/Set an initial condition.
game_active = True
/ Set up the while loop.
while game_active:
/Run the game.
/ At some point, the game ends and game_active will be set to False.
/ When that happens, the loop will stop executing.

在 Python 中你可以利用 input() 函数接受用户输入。函数可以接受一条提示信息,等待用户输入。
/ Get some input from the user.
variable = input('Please enter a value: ')
/ Do something with the value that was entered.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容