figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; }
button, input, select, textarea { color: inherit; font: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 2; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.7); color: rgb(85, 85, 85); border-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px !important; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right: 0px; background-color: inherit; }
.CodeMirror-linenumber { user-select: none; }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background: inherit; position: relative !important; }
.md-diagram-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; background: 0px 0px; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; -webkit-tap-highlight-color: transparent; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print {
body, html { border: 1px solid transparent; height: 99%; break-after: avoid; break-before: avoid; }
#write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
.typora-export * { -webkit-print-color-adjust: exact; }
html.blink-to-pdf { font-size: 13px; }
.typora-export #write { padding-left: 32px; padding-right: 32px; padding-bottom: 0px; break-after: avoid; }
.typora-export #write::after { height: 0px; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background: rgb(204, 204, 204); display: block; overflow-x: hidden; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
[contenteditable="true"]:active, [contenteditable="true"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) {
.md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
.md-toc-h4 .md-toc-inner { margin-left: 5em; }
.md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
.md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; vertical-align: initial; }
a.md-print-anchor { white-space: pre !important; border-width: initial !important; border-style: none !important; border-color: initial !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; background: 0px 0px !important; text-decoration: initial !important; text-shadow: initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: hidden; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; zoom: 90%; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; overflow-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; }
.MathJax_SVG * { transition: none 0s ease 0s; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; margin-top: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="mermaid"] svg, [lang="flow"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }

.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background: inherit; }
.CodeMirror-scroll { overflow: auto hidden; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right: 1px solid rgb(221, 221, 221); background: inherit; white-space: nowrap; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background: inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background: inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; background: 0px 0px !important; border: none !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-radius: 0px; border-width: 0px; background: 0px 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; overflow-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; }
.CodeMirror-wrap pre { overflow-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right: 30px solid transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background: rgba(255, 255, 0, 0.4); }
@media print {
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}

:root { --side-bar-bg-color: #fafafa; --control-text-color: #777; }
html { font-size: 16px; }
body { font-family: "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); line-height: 1.6; }
#write { max-width: 860px; margin: 0px auto; padding: 30px 30px 100px; }
#write > ul:first-child, #write > ol:first-child { margin-top: 30px; }
a { color: rgb(65, 131, 196); }
h1, h2, h3, h4, h5, h6 { position: relative; margin-top: 1rem; margin-bottom: 1rem; font-weight: bold; line-height: 1.4; cursor: text; }
h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor { text-decoration: none; }
h1 tt, h1 code { font-size: inherit; }
h2 tt, h2 code { font-size: inherit; }
h3 tt, h3 code { font-size: inherit; }
h4 tt, h4 code { font-size: inherit; }
h5 tt, h5 code { font-size: inherit; }
h6 tt, h6 code { font-size: inherit; }
h1 { padding-bottom: 0.3em; font-size: 2.25em; line-height: 1.2; border-bottom: 1px solid rgb(238, 238, 238); }
h2 { padding-bottom: 0.3em; font-size: 1.75em; line-height: 1.225; border-bottom: 1px solid rgb(238, 238, 238); }
h3 { font-size: 1.5em; line-height: 1.43; }
h4 { font-size: 1.25em; }
h5 { font-size: 1em; }
h6 { font-size: 1em; color: rgb(119, 119, 119); }
p, blockquote, ul, ol, dl, table { margin: 0.8em 0px; }
li > ol, li > ul { margin: 0px; }
hr { height: 2px; padding: 0px; margin: 16px 0px; background-color: rgb(231, 231, 231); border: 0px none; overflow: hidden; box-sizing: content-box; }
li p.first { display: inline-block; }
ul, ol { padding-left: 30px; }
ul:first-child, ol:first-child { margin-top: 0px; }
ul:last-child, ol:last-child { margin-bottom: 0px; }
blockquote { border-left: 4px solid rgb(223, 226, 229); padding: 0px 15px; color: rgb(119, 119, 119); }
blockquote blockquote { padding-right: 0px; }
table { padding: 0px; word-break: initial; }
table tr { border-top: 1px solid rgb(223, 226, 229); margin: 0px; padding: 0px; }
table tr:nth-child(2n), thead { background-color: rgb(248, 248, 248); }
table tr th { font-weight: bold; border-width: 1px 1px 0px; border-top-style: solid; border-right-style: solid; border-left-style: solid; border-top-color: rgb(223, 226, 229); border-right-color: rgb(223, 226, 229); border-left-color: rgb(223, 226, 229); border-image: initial; border-bottom-style: initial; border-bottom-color: initial; text-align: left; margin: 0px; padding: 6px 13px; }
table tr td { border: 1px solid rgb(223, 226, 229); text-align: left; margin: 0px; padding: 6px 13px; }
table tr th:first-child, table tr td:first-child { margin-top: 0px; }
table tr th:last-child, table tr td:last-child { margin-bottom: 0px; }
.CodeMirror-lines { padding-left: 4px; }
.code-tooltip { box-shadow: rgba(0, 28, 36, 0.3) 0px 1px 1px 0px; border-top: 1px solid rgb(238, 242, 242); }
.md-fences, code, tt { border: 1px solid rgb(231, 234, 237); background-color: rgb(248, 248, 248); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9em; }
code { background-color: rgb(243, 244, 244); padding: 0px 2px; }
.md-fences { margin-bottom: 15px; margin-top: 15px; padding-top: 8px; padding-bottom: 6px; }
.md-task-list-item > input { margin-left: -1.3em; }
@media print {
html { font-size: 13px; }
table, pre { break-inside: avoid; }
pre { overflow-wrap: break-word; }
}
.md-fences { background-color: rgb(248, 248, 248); }
#write pre.md-meta-block { padding: 1rem; font-size: 85%; line-height: 1.45; background-color: rgb(247, 247, 247); border: 0px; border-radius: 3px; color: rgb(119, 119, 119); margin-top: 0px !important; }
.mathjax-block > .code-tooltip { bottom: 0.375rem; }
.md-mathjax-midline { background: rgb(250, 250, 250); }
#write > h3.md-focus::before { left: -1.5625rem; top: 0.375rem; }
#write > h4.md-focus::before { left: -1.5625rem; top: 0.285714rem; }
#write > h5.md-focus::before { left: -1.5625rem; top: 0.285714rem; }
#write > h6.md-focus::before { left: -1.5625rem; top: 0.285714rem; }
.md-image > .md-meta { border-radius: 3px; padding: 2px 0px 0px 4px; font-size: 0.9em; color: inherit; }
.md-tag { color: rgb(167, 167, 167); opacity: 1; }
.md-toc { margin-top: 20px; padding-bottom: 20px; }
.sidebar-tabs { border-bottom: none; }
#typora-quick-open { border: 1px solid rgb(221, 221, 221); background-color: rgb(248, 248, 248); }
#typora-quick-open-item { background-color: rgb(250, 250, 250); border-color: rgb(254, 254, 254) rgb(229, 229, 229) rgb(229, 229, 229) rgb(238, 238, 238); border-style: solid; border-width: 1px; }
.on-focus-mode blockquote { border-left-color: rgba(85, 85, 85, 0.12); }
header, .context-menu, .megamenu-content, footer { font-family: "Segoe UI", Arial, sans-serif; }
.file-node-content:hover .file-node-icon, .file-node-content:hover .file-node-open-state { visibility: visible; }
.mac-seamless-mode #typora-sidebar { background-color: var(--side-bar-bg-color); }
.md-lang { color: rgb(180, 101, 77); }
.html-for-mac .context-menu { --item-hover-bg-color: #E6F0FE; }
#md-notification .btn { border: 0px; }
.dropdown-menu .divider { border-color: rgb(229, 229, 229); }
-->

Python学习day07-Python进阶(1)数据类型的内置方法

一. 数字类型内置方法

1. 整型内置方法

1.1 作用

描述我们的年龄,id号等等,日常生活中可以用整数来表示的事物。

1.2 定义方法

 
 
 
​x
 
 
 
 
1
x = 10
2
x = int ('10')
3
x = int ('10.1')
4
5
6
# 当然第三个语句会报以下这个错误
7
#ValueError: invalid literal for int() with base 10: '10.1'
8
# 所以int括号里是不能加小数的
 
 

1.3 内置方法

整型数据是没有内置方法的,毕竟是比较基础的数据类型,只有算术运算和比较运算,也没有逻辑运算,像平时看到的and和or都没有。

1.4 存在一个还是多个值

整型仅存在一个值

1.5 有序or无序

整形数据没有顺序的概念

1.6 可变or不可变

 
 
 
x
 
 
 
 
1
# 可变和不可变都是针对变量值而言的
2
# 比如以下例程
3
# 可变(针对变量值而言,如果变量值改变而id内存地址不变,则称为可变的量,比如列表,字典等)
4
lis = [1,2,3]
5
print(id(lis))#2978432595720
6
lis[0]=2
7
print(id(lis))#2978432595720
8
#当然以上这个id内存地址不是固定不变的,每次生成都会改变
9
10
# 不可变(值变id号也变化,也就是说值和id内存空间是绑定的,值一旦改变内存空间也变化了)
11
x = 10
12
print(id(x))#1896115520
13
x += 1
14
print(id(x))#1896115552
 
 

所以,综上所述,整型是不可变得

2. 浮点型内置方法

2.1 作用

表示薪资等有小数部分的数据。

2.2 定义方法

 
 
 
x
11
8
11
 
 
 
 
1
x1 = float('1.11')
2
x2 = float('1')
3
print(type(x2))
4
5
输出结果为:
6
1.11
7
1.0
8
<class 'float'>
9
<class 'float'>
10
11
12
# 可以看到,上述定义中虽然float里面是1,但是类型还是浮点型,而不是整型
 
 

2.3 内置方法

和整型类似,仅有算术运算和比较运算。

2.4 存在一个还是多个值

仅存在一个值

2.5 有序or无序

没有此类概念

2.6 可变or不可变

不可变的数据类型

二. 字符串类型内置方法

1. 作用

描述姓名,性别,各种词语等等。

2. 定义方法

 
 
 
xxxxxxxxxx
1
 
 
 
 
1
s = b'ab'#此处留意,b开头在字符串前,打印出来会是bytes类型,也就是二进制类型,不是十进制
2
print(s)
3
print(type(s))
4
5
#其输出结果为:
6
b'ab'
7
<class 'bytes'>
8
 
 
  • 字符串常用的几个特殊字符

     
     
     
    x
    129
    27
     
     
     
     
    1
    #\n 换行
    2
    s = 'a\na'
    3
    print(s)
    4
    5
    a
    6
    a
    7
    8
    9
    #\t 缩进,通常为四个空格
    10
    s = 'a\ta'
    11
    print(s)
    12
    13
    aa
    14
    15
    16
    #\r 原位打印,即覆盖掉上一个打印结果
    17
    s = 'a\ra'
    18
    print(s)
    19
    20
    a
    21
    22
    #r'' 在字符串前面加r,可以使后面字符串内所有的\加字符的特殊字符都无效,比如
    23
    s = r'\na\ra'
    24
    print(s)
    25
    26
    \na\ra
    27
     
     

    以上几个特殊字符可能会在平时用到,可以多了解一下

3. 内置方法

字符串的内置方法是重点,熟练掌握之后可以融会贯通列表和字典的内置方法,有许多相似。

 
 
 
x
 
 
 
 
1
s = 'dog is cut'
2
# 索引号自左至右,从0开始
3
# 最常用的内置方法
4
#1. 索引取值
5
print(s[1])
6
o
7
8
# 2. 切片
9
print(s[0:4:1])#1表示从左向右切片
10
`dog `
11
print(s[4::-1])# -1表示从右向左,不推荐用
12
13
# 3.for循环
14
for i in s:
15
print(i)
16
17
# 4.striip()# 去两端指定字符,不能去中间的
18
s1 = '      nick handsome'
19
print(s1.strip())
20
print(s1.strip(' niem'))
21
s2 = '**!!nick handsome'
22
print(s2.strip('-*!e'))# 首先判断字符串s2的两端字符,为*,然后去strip里面找有没有*,有就去掉,再去判断字符串s2里的-!,以此类推
23
24
#制定多个字符一起去掉,只要strip里面有的字符就全部干掉
25
26
#5. split()切割,断开
27
print(s2.split())#默认以空格切割字符串
28
print(s2.split('!'))#以制定字符切割
29
print(s2.split('!',1))#切割一次,数字就是次数
30
31
#6. in 或not in
32
print('*'in s2)
33
34
#7. 长度len
35
print(len(s2)) #求字符串的长度
36
lis = ['sfd','sdf','sdfsf','sfdf']
37
dict = {'a':1,'b':55,'c':464}
38
print(len(s1))
39
print(len(dict))
40
print(len(lis))
41
42
# 稍微没那么常用的内置方法
43
# 1. lstrip() 左切割
44
# 2. rstrip()右切割,用法等同于strip
45
46
#
47
# 3. lower&upper全部大写,全部小写
48
s3 = 'aaabbJ'
49
print(s3.lower())
50
print(s3.upper())
51
# 4. startswith&endswith
52
# 判断字符串开头和结尾是不是某字符,返回值是True或者False
53
54
# 5. rsplit右切割
55
56
# 6. join# 用的较多的,增加字符串
57
print(s3.join(['353','345','53453','3534']))# 以s3位间隔符,拼接列表里每一个元素
58
s = '辣条/薯片/汽水/当归'
59
s1 = s.split('/'))
60
' '.join(s1)
61
print(s)# join和split通常组合使用
62
# 7. replace#顾名思义,替换
63
s2 = 'nick handsome'
64
print(f"{s2.replace('handsome','shuai')}")
65
66
# 8. isdigit判断是否为纯数字,salpha()判断是否为纯字符
67
s2 = 'aaccc'
68
print(s2.isdigit())
69
print(s2.isalpha())
70
71
# 比如如果要实现账号密码的登录验证:
72
username_db = 'nick'
73
pwd_db = '123'
74
inp_username = input('username:')
75
inp_pwd = input('pwd:')
76
77
78
79
# 不太常用的一些内置方法
80
81
# find|rfind|index|rindex|count
82
s2 = '****nick % handsome% ****'
83
print(s2.find('%'))
84
print(s2.rfind('%')) # 从左找和从右找,找到即停止,只限第一个
85
print(s2.index('%'))
86
print(s2.rindex('%'))#index找不到会报错,但是find找不到会返回值-1,所以日后编程多用find,而不是index
87
88
# center|ljust|rjust|z
89
s2 = 'nick handsome'
90
print(s2.center(50,'*'))# 填充到50字符,居中
91
print(s2.ljust(50,'*'))#居左
92
print(s2.rjust(50,'*'))#居右
93
94
# fill
95
print(s2.zfill(50))#填充0居右
96
97
# expandtabs
98
s2 = 'a\ta'# \t是缩进四个字符
99
print(s2)
100
print(s2.expandtabs(32))# 针对\t扩充tab
101
102
# captalize|swapcase|title
103
s2 = 'harry Potter'
104
105
print(s2.capitalize())#首字母大写,用在段落开始
106
print(s2.swapcase())#大小写互换
107
print(s2.title())#所有单词首字母大写,仅针对英文,对中文无效
108
 
 

4. 存在一个还是多个值

仅有一个值

5. 有序or无序

字符串有索引号,所以有序

6. 可变or不可变

字符串是不可变的数据类型

Tips:

  1. 字符串,列表,字典的内置方法是以后实现很多功能非常简便的方法,要多用,才能熟练。

  2. 最近做的一个非常有意思的leetcode的题目,贴在这里,供大家参考一下,就能看出来用内置方法和暴力破解差距有多大。

     
     
     
    x
     
     
     
     
    1
    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
    2
    3
    字符          数值
    4
    I             1
    5
    V             5
    6
    X             10
    7
    L             50
    8
    C             100
    9
    D             500
    10
    M             1000
    11
    例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。
    12
    13
    通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
    14
    15
    I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
    16
    X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 
    17
    C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
    18
    给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
    19
    20
    来源:力扣(LeetCode)
    21
    链接:<https://leetcode-cn.com/problems/roman-to-integer>
     
     

    以上是题目,下面是我自己做出的两种解题方法。

    第一种:暴力破解法,非常暴力

 
 
 
xxxxxxxxxx
1
54
 
 
 
 
1
class Solution:
2
    def romanToInt(self, s: str) -> int:
3
        s1 = list(s)
4
        d = 0
5
        size = len(s)
6
        s1.append('0')
7
        count = 0
8
        for i in range(size):
9
            if count == i+1 :
10
                continue
11
            if s1[i] == 'I':
12
                if s1[i+1] == 'X':
13
                    d += 9
14
                    count += 2
15
                elif s1[i+1] == 'V':
16
                    d += 4
17
                    count += 2
18
                else:
19
                    d += 1
20
                    count += 1
21
            if s1[i] == 'X':
22
                if s1[i+1] == 'C':
23
                    d += 90
24
                    count += 2
25
                elif s1[i+1] == 'L':
26
                    d += 40
27
                    count += 2
28
                else:
29
                    d += 10
30
                    count += 1
31
            if s1[i] == 'C':
32
                if s1[i+1] == 'M':
33
                    d += 900
34
                    count += 2
35
                elif s1[i+1] == 'D':
36
                    d += 400
37
                    count += 2
38
                else:
39
                    d += 100
40
                    count += 1
41
            if s1[i] == 'V':
42
                d += 5
43
                count += 1
44
            if s1[i] == 'L':
45
                d += 50
46
                count += 1
47
            if s1[i] == 'D':
48
                d += 500
49
                count += 1
50
            if s1[i] == 'M':
51
                d += 1000
52
                count += 1
53
        return(d)
54
        
 
 

第二种,用字典,和各种内置方法:

 
 
 
xxxxxxxxxx
1
18
 
 
 
 
1
class Solution:
2
    def romanToInt(self, s: str) -> int:
3
                    my_dict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900,'0':0}
4
                    s1 = list(s)
5
                    d = 0
6
                    size = len(s)
7
                    count = 0
8
                    s1.append('0')
9
                    for i in range(size):
10
                        if count == i + 1:
11
                             continue
12
                        if my_dict[s1[i]] < my_dict[s1[i+1]]:
13
                            d += my_dict[f'{s1[i]}{s1[i+1]}']
14
                            count+=2
15
                        else:
16
                            d += my_dict[s1[i]]
17
                            count+=1
18
                    return(d)
 
 

以上明显可以看出,执行用时基本缩短了一半,可见效率很高。

Python学习day07 - Python进阶(1) 内置方法的更多相关文章

  1. 4月17日 python学习总结 反射、object内置方法、元类

    一.反射 下述四个函数是专门用来操作类与对象属性的,如何操作? 通过字符串来操作类与对象的属性,这种操作称为反射 class People: country="China" def ...

  2. python学习日记(OOP——类的内置方法)

    __str__和__repr__ 改变对象的字符串显示__str__,__repr__ 我们先定义一个Student类,打印一个实例: class Student(object): def __ini ...

  3. 铁乐学python_day24_面向对象进阶1_内置方法

    铁乐学python_day24_面向对象进阶1_内置方法 题外话1: 学习方法[wwwh] what where why how 是什么,用在哪里,为什么,怎么用 学习到一个新知识点的时候,多问问上面 ...

  4. Python之路(第二十九篇) 面向对象进阶:内置方法补充、异常处理

    一.__new__方法 __init__()是初始化方法,__new__()方法是构造方法,创建一个新的对象 实例化对象的时候,调用__init__()初始化之前,先调用了__new__()方法 __ ...

  5. Python之路(第二十六篇) 面向对象进阶:内置方法

    一.__getattribute__ object.__getattribute__(self, name) 无条件被调用,通过实例访问属性.如果class中定义了__getattr__(),则__g ...

  6. Python之路(第二十七篇) 面向对象进阶:内置方法、描述符

    一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...

  7. Python: list列表的11个内置方法

    先来逼逼两句: 在实际开发中,经常需要将一组(不只一个)数据存储起来,以便后边的代码使用.在VBA中有使用数组,可以把多个数据存储到一起,通过数组下标可以访问数组中的每个元素.Python 中没有数组 ...

  8. python基础语法18 类的内置方法(魔法方法),单例模式

    类的内置方法(魔法方法): 凡是在类内部定义,以__开头__结尾的方法,都是类的内置方法,也称之为魔法方法. 类的内置方法,会在某种条件满足下自动触发. 内置方法如下: __new__: 在__ini ...

  9. Python之面向对象之反射、内置方法

    一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静态方法:让类里的方法直接被 ...

随机推荐

  1. 如何收集项目日志统一发送到kafka中?

    上一篇(http://qindongliang.iteye.com/blog/2354381 )写了收集sparkstreaming的日志进入kafka便于后续收集到es中快速统计分析,今天就再写一篇 ...

  2. iOS开发系列-修改项目工程名

    当前有项目工程名为iOS,需要修改工程名为IFLY.在修改前注意备份项目 修改项目名 出现弹框,点击Rename 修改工程目录文件名 注意Tests与UITests不要删除 选中IFLY.xcodep ...

  3. MapReduce1.0的缺陷

  4. gmock 简单笔记

    std::shared_ptr<MockThreadRCInvester> spMockaAcc; HelperThreadRCInvester helperAcc; // spMockA ...

  5. wordpress 获取所有管理员的邮箱

    function get_administrator_email(){ $blogUsers = get_users('role=Administrator'); $adminEmails = arr ...

  6. Java 多线程 - 死锁deadlock产生原因+避免方法

    ref: java中产生死锁的原因及如何避免 https://blog.csdn.net/m0_38126177/article/details/78587845 java如何避免死锁 http:// ...

  7. 【JZOJ4905】【BZOJ4720】【luoguP1850】换教室

    description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的课 ...

  8. Flink on YARN(下):常见问题与排查思路

    Flink 支持 Standalone 独立部署和 YARN.Kubernetes.Mesos 等集群部署模式,其中 YARN 集群部署模式在国内的应用越来越广泛.Flink 社区将推出 Flink ...

  9. 揭秘 Flink 1.9 新架构,Blink Planner 你会用了吗?

    本文为 Apache Flink 新版本重大功能特性解读之 Flink SQL 系列文章的开篇,Flink SQL 系列文章由其核心贡献者们分享,涵盖基础知识.实践.调优.内部实现等各个方面,带你由浅 ...

  10. BZOJ 4557 (JLOI 2016) 侦查守卫

    4557: [JLoi2016]侦察守卫 Time Limit: 20 Sec Memory Limit: 256 MB Submit: 493 Solved: 342 [Submit][Status ...