一些适合初学者的Python示例有哪些?

一些适合初学者的Python示例有哪些?

在这篇文章中,我们将学习一些对初学者有用的基本Python示例。本文还包括一些Python面试中提出的基本问题。让我们开始吧!!!

如何从列表中创建元组?

使用 Python tuple() 方法,我们可以将列表转换为元组。由于元组是不可变的,因此在转换为元组后我们无法更新列表。

示例

以下程序返回使用 tuple() 函数将列表转换为元组 -

# input list inputList = ['hello', 'tutorialspoint', 'python', 'codes'] 1. converting input list into a tuple resultTuple = tuple(inputList) 1. printing the resultant tuple print(resultTuple) 1. Printing the type of resultant tuple print(type(resultTuple)) 登录后复制