探索python基本术语

本文将深入探讨Python编程中的核心术语,包括身份标识、关键字、运算符、对象编号和类型、序列、集合类型、映射、可迭代对象、迭代器、简单和复合语句,以及内置类型、函数和方法。此外,还会讨论可变与不可变对象,以及注释和文档字符串的重要性。
摘要由CSDN通过智能技术生成

Python基本术语 (Python basic terms)

  1. Identifiers

    身份标识
  2. keywords

    关键字
  3. Operators

    经营者
  4. Object Id,Object Type

    对象编号,对象类型
  5. Sequences

    顺序
  6. Set Types

    集合类型
  7. Mappings

    对应
  8. Iterable

    可迭代的
  9. Iterator

    迭代器
  10. Simple statements

    简单的陈述
  11. Compound statements

    复合陈述
  12. Built-in types

    内置类型
  13. Built-in functions

    内建功能
  14. Built-in methods

    内置方法
  15. Mutable, Immutable

    可变的,不可变的
  16. Comments

    注释
  17. Docstring

    文件串

身份标识 (Identifiers)

Python identifiers are names given to identify variables, class, function, module, packages, methods, instance variables, or any other object.Identifiers start with lowercase or uppercase letter A-Z a-z , or underscore.It can then followed by zero or more letters, digits, and underscore.Special characters and space are not allowed in identifiers.

Python标识符是用于标识变量,类,函数,模块,包,方法,实例变量或任何其他对象的名称。标识符以小写或大写字母AZ azunderscore ,然后可以跟着零个或多个字母,数字和下划线。标识符中不得包含特殊字符和空格。

Naming Convention:

命名约定:

  1. If an identifier starts with an underscore, then it is a private identifier

    如果标识符以下划线开头,则它是私有标识符

    Refer to my story for

    参考我的故事

    underscores in python.

    在python中下划线

  2. Class names should normally use the CapWords convention.

    类名通常应使用CapWords约定。

    Class names should normally use the CapWords convention.class BankAccount:

    类名通常应使用CapWords约定。 class BankAccount:

  3. Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.

    模块应使用简短的全小写名称。 如果模块名称可以提高可读性,则可以在模块名称中使用下划线。
  4. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

    尽管不鼓励使用下划线,但Python软件包也应使用短的全小写名称。
  5. Function names should be lowercase, with words separated by underscores as necessary to improve readability.

    函数名称应小写,必要时用下划线分隔单词,以提高可读性。

    Function names should be lowercase, with words separated by underscores as necessary to improve readability.def withdrawal():

    函数名称应小写,必要时用下划线分隔单词,以提高可读性。 def withdrawal():

    Function names should be lowercase, with words separated by underscores as necessary to improve readability.def withdrawal():def bank_withdrawal():

    函数名称应小写,必要时用下划线分隔单词,以提高可读性。 def withdrawal(): def bank_withdrawal():

  6. Variable names follow the same convention as function names.

    变量名遵循与函数名相同的约定。

    Variable names follow the same convention as function names.a=[1,2,3]

    变量名遵循与函数名相同的约定。 a=[1,2,3]

    Variable names follow the same convention as function names.a=[1,2,3]d={‘a’:1}

    变量名遵循与函数名相同的约定。 a=[1,2,3] d={'a':1}

    Variable names follow the same convention as function names.a=[1,2,3]d={‘a’:1}colors=[‘red’,’blue’]

    变量名遵循与函数名相同的约定。 a=[1,2,3] d={'a':1} colors=['red','blue']

  7. Method names and also instance variables should be lowercase with words separated by underscores as necessary to improve readability.

    方法名称以及实例变量应使用小写字母,并在必要时使用下划线分隔单词,以提高可读性。
  8. Use one leading underscore only for non-public methods and instance variables.

    仅对非公共方法和实例变量使用前导下划线。

Names to avoid:

应避免的名称:

Never use the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter oh), or ‘I’ (uppercase letter eye) as single-character variable names. In some fonts, these characters are indistinguishable from the numerals one and zero.

切勿将字符“ l”(小写字母el),“ O”(大写字母oh)或“ I”(大写字母eye)用作单字符变量名称。 在某些字体中,这些字符与数字1和零没有区别。

Example 1: Space is given in the function name

示例1:函数名称中给定空格

def bank withdrawal():pass#Output:SyntaxError: invalid syntax

Example 2:Special characters given in function name

示例2:函数名称中给定的特殊字符

def withdrawal%():pass#Output:SyntaxError: invalid syntax

关键字: (Keywords:)

Keywords are reserved words that cannot be used as ordinary identifiers.

关键字是保留字,不能用作普通标识符。

Image for post
: Python Documentation :Python文档

经营者 (Operators)

The following operators are supported in python.

python支持以下运算符。

Image for post
Python Documentation Python文档

对象编号,对象类型 (Object Id,Object Type)

Every object has an identity, type, and value.Object Id is the object address in the memory.id() function returns the object’s identityObject type determines the type of the object like list,dict, tuple, string etc.type() function returns the object type

每个对象都有一个标识,类型和值。 对象ID是内存中的对象地址。 id()函数返回对象的标识对象类型确定对象的类型,如列表,字典,元组,字符串等type()函数返回对象的类型

list1=[1,2,3]
print (id(list1))#Output:39270408print (type(list1))#Output:<class 'list'>tuple1=(1,2,3)
print (id(tuple1))#Output:39405192print (type(tuple1))#Output:<class 'tuple'>dict1={1:'a',2:'b'}
print (id(dict1))#Output:22432336print (type(dict1))#Output:<class 'dict'>

顺序: (Sequences:)

Sequences represent finite ordered set indexed by non-negative numbers.

序列表示由非负数索引的有限有序集。

  1. Immutable Sequences

    不变序列

    Strings

    弦乐

    Tuples

    元组

    Bytes

    字节数

str1="python"print (str1[1])#Output: ytup1=(1,2,3)
print (tup1[1])#Output:2

2. Mutable SequencesListsByteArrays

2.可变序列 ListsByteArrays

lis1=[1,2,3]
print (lis1[2]) #Output:3

集合类型 (Set Types)

Set types represent unordered, finite sets of unique, immutable objects. Common uses for sets is to remove duplicates from the sequence and perform mathematical set operations like union, intersection, difference, symmetric difference.

集类型表示无序,有限的唯一,不可变对象集。 集合的常见用途是从序列中删除重复项,并执行数学集合操作,例如并集,交集,差,对称差。

  1. Sets

    套装

    Sets are immutable. Created by using a built-in set() constructor.Empty set is given as

    集是不可变的。 通过使用内置的set()构造函数创建。空集为

    set()

    set()

  2. Frozen sets

    冷冻套

    Frozen sets are immutable. Since it is immutable and also hashable, it can be used as dictionary keys

    冻结集是不可变的。 由于它是不可变的并且也是可哈希的,因此可以用作字典键

set1=set((1,2,3))
print (set1)#Output:{1,2,3}set2={1,2,3}
print (set2)#Output:{1,2,3}fro1=frozenset({1,2,3})
print (fro1)#Output:frozenset({1, 2, 3})

对应 (Mappings)

A mapping object maps values of one type (the key type) to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. A dictionary’s keys are almost arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity.

映射对象将一种类型(键类型)的值映射到任意对象。 映射是可变的对象。 当前只有一种标准映射类型,即dictionary 。 字典的键几乎是任意值。 唯一不可接受作为键的值类型是包含列表,字典或其他可变类型的值,这些列表或字典或其他可变类型通过值而不是对象标识进行比较。

d={'a':1,'b':2}

可迭代 (Iterables)

An object capable of returning its members one at a time. Iterables can be iterated upon using for loop.

一个能够一次返回其成员的对象。 可迭代对象可在使用for循环时进行迭代。

Example. List,set,tuple,dict,range object,file objects

例子 列表,集合,元组,字典,范围对象,文件对象

list1=[1,2,3]for i in list1:
print(i , end=" ") #Output:1 2 3set1={1,2,3}for i in set1:
print(i , end=" ")#Output:1 2 3tuple1=(1,2,3)for i in tuple1:
print(i , end=" ")#Output:1 2 3dict1={1:'a',2:'b'}for i in dict1:
print (i,end=" ")#Output:1 2 3r=range(1,4)for i in r:
print (i,end=" ")#Outpur:1 2 3

迭代器 (Iterators)

An object representing a stream of data. If an iterable contains the iter() function, we can convert to an iterator.

表示数据流的对象。 如果一个iterable包含iter()函数,我们可以转换为一个迭代器。

Image for post

We can iterate over iterators 1.Using for loop

我们可以遍历迭代器1.使用循环

We can iterate over the iterator. But it will iterate one time only. Attempting to iterate second time will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

我们可以遍历迭代器。 但是它只会迭代一次。 尝试进行第二次迭代只会返回上一次迭代过程中使用的相同的耗尽迭代器对象,使其看起来像一个空容器。

n1=[1,2,3]
n2=iter(n1)for i in n2:
print (i, end=" ") #Output: 1 2 3 print ("")#exhausted iterator returns an empty container
n3=list(n2)
print (n3)#Output: []

2.Using next() functionWhen the iterator is exhausted( no more data available) a StopIteration exception is raised instead.

2.使用next()函数当迭代器耗尽(没有更多可用数据)时,将引发StopIteration异常。

n1=[1,2,3]
n2=iter(n1)
print (next(n2))#Output: 1print (next(n2))#Output: 2print (next(n2))#Output: 3print (next(n2))#Output:StopIteration

Refer to my story Iterable vs Iterator

参考我的故事Iterable vs Iterator

简单的陈述 (Simple Statements)

A simple statement is comprised of a single logical line

一个简单的语句由一条逻辑线组成

Image for post
Python documentation Python文档

复合陈述 (Compound Statements)

Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause.

复合语句包含(其他组)其他语句; 它们以某种方式影响或控制其他语句的执行。复合语句由一个或多个“子句”组成。 子句由标题和“套件”组成。 特定复合语句的子句标题都处于相同的缩进级别。 每个子句头均以唯一标识的关键字开头,以冒号结尾。 套件是由子句控制的一组语句。

Image for post
Python Documentation Python文档

内置类型 (Built-in types)

The principal built-in types are numerics, sequences, mappings, classes, instances, and exceptions.

内置的主要类型是数字,序列,映射,类,实例和异常。

内建功能 (Built-in functions)

The Python interpreter has a number of built-in functions that are always available. A built-in function object is a wrapper around a C function. The number and type of arguments are determined by the C function.

Python解释器具有许多始终可用的内置函数。 内置函数对象是C函数的包装器。 参数的数量和类型由C函数确定。

Image for post
Python Documentation Python文档

内置方法 (Built-in methods)

Methods are called on an object. Its return type is None. It will modify the original object itself. But python function doesn't modify the object, it will return a new object.

方法是在对象上调用的。 其返回类型为None 。 它将修改原始对象本身。 但是python函数不会修改该对象,它将返回一个新对象。

Ex. List object has a built-in method sort and built-in function sorted()

例如 列表对象具有内置方法sort和内置函数sorted()

sort →This has to be called on an object (list) like list.sort()It will modify the original list. It won’t return anything.

sort→必须在像list.sort()这样的对象(列表)上list.sort()它,它将修改原始列表。 它不会返回任何东西。

sorted() -This is built in function. sorted(list)It will return a new sorted list. It won’t modify the original list.

sorted()-这是内置函数。 sorted(list)它将返回一个新的排序列表。 它不会修改原始列表。

#built-in methodslist1=[2,1,5,3,7,6,4]
print (list1.sort())#Output:Noneprint (list1)#Output:[1,2,3,4,5,6,7]
#built-in functions
list2=[2,1,5,3,7,6,4]
print (sorted(list2)) #Output:[1,2,3,4,5,6,7]
# It won't modify the original list.
print (list2)#Output:[2, 1, 5, 3, 7, 6, 4]

Built-in methods supported by list, tuple,dict, and set.

列表,元组,字典和集合支持的内置方法。

Image for post
Image Source: Author
图片来源:作者

Built-in methods supported by strings

字符串支持的内置方法

Image for post
Image Source: Author
图片来源:作者

可变对象 (Mutable objects)

Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.

价值可以改变的物体被认为是可变的 ; 创建后其值不可更改的对象称为不可变的

Mutable objects -List,dictionary,set

可变对象 -列表,字典,设置

Immutable objects -Strings, tuple

不可变对象-字符串,元组

评论 (Comment)

Comments in Python start with # and continue until the end of the line. Comments are important because it will improve the readability of the code. Comments in the program are for humans to read and understand and not for the computer to execute.

Python中的注释以#开头,直到行尾。 注释很重要,因为它可以提高代码的可读性。 该程序中的注释供人类阅读和理解,而不供计算机执行。

# sorting the given listcolors=['red','blue','yellow']
print (sorted(colors))

DocString (DocString)

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It allows programmers to add quick notes about the function/class. It can span multiple lines also. We can access the docstring using the __doc__ method.For consistency, always use “””triple double quotes””” around docstrings.

docstring是一个字符串文字,它作为模块,函数,类或方法定义中的第一条语句出现。 它允许程序员添加有关函数/类的快速注释。 它也可以跨越多行。 我们可以使用__doc__方法访问文档字符串。为了保持一致,请始终在文档字符串前后使用“”“三重双引号””””。

def add1(a,b):"""This function used to calculate subtraction of 2 numbers"""return a-b
print(add1.__doc__)#Output: This function used to calculate subtraction of 2 numbers

翻译自: https://medium.com/dev-genius/exploring-python-basic-terms-15ba62286c53

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值