Detailed introduction to python types (type)
Empty (None)
None can be used to indicate that the value of a certain variable is missing, similar to null in other languages.
Like other null values: 0, [] and empty string, Boolean variables give False instead of True.
1 |
|
The result is:
1 |
|
When a function does not return any value, it returns None:
1 2 |
|
The result is:


1 2 |
|
1 |
|


1 2 |
|
1 2 |
|


1 |
|
1 2 |
|


1 |
|
1 |
|


1 2 3 |
|
1 2 3 |
|

1 2 3 |
|

Tuples are very similar to lists, but they cannot be changed. You can create a new tuple with parentheses, or without...:
1 |
|
1 |
|
空元组用()新建。
元组的运行速度比列表快
其他使用方法和列表类似。
列表切片(List Slices)
列表切片是一种检索列表值的高级方法。基本的切片方法是用两个被冒号分开的整数来索引列表。这样可以从旧列表返回一个新列表。
1 |
|
结果是:


1 2 3 |
|
跟range的参数相似,第一的下标的值会包括,但不包括第二个下标的值。
如果第一个下标省略,默认从头开始,
如果第二个下标省略,默认到结尾结束。
切片同样可以用于元组。
切片也有第三个参数,决定了步长。第一二个分别决定了开头与结尾。
1 2 3 4 |
|
结果是:
1 2 3 |
|
参数是复数的话就倒着走。-1是倒数第一,-2是倒数第二,第三个参数为负就会倒着切,这时候第一个参数和第二个参数就要倒着看了,也就是第二个参数变成了开始,第一个变成了结尾(因此-1会使整个列表倒序)
1 |
|
结果是:


1 2 3 |
|
列表解析(List Comprehensions)
这是一种快速创建遵循某些规则的列表的方法:
1 |
|
结果是:


1 |
|
也可以包含if statement 加强限定条件。
1 |
|
结果是:


1 |
|
1 |
|
结果是:


1 |
|
range的范围过大会超出内存的容量引发MemoryError
String Formatting
为了使string和non-string结合,可以把non-string转化为string然后再连起来。
string formatting提供了一种方式,把non-string嵌入到string里,用string的format method来替换string里的参数。
1 2 |
|
format里的参数和{}里的参数是对应的。{}的参数是format()里参数的下标
参数被命名这种情况也是可以的:
1 |
|
结果是:


1 |
|
Useful Functions
Python 内置了许多有用的函数
join ,用一个string充当分隔符把一个由string组成的列表连起来。
1 |
|
结果是:


1 |
|
replace,用一个string 取代另一个。
1 |
|
结果是:


1 |
|
startwith和endwith,判断是否是由……开头或结束:
1 |
|
结果是:


1 2 |
|
lower和upper可以改变string的大小写
1 |
|
结果是:


1 2 |
|
split的作用于join 相反,他可以按某个string为分隔符将一串string分开并成为列表的形式。
1 |
|
结果是:
1 |
|
有关数学的一些函数有:最大值max,最小值min,绝对值abs,约等数round(第二个参数可以决定保留几位小数),对列表里的数求和用sum等:
1 |
|
结果是:


1 2 3 4 5 |
|
all和any可以把列表当成参数,然后返回True或 False,
1 |
|
1 |
|
all和any的区别是,all需要所有的值都满足,any只需要有一个满足就行了。
枚举(enumerate),字面意思,把列表中的值按顺序一个一个列出来。
1 |
|
结果是:


1 2 3 4 5 |
|
The above is the detailed content of Detailed introduction to python types (type). 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





Setting up a wireless network is common, but choosing or changing the network type can be confusing, especially if you don't know the consequences. If you're looking for advice on how to change the network type from public to private or vice versa in Windows 11, read on for some helpful information. What are the different network profiles in Windows 11? Windows 11 comes with a number of network profiles, which are essentially sets of settings that can be used to configure various network connections. This is useful if you have multiple connections at home or office so you don't have to set it all up every time you connect to a new network. Private and public network profiles are two common types in Windows 11, but generally

An error occurs when ubuntu mounts a mobile hard disk: mount: unknownfilesystemtype'exfat'. The processing method is as follows: Ubuntu13.10 or install exfat-fuse: sudoapt-getinstallexfat-fuseUbuntu13.04 or below sudoapt-add-repositoryppa:relan/exfatsudoapt-getupdatesudoapt-getinstallfuse- exfatCentOS Linux mount exfat format USB disk error solution to load extfa in CentOS

The usage of the Type keyword in Go includes defining new type aliases or creating new structure types. Detailed introduction: 1. Type alias. Use the "type" keyword to create an alias for an existing type. This alias does not create a new type, but only provides a new name for the existing type. Type aliases can improve code. The readability of the code makes the code clearer; 2. Structure type. Use the "type" keyword to create a new structure type. The structure is a composite type that can be used to define custom types containing multiple fields. etc.

Part 1 Let’s talk about the nature of Python sequence types. In this blog, let’s talk about Python’s various “sequence” classes and the three built-in commonly used data structures – list, tuple and character. The nature of the string class (str). I don’t know if you have noticed it, but these classes have an obvious commonality. They can be used to save multiple data elements. The most important function is: each class supports subscript (index) access to the elements of the sequence, such as using SyntaxSeq[i]. In fact, each of the above classes is represented by a simple data structure such as an array. However, readers familiar with Python may know that these three data structures have some differences: for example, tuples and strings cannot be modified, while lists can.

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

Go functions can return multiple values of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

It’s great to use dynamic language for a while, and the code is reconstructed in the crematorium. I believe you must have heard this sentence. Like unit testing, although it takes a small amount of time to write code, it is very worthwhile in the long run. This article shares how to better understand and use Python's type hints. 1. Type hints are only valid at the syntax level. Type hints (introduced since PEP3107) are used to add types to variables, parameters, function parameters, and their return values, class properties, and methods. Python's variable types are dynamic and can be modified at runtime to add type hints to the code. It is only supported at the syntax level and has no impact on the running of the code. The Python interpreter will ignore the type hints when running the code. Therefore the type

C++ functions have the following types: simple functions, const functions, static functions, and virtual functions; features include: inline functions, default parameters, reference returns, and overloaded functions. For example, the calculateArea function uses π to calculate the area of a circle of a given radius and returns it as output.
