_010001SUTANG_

To ancient evenings and distant music.

《聪明办法学 Python(第2版)》第一章学习心得:启航 Getting Started

通过学习《聪明办法学 Python(第2版)》第一章“启航 Getting Started”,我对 Python 编程有了初步而系统的认识,对编程的兴趣也愈发浓厚。

第一印象:C 语言 vs Python

教程开篇通过对比 C 语言和 Python 的 “Hello, World” 程序,让我立刻感受到了两者在语法层面的差异:

  • C 语言版本:

    1
    2
    3
    4
    5
    #include <stdio.h>
    int main() {
    printf("Hello, World\n");
    return 0;
    }
  • Python 版本:

    1
    print("聪明办法学 Python")

这种简洁、自然的写法让我深刻体会到 Python 的“易读易写”哲学,对于初学者而言,学习曲线更为平缓,也激发了我进一步学习的动力。

注释的作用与写法

教程强调了注释对于代码可读性的重要性,并引用了一句幽默的话:

“当初写这段代码的时候,只有上帝和我知道它是干嘛的;现在只有上帝知道。”

这让我意识到良好的注释不仅是对他人负责,也是在为未来的自己埋下伏笔。Python 中常见的注释方式包括:

  • 单行注释:#
  • 多行注释:''' 多行内容 '''\"\"\" 多行内容 \"\"\"

在实际编程中,适当的注释能够大大提升代码的可维护性和团队协作效率。

控制台输入输出的灵活性

print() 函数在 Python 中的强大表现令人印象深刻。通过一些简单的参数设置,可以灵活控制输出格式:

  • end 参数:改变输出后的结尾字符
  • sep 参数:定义多个值之间的分隔符
  • 字符串操作:使用 + 拼接、* 重复
  • f-string 格式化输出:
    1
    2
    name = "Python"
    print(f"我正在学习{name}")

此外,input() 函数使程序具备了与用户交互的能力。虽然输入默认是字符串类型,但可以通过 int()float() 等函数进行类型转换,从而处理数字输入。

错误类型与模块导入

本章还介绍了三类常见错误:

  • 语法错误:程序结构不符合语法规范
  • 运行时错误:程序执行过程中触发,如除以零
  • 逻辑错误:程序能运行但结果不符合预期

了解这些错误类型,有助于我在今后的编程中更高效地定位和修复问题。

模块导入的概念也让我初步认识到了 Python 的强大生态系统。例如,使用 import math 并调用 math.factorial(5) 来计算阶乘,这不仅丰富了编程功能,也让我对未来学习和使用各种第三方库充满期待。


总结

通过第一章的学习,我初步掌握了 Python 的基本语法、注释、输入输出、错误类型识别与模块导入等核心概念。Python 的简洁与强大让我倍感亲切,也为后续深入学习奠定了坚实的基础。

“好的开始是成功的一半”,这一章无疑让我在学习编程的路上迈出了扎实的第一步。

博客搭建记录(EN)

Under the patient guidance of seniors and driven by course requirements, I successfully completed the journey of building my personal blog using GitHub and Hexo, publishing my first blog post. The entire process was filled with exploration and rewarding experiences:

During the preliminary preparation phase, I strictly followed the workflow: First, I downloaded and installed Node.js (verified by entering node -v and npm -v in the terminal), followed by installing Git (checked with git --version). These two tools formed the foundation for Hexo’s operation and code management. Next, I registered an account on GitHub’s official website with the username [your GitHub username], laying the groundwork for my blog’s online home.

In the local blog construction phase, I created a dedicated directory on my hard drive (e.g., D:\my-blog), globally installed Hexo via the command line (npm install -g hexo-cli), and confirmed the installation with hexo -v. After initializing the project with hexo init, seeing the default “Hello World” page at http://localhost:4000 sparked indescribable excitement! I then proceeded to personalize the configuration: opening the _config.yml file in a text editor, modifying title to my blog’s name, subtitle to a personalized tagline, and author to my pen name. After carefully comparing themes on Hexo’s official site, I finally cloned my preferred theme (e.g., git clone https://github.com/iissnan/hexo-theme-next themes/next) and updated the theme value in the configuration file.

During the content creation phase, I used hexo new "My First Blog" to generate a Markdown file in source/_posts, edited the content with Typora, and previewed the layout in real-time using hexo clean && hexo g && hexo s. Markdown’s simple syntax allowed me to focus purely on writing without formatting distractions.

The deployment phase was critical: I created a public repository named [username].github.io on GitHub, then configured SSH keys in Git Bash—generating a key pair with ssh-keygen and adding the full content of id_rsa.pub to my GitHub account’s SSH settings. Next, I modified the deploy section in Hexo’s configuration file, setting the repository URL to SSH format (git@github.com:[username]/[username].github.io.git) and the branch to main. When I ran hexo clean && hexo g && hexo d to complete deployment and saw my live blog at https://[username].github.io, the fatigue from debugging melted into pure accomplishment!

This practice not only equipped me with static blog-building skills but also deepened my appreciation for continuous output—just as seniors emphasized: “A blog’s vitality comes from its relentless flow of content.” Moving forward, I will persist in documenting technical insights and growth milestones, nurturing this self-built knowledge corner to thrive.

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

0%