lbk60ox 发表于 2024-8-18 10:10:37

什么是Python?之 7 语言的形成单词、函数


    <h1 style="color: black; text-align: left; margin-bottom: 10px;">一个语言的本质是单词及相应的动词<span style="color: black;">形成</span>的语句,及句子的含义</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">任何一种“语言”,都是<span style="color: black;">各样</span>“语言特性”的组合。打个比方吧,一个程序语言就像一台电脑。它的牌子可能叫“联想”,<span style="color: black;">或</span>“IBM”,<span style="color: black;">或</span>“Dell”,<span style="color: black;">或</span>“苹果”。<span style="color: black;">那样</span>,你<span style="color: black;">能够</span>说苹果<span style="color: black;">必定</span>比 IBM 好吗?你<span style="color: black;">不可</span>。你得<span style="color: black;">瞧瞧</span>它里面装的是什么型号的处理器,有多少个核,主频多少,有多少 L1 cache,L2 cache……,有多少内存和硬盘,<span style="color: black;">表示</span>器分辨率有多大,显卡是什么 GPU,网卡速度,等等<span style="color: black;">各样</span>“配置”。有时候你还得看各个组件之间的兼容性。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这些配置对应到程序语言里面,<span style="color: black;">便是</span><span style="color: black;">所说</span>“语言特性”。举<span style="color: black;">有些</span>语言特性的例子:</span></p><span style="color: black;">变量定义</span>num&nbsp; = 1
    name =<span style="color: black;">"jack"</span>
    <span style="color: black;"># 定义了一个变量 名为num它的值是 数字 1</span>
    <span style="color: black;"># 定义了一个变量 名为name它的值是 字符串 "jack"</span>

    <span style="color: black;">倘若</span>我想定义一个变量,什么都值都不想给呢?

    noneValue =None
    <span style="color: black;">&gt;&gt;</span>&gt; num = <span style="color: black;">1</span>
    <span style="color: black;">&gt;&gt;</span>&gt; name = <span style="color: black;">"jack"</span>
    <span style="color: black;">&gt;&gt;</span>&gt; noneName = None<span style="color: black;">&gt;&gt;</span>&gt; type(num)
    &lt;<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">int</span>&gt;</span>
    <span style="color: black;">&gt;&gt;</span>&gt; type(name)
    &lt;<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">str</span>&gt;</span>
    <span style="color: black;">&gt;&gt;</span>&gt; type(noneName)
    &lt;<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">NoneType</span>&gt;</span>
    <span style="color: black;">&gt;&gt;</span>&gt;<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/e9b78aa0fa914573a3e020e7aefcb902~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=KI2VCfBha4hCTRsOlzDMfu6Uqo8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思考:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">怎样</span>要定义一个变量小数,<span style="color: black;">或</span>无限小数应该怎么办</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>要定义一堆数据的集合要怎么办?</p><span style="color: black;">算术运算</span>&nbsp;<span style="color: black;"># 比如j 加减乘除</span>
    &gt;&gt;&gt; one =<span style="color: black;">1</span>
    <span style="color: black;">&gt;&gt;</span>&gt; two =<span style="color: black;">2</span>
    <span style="color: black;">&gt;&gt;</span>&gt; one + two
    <span style="color: black;">3</span>
    <span style="color: black;">&gt;&gt;</span>&gt; <span style="color: black;">1</span> + <span style="color: black;">2</span>
    <span style="color: black;">3</span>
    <span style="color: black;">&gt;&gt;</span>&gt;
    &gt;&gt;&gt; <span style="color: black;">1</span>/<span style="color: black;">2</span>
    <span style="color: black;">0</span>.<span style="color: black;">5</span>
    <span style="color: black;">&gt;&gt;</span>&gt; one /two
    <span style="color: black;">0</span>.<span style="color: black;">5</span>
    <span style="color: black;">&gt;&gt;</span>&gt; <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/a7342aa54cb54f39b91f67a1b5879418~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=4eL2lU%2BABIvGsQKPCYdXKwC3MGA%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">for 循环语句,while 循环语句</span><span style="color: black;">&gt;&gt;&gt;</span> <span style="color: black;">while</span> <span style="color: black;">i</span> <span style="color: black;">&lt;</span> <span style="color: black;">10:</span>
    <span style="color: black;">print(i)</span>
    <span style="color: black;">i</span> <span style="color: black;">=</span> <span style="color: black;">i</span> <span style="color: black;">+</span> <span style="color: black;">1</span>


    <span style="color: black;">1</span>
    <span style="color: black;">2</span>
    <span style="color: black;">3</span>
    <span style="color: black;">4</span>
    <span style="color: black;">5</span>
    <span style="color: black;">6</span>
    <span style="color: black;">7</span>
    <span style="color: black;">8</span>
    <span style="color: black;">9</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/2eececbfb4be4695a129b239d2242bbf~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=CyZpv7%2BuFyeJEie4FpLNFHyj2Xg%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">函数定义,函数调用</span><span style="color: black;">&gt;&gt;</span>&gt; <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">max</span><span style="color: black;">(one,two)</span></span>:
    if one &gt; two :
    return one
    <span style="color: black;">else:</span>
    <span style="color: black;">return</span> two

    <span style="color: black;"># 计算两个数字的<span style="color: black;">体积</span></span>
    <span style="color: black;">&gt;&gt;</span>&gt; max(<span style="color: black;">1</span>, <span style="color: black;">2</span>)
    <span style="color: black;">2</span>
    <span style="color: black;">&gt;&gt;</span>&gt; <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/6e00c47c032149f6bddbafd367310c6a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=AumQ%2Bz05UOkqFM7CpB8HgWUwFss%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">递归</span><span style="color: black;">静态类型系统</span><span style="color: black;">类型推导</span><span style="color: black;">lambda 函数</span><span style="color: black;">面向对象</span><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Person</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self)</span>:</span>
    <span style="color: black;">pass</span>

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">say</span><span style="color: black;">(self,words)</span>:</span>
    print(<span style="color: black;">"hello"</span> ,words)

    p = Person()
    p.say(<span style="color: black;">" world "</span>)<span style="color: black;">垃圾回收</span><span style="color: black;">指针算术</span><span style="color: black;">goto 语句</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">程序语言<span style="color: black;">亦</span>是<span style="color: black;">同样</span>的道理。<span style="color: black;">针对</span>初学者<span style="color: black;">来讲</span>,其实<span style="color: black;">不必</span>纠结到底要先学哪一种语言,再学哪一种。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">每一种语言里面必然有一套“通用”的特性。<span style="color: black;">例如</span>变量,函数,整数和浮点数运算,等等。这些是<span style="color: black;">每一个</span>通用程序语言里面都<span style="color: black;">必要</span>有的,一个都<span style="color: black;">不可</span>少</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这个过程中,随着需求的<span style="color: black;">显现</span>,他可能会问<span style="color: black;">这般</span>的问题:</span></p><span style="color: black;">这个语言的“变量定义”是什么语法,<span style="color: black;">必须</span>“声明类型”吗,还是<span style="color: black;">能够</span>用“类型推导”?</span><span style="color: black;">它的“类型”是什么语法?<span style="color: black;">是不是</span>支持“泛型”?泛型的 “variance” <span style="color: black;">怎样</span>表达?</span><span style="color: black;">这个语言的“函数”是什么语法,“函数调用”是什么语法,可否<span style="color: black;">运用</span>“缺省参数”?</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">把握</span>关键语言特性,忽略次要特性</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">printf 的作业。学生写出几百行的 printf,却<span style="color: black;">不睬</span>解变量和函数是什么,<span style="color: black;">乃至</span>连算术语句和循环语句都不<span style="color: black;">晓得</span>怎么用!这<span style="color: black;">便是</span><span style="color: black;">为何</span><span style="color: black;">非常多</span>初学者感觉编程很难,我连 %d,%f,%.2f 的含义都记不住,还怎么学编程!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>举<span style="color: black;">有些</span>次要语言特性的例子:</span></p><span style="color: black;">C 语言的语句块,<span style="color: black;">倘若</span>里面<span style="color: black;">仅有</span>一条语句,<span style="color: black;">能够</span>不打花括号。</span><span style="color: black;">Go 语言的函数参数类型<span style="color: black;">倘若</span><span style="color: black;">同样</span><span style="color: black;">能够</span>合并在<span style="color: black;">一块</span>写,<span style="color: black;">例如</span> func foo(s string, x, y, z int, c bool) { ... }</span><span style="color: black;">Perl 把正则表达式<span style="color: black;">做为</span>语言的一种特殊语法</span><span style="color: black;">JavaScript 语句<span style="color: black;">能够</span>在某些时候省略句尾的分号</span><span style="color: black;">Haskell 和 ML 等语言</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">在Python中<span style="color: black;">怎样</span><span style="color: black;">显示</span>这是一堆语句块呢,基本上是靠缩进</h1>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/cf2fc053a8c4418f8fbc188d87998fc1~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=ukEvOv8eKSeJdx4YEZPpb73oq40%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大众</span>看到这两行语句了么,print前面的空格不<span style="color: black;">同样</span>,会<span style="color: black;">引起</span>程序出错,</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>你的程序这两句,<span style="color: black;">没法</span>决定它们是不是一个语句块</p><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">say</span><span style="color: black;">()</span>:</span>print(<span style="color: black;">"11111"</span>)
    print(<span style="color: black;">"11222"</span>)<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/11ae261ab8504c519266a0a0bd134ffa~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=R7%2BVGZjBjAqG8tcjQE0xNH%2BOTzI%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">say</span><span style="color: black;">()</span>:</span>
    print(<span style="color: black;">"11111"</span>)
    print(<span style="color: black;">"11222"</span>)<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/bb4889d7dd8e42d7b5cb21b9c3288506~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723902758&amp;x-signature=wJXSzCa%2BS9mpYbg3RbuN%2FyOrEHM%3D" style="width: 50%; margin-bottom: 20px;"></div>




DH802036 发表于 2024-9-25 14:10:30

“BS”(鄙视的缩写)‌

1fy07h 发表于 2024-10-5 21:43:17

认真阅读了楼主的帖子,非常有益。

nqkk58 发表于 2024-11-12 20:29:27

这夸赞甜到心里,让我感觉温暖无比。
页: [1]
查看完整版本: 什么是Python?之 7 语言的形成单词、函数