百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

《Python程序设计》——Python入门(2)

bigegpt 2024-09-09 01:21 7 浏览

今天开始为大家带来《Python程序设计》的学习。今天学习第一章——Python入门第二部分的内容,主要内容是程序结构和编码规范。

Today begins the study of Python Programming for you. Today learn the first chapter - the second part of Introduction to Python, which focuses on program structure and coding specifications.

01

文件类型

在python中,常用的文件类型有三种:源代码文件,字节代码文件和优化代码文件。

(1)源代码文件:python源代码文件扩展名为p.y,可以使用文本编辑器编辑。

(2)字节代码文件:字节代码文件扩展名为.pyc,是由python源代码文件编译而成的二进制文件,由python加速执行,其速度较快,能够隐藏源代码。可以通过python.exe或脚本模式将python源文件编译成python字节代码文件。

(3)优化代码文件:python优化代码文件是优化编译后的文件,无法用文本编辑器进行编辑,一般用于嵌入式系统。

In python, there are three common file types: source code files, byte code files and optimized code files.

(1) Source code files: python source code files have the extension p.y and can be edited with a text editor.

(2) Byte code files: Byte code files with the extension .pyc are binary files compiled from python source code files, which are accelerated and executed by python, and are faster and can hide the source code. You can compile python source files into python byte code files by python.exe or script mode.

(3) Optimized code file: python optimized code file is a file compiled by optimization, which cannot be edited by text editor and is generally used in embedded system.

02

代码结构

简单的python程序只需要很少的代码。但是,一个具有复杂功能的python程序代码量可能非常大,需要具有良好的代码结构。

A simple python program requires very little code. However, a python program with complex functionality can be very large in code and needs to have good code structure.

02

程序编码规范

(1)注释:单行注释:将要注释的一行代码以#开头。

多行注释:将要注释的多行代码以#开头,或将要注释的多行代码放在成对'''(3个单引号)和"""(3个双引号)之间。

批量注释法:在IDLE中,选中要注释的代码块,同时按Alt+3组合键,添加批量注释。选中要注释的代码块,同时按Alt+4组合键,删除批量注释。

在PyCharm中,选中要注释或取消注释的代码块,同时按Ctrl+/组合键,在添加批量注释和删除批量注释之间切换。

(2)代码缩进:python采用严格的缩进方式来体现代码的从属关系,而不使用大括号{}。缩进空格数是可变的,但同一个语句块的语句必须包含相同的缩进空格数。

(3)多行书写一条语句:python可以用反斜杠(\)来实现多行书写一条语句。

(4)空行:空行是程序代码的一部分。例如,函数之间和类的方法之间用空行隔开,表示一段新的代码的开始;类和函数入口之间也用空行隔开,以突出函数入口的开始。空行的作用是分隔两段不同功能或含义的代码。

(5)语句块:缩进相同的一组语句构成一个语句块,又称为语句组。像if,while,def和class这样的复合语句,首行以关键字开始,以冒号(:),该行之后的一行或多行代码构成语句块。同一个语句块中的语句缩进必须相同。

(1) Comments. Single line comment: the line of code to be commented starts with #.

Multi-line comment: the multi-line code to be commented starts with #, or the multi-line code to be commented is placed between pairs of ''' (3 single quotes) and """ (3 double quotes).

Batch comment method: In IDLE, select the code block to be commented and press Alt+3 at the same time to add a batch comment. Select the block to be commented and press Alt+4 at the same time to remove the batch comment.

In PyCharm, select the block to be commented or uncommented, and press Ctrl+/ at the same time to toggle between adding bulk comments and deleting bulk comments.

(2) Code indentation: python uses strict indentation to reflect the subordination of code, instead of using curly brackets {}. The number of indented spaces is variable, but statements in the same statement block must contain the same number of indented spaces.

(3) Writing a statement on multiple lines: python can use backslashes (\) to write a statement on multiple lines.

(4) blank lines: blank lines are part of the program code. For example, a blank line separates functions and methods of a class, indicating the beginning of a new piece of code; a blank line also separates classes and function entries to highlight the beginning of a function entry. The role of blank lines is to separate two different functions or meaning of the code.

(5) statement block: indentation of the same set of statements constitute a statement block, also known as a statement group. Like if, while, def and class such a compound statement, the first line to the keyword to start with a colon (:), the line after the line or lines of code constitute a statement block. The indentation of statements in the same block must be the same.


参考资料:文字:百度;图片:微博;翻译:百度翻译

本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。

相关推荐

Java 泛型大揭秘:类型参数、通配符与最佳实践

引言在编程世界中,代码的可重用性和可维护性是至关重要的。为了实现这些目标,Java5引入了一种名为泛型(Generics)的强大功能。本文将详细介绍Java泛型的概念、优势和局限性,以及如何在...

K8s 的标签与选择器:流畅运维的秘诀

在Kubernetes的世界里,**标签(Label)和选择器(Selector)**并不是最炫酷的技术,但却是贯穿整个集群管理与运维流程的核心机制。正是它们让复杂的资源调度、查询、自动化运维变得...

哈希Hash算法:原理、应用(哈希算法 知乎)

原作者:Linux教程,原文地址:「链接」什么是哈希算法?哈希算法(HashAlgorithm),又称为散列算法或杂凑算法,是一种将任意长度的数据输入转换为固定长度输出值的数学函数。其输出结果通常被...

C#学习:基于LLM的简历评估程序(c# 简历)

前言在pocketflow的例子中看到了一个基于LLM的简历评估程序的例子,感觉还挺好玩的,为了练习一下C#,我最近使用C#重写了一个。准备不同的简历:image-20250528183949844查...

55顺位,砍41+14+3!季后赛也成得分王,难道他也是一名球星?

雷霆队最不可思议的新星:一个55号秀的疯狂逆袭!你是不是也觉得NBA最底层的55号秀,就只能当饮水机管理员?今年的55号秀阿龙·威金斯恐怕要打破你的认知了!常规赛阶段,这位二轮秀就像开了窍的天才,直接...

5分钟读懂C#字典对象(c# 字典获取值)

什么是字典对象在C#中,使用Dictionary类来管理由键值对组成的集合,这类集合被称为字典。字典最大的特点就是能够根据键来快速查找集合中的值,其键的定义不能重复,具有唯一性,相当于数组索引值,字典...

c#窗体传值(c# 跨窗体传递数据)

在WinForm编程中我们经常需要进行俩个窗体间的传值。下面我给出了两种方法,来实现传值一、在输入数据的界面中定义一个属性,供接受数据的窗体使用1、子窗体usingSystem;usingSyst...

C#入门篇章—委托(c#委托的理解)

C#委托1.委托的定义和使用委托的作用:如果要把方法作为函数来进行传递的话,就要用到委托。委托是一个类型,这个类型可以赋值一个方法的引用。C#的委托通过delegate关键字来声明。声明委托的...

C#.NET in、out、ref详解(c#.net framework)

简介在C#中,in、ref和out是用于修改方法参数传递方式的关键字,它们决定了参数是按值传递还是按引用传递,以及参数是否必须在传递前初始化。基本语义对比修饰符传递方式可读写性必须初始化调用...

C#广义表(广义表headtail)

在C#中,广义表(GeneralizedList)是一种特殊的数据结构,它是线性表的推广。广义表可以包含单个元素(称为原子),也可以包含另一个广义表(称为子表)。以下是一个简单的C#广义表示例代...

「C#.NET 拾遗补漏」04:你必须知道的反射

阅读本文大概需要3分钟。通常,反射用于动态获取对象的类型、属性和方法等信息。今天带你玩转反射,来汇总一下反射的各种常见操作,捡漏看看有没有你不知道的。获取类型的成员Type类的GetMembe...

C#启动外部程序的问题(c#怎么启动)

IT&OT的深度融合是智能制造的基石。本公众号将聚焦于PLC编程与上位机开发。除理论知识外,也会结合我们团队在开发过程中遇到的具体问题介绍一些项目经验。在使用C#开发上位机时,有时会需要启动外部的一些...

全网最狠C#面试拷问:这20道题没答出来,别说你懂.NET!

在竞争激烈的C#开发岗位求职过程中,面试是必经的一道关卡。而一场高质量的面试,不仅能筛选出真正掌握C#和.NET技术精髓的人才,也能让求职者对自身技术水平有更清晰的认知。今天,就为大家精心准备了20道...

C#匿名方法(c#匿名方法与匿名类)

C#中的匿名方法是一种没有名称只有主体的方法,它提供了一种传递代码块作为委托参数的技术。以下是关于C#匿名方法的一些重要特点和用法:特点省略参数列表:使用匿名方法可省略参数列表,这意味着匿名方法...

C# Windows窗体(.Net Framework)知识总结

Windows窗体可大致分为Form窗体和MDI窗体,Form窗体没什么好细说的,知识点总结都在思维导图里面了,下文将围绕MDI窗体来讲述。MDI(MultipleDocumentInterfac...