In this article, we will learn some basic Python examples that are useful for beginners. This article also includes some basic questions asked in Python interviews. let's start! ! !
Using the Python tuple() method, we can convert a list into a tuple. Since tuples are immutable, we cannot update the list after converting to tuple.
The following program returns a list converted to a tuple using the tuple() function -
# input list inputList = ['hello', 'tutorialspoint', 'python', 'codes'] # converting input list into a tuple resultTuple = tuple(inputList) # printing the resultant tuple print(resultTuple) # Printing the type of resultant tuple print(type(resultTuple))
('hello', 'tutorialspoint', 'python', 'codes') <class 'tuple'>
NumPy arrays are more general than Python lists. NumPy arrays make reading and writing objects faster and more efficient.
The following program shows how to create an empty NumPy array and a Numpy garbage array of a given shape -
# importing NumPy module import numpy # Creating an empty NumPy array without shape array1 = numpy.array([]) # printing array print(array1) # Creating a NumPy array with given shape and garbage values array2 = numpy.empty(shape=(3,3)) print(array2)
[] [[4.14578705e-316 1.77863633e-322 0.00000000e+000] [0.00000000e+000 2.37663529e-312 7.87101931e-071] [3.88586518e-033 5.03180591e-091 1.20858772e+161]]
Python has a unique feature called negative indexing in arrays and lists.
Python allows "indexing from the end" , i.e. negative indexing.
This means that the last value in the sequence has an index of -1, the second to last value has an index of -2, and so on.
Negative indexing can be used to your advantage when you want to pick values from the end (right side) of the iterable.
"set" is a Python data type, a collection. It has been part of Python since version 2.4. A collection is a collection of distinct and immutable items that are not ordered in any particular way.
The following program returns the sum of the first 100 natural numbers -
# printing the sum of numbers from 1 to 100 print(sum(range(1,101)) )
5050
List | Tuple |
---|---|
Lists are editable, which means they may change. | Tuples are immutable, which means we cannot change the elements of the tuple. |
Listing is relatively slow. | Tuples outperform lists in terms of efficiency. |
Grammar -list = [40, tutorialspoint, 100] Copy after login |
Grammar -tuple = (40, tutorialspoint, 100) Copy after login |
虽然我们可以使用 Python 编写脚本,但它主要用作通用编程语言。
解释性语言是执行前不存在于机器代码中的任何脚本语言。因此,Python 是一种解释性语言。此外,由于它是一种解释性语言,因此在运行时运行之前无法将其转换为计算机可读代码。
PEP 是 Python 增强提案的缩写。它是格式化 Python 代码以提高可读性的指南集合。
装饰器仅用于向方法添加某些布局模式,而不影响函数的结构。装饰者通常在他们要改进的活动之前就被确定。在我们使用装饰器之前,我们必须首先定义它的函数,即装饰器函数。
然后编写将在其中实现装饰器函数的函数,并将装饰器函数简单地放置在其上方。在这种情况下,@符号位于装饰器之前。
数字 - Python 最常见的内置数据结构是整数、复数和浮点数。
5, 2+3i, 3.5.
列表 - 列表是按特定顺序排序的对象的集合。列表的组成部分可以有多种数据类型。
[10, ‘tutorialspoint’, 4.89]
元组 - 元组是一组按特定顺序排列的项目。与列表不同,元组是不可变的,这意味着它们无法更改。
(10, ‘tutorialspoint’, 4.89)
字符串 - 字符串是字符的集合。可以使用单引号或双引号声明字符串。
“Hello ‘tutorialspoint’”.
集合 - 集合是不按任何特定顺序排列的不相关项的集合。
(5, 2, 8, 1)
字典 - 字典是键和值对的集合,其中每个值都可以通过其键访问。项目的顺序/顺序无关紧要。
{10:’tutorialspoint’, 20:python}
self 是一个类实例或一个对象。在 Python 中,这被明确指定为第一个参数。 Java 中的情况并非如此,它是可选的。局部变量有助于区分类的方法和属性。
类的 self 变量对应于 init 方法中新创建的对象,但它指的是该实体的方法可以在该类的其他方法中调用。
break - 当满足条件时,循环终止,控制权转移到下一条语句。
pass - 当代码块需要在语法上有效但您不想运行它时,请使用此 pass 语句。本质上,这是一个空动作。当它被执行时,什么也没有发生。
继续 - 当满足指定条件时,控制被发送到循环的开头,允许跳过循环当前执行的某些部分。
要将字符串转换为小写,请使用lower()函数。
以下程序将字符串中的每个字符转换为小写字母 -
# input string inputString = 'TUTORIALSPOINT' # converting each character of the string into lowercase # using the lower() function print("Converting every character of the string into lowercase:") print(inputString.lower())
Converting every character of the string into lowercase: tutorialspoint
在本文中,我们学习了 16 个不同的 Python 示例。这些也是 Python 面试的关键问题。
The above is the detailed content of What are some Python examples for beginners?. For more information, please follow other related articles on the PHP Chinese website!