What are some Python examples for beginners?
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! ! !
How to create a tuple from a list?
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.
Example
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))
Output
('hello', 'tutorialspoint', 'python', 'codes') <class 'tuple'>
What is a NumPy array?
NumPy arrays are more general than Python lists. NumPy arrays make reading and writing objects faster and more efficient.
In Python, what are some ways to create an empty NumPy array and a Numpy array of a given shape?
Example
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)
Output
[] [[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]]
What are negative indexes in Python?
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.
What is the Python data type SET and how to use it?
"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.
How to print the sum of all numbers from 1 to 100?
Example
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)) )
Output
5050
What is the difference between lists and tuples in Python?
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 编写脚本,但它主要用作通用编程语言。
Python 是一种解释型编程语言。解释一下。
解释性语言是执行前不存在于机器代码中的任何脚本语言。因此,Python 是一种解释性语言。此外,由于它是一种解释性语言,因此在运行时运行之前无法将其转换为计算机可读代码。
什么是 pep 8?
PEP 是 Python 增强提案的缩写。它是格式化 Python 代码以提高可读性的指南集合。
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}
Python 中的 self 是什么?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
