Python Lesson 1 :About list for if and function

简介: python 关于list、for、if、和function的语法

1.1 安装Python3

在Mac或Linux系统使用命令 python3 -V(Uppercase "v"by the way )。如果已经安装,则显示版本,否则提示命令不存在,访问官方网址安装 www.python.org ;If Python 3 is missing from your computer, download a copy for your favorite OS from the website.

安装Python 3时会自动安装IDLE(集成开发环境Integrated development environment)

2.1 Create simple python lists

python 定义list,直接使用数组的方式 语法如下:


ls=['a',65,'b',66,['c',['d',68,'it's ok']]] 

list可以嵌套list,最大限度不能超过1000.

print(ls4[2]) 命令将输出 it's ok ;4 2 1 形同数组偏移量(offset)

2.1.1 it's time to iterate

for命令循环list元素,语法如下:


for target identifer in list :
    processing code 

循环输入ls变量的内容:

for list_item in ls :
    print(list_item)

则输出结果为:a 65 b 66 ['c',['d',68,'it's ok]],其中最后一部分作为整体输出。

2.1.2 if语法

if语法如下:

if some condition holds :
    the "true" suite
elif some other holds:
    the "true" suite
else :
    the "false" suite

如果需要将list中的list元素输出,可以使用if语句

for list_item in ls :
    if isinstance(list_item,list) :
        for nested_item in list_item :
            print(nested_item)
    else :
        print(list_item)

2.1.3 def定义函数

定义函数语法如下:

def function name (arguments) :
    function code suite

对于ls变量,包含两层list,如果要将list中的每层list元素单独输出,则需要在2.1.2的循环判断基础上在加一层循环判断,则代码混乱且不易读,可以将list循环输出定义为方法,然后递归调用即可,实现代码如下:


def recursion_ls(list_param) :
    for list_item in list_param :
        if isinstance(list_item,list) :
            recursion_ls(list_item)
        else :
            ''' 这里可以添加
            多行注释 '''
             
            print(list_item)
            
recursion_ls(ls) 

function is a module in python;python 提供了一个中央库(简称PyPI)用于管理第三方的模块,和 perl语言的CPAN一样。

The Python Package Index (or PyPI for short) provides a centralized repository for third-party Python modules on the Internet. When you are ready, you’ll use PyPI to publish your module and make your code available for use by others. And your module is ready, but for one important addition.
给module添加注释(''' 注释内容 '''):

使用''' This is the standard way to include a multiple-line comment in your code. '''给module添加注释

目录
相关文章
|
4天前
|
Python
|
8天前
|
Python
【Azure Function】发布 Python Function 到 Azure 成功,但是无法显示Function列表
"module not found" error: "Failure Exception: ImportError: libpq.so.5: cannot open shared object file: No such file or directory. Cannot find module."
Python控制流:条件语句(if, elif, else)
本文详细介绍了Python条件语句的使用方法,包括if、elif和else,以及条件表达式和多条件判断。通过一个综合详细的例子,我们展示了条件语句在实际编程中的应用。希望本文对您理解和应用Python条件语句有所帮助。
|
15天前
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
14 1
|
17天前
|
存储 索引 Python
【Python】已解决:IndexError: list index out of range
【Python】已解决:IndexError: list index out of range
28 1
|
18天前
|
机器学习/深度学习 数据处理 Python
【Python】已解决:FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated
【Python】已解决:FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated
15 0
|
1月前
|
存储 Python
Python中list, tuple, dict,set的区别和使用场景
Python中list, tuple, dict,set的区别和使用场景
|
1月前
|
Python
Python中的Map Function
Python中的Map Function
|
1月前
|
存储 安全 Python
Python List深度使用(四)
Python List 是 Python 中非常常用的一种数据类型,它通过数组实现,可以容纳任意类型的元素,并支持动态扩容。在使用 Python List 时,需要充分考虑其优缺点和性能特征,并避免频繁进行添加或删除操作。在多线程多进程中使用 Python List,需要特别注意线程安全和同步问题。通过深入了解 Python List 的特性和使用方法,我们可以更好地应用它来实现我们的需求。
23 4
|
1月前
|
Python
Python List深度使用(二)
在 Python 中,列表的复制过程中,浅拷贝和深拷贝是两种不同的方式。浅拷贝只是复制了一层引用,而不会复制引用所指向的对象,因此在修改原始列表中嵌套的可变对象时,新列表也会随之改变。深拷贝则会递归复制所有的对象,包括嵌套的可变对象,因此不受原始列表的影响。如果列表中没有嵌套的可变对象,或者对新列表的改动不影响原始列表,则可以使用浅拷贝。浅拷贝是指在复制一个对象时,仅复制对象本身和对象内部第一层的引用,而不会复制其内部的对象。深拷贝是指在复制一个对象时,不仅复制对象本身,还会递归复制其内部的所有对象。
13 3