Home Backend Development Python Tutorial Python数据类型学习笔记

Python数据类型学习笔记

Jun 10, 2016 pm 03:06 PM
python type of data

带你走进数据类型

一:整数、浮点数

Python中整数和浮点数的定义以及运算和C++都是一样的,我在这里就不需多说了,我就说明一点:Python相对于C/C++而言,定义整数没有int 和 long long 这些区分的,直接赋值即可。这就体现出了Python简洁的功能。

二:布尔值、空值

首先我们先谈谈Python中的布尔值True和False。在Python中可以直接使用True和False(请注意大小写),也可以通过表达式计算出True或者False。

另外,还有三种常见的布尔运算符and、or和not以及三目运算符and...or...。

Python中还有一种特殊的值:空值,用None表示。

这里注意两个地方:第一,None注意大小写(不能写成none);第二,None不等于0,因为0是有意义的呀,None是空值。

三:列表

列表,顾名思义,就是列出来一个表以表示出某种信息。比如,我们现在需要存储两个人的名字张三和李四,那么我们可以用列表(用方括号表示)来存储它。

是不是感觉不是很陌生,亲?对,表面看起来这和C++中一维数组没有什么区别嘛。我们在学习的时候可以这样相互关联起来加强记忆,同时也要注意不同之处,列表中的元素可以是不同的数据类型,比如张三今年24岁,我们也可以用列表来存储张三的姓名和年龄这两个属性值(分别是字符串和整数吧)。

常用序列的操作:

1、索引

刚才就有说到,列表就是通过列一个表来表示序列而显示信息,那么我们首先要想到的就是怎么样来遍历这个表呢?接下来,我们学习索引。

列表中的所有元素都是有编号的—从0开始递增(和C++没区别吧),我们就可以通过编号来访问列表了。

请注意一点:编号从0开始递增指的是列表中元素从左往右而言,最后一个元素就是列表中倒数第一个元素,所以Python就提供了这样一种机制,可以通过编号为负数来访问列表,如果编号为-2,就表示访问的是列表中倒数第二个元素。

2、分片

现在考虑这样一个问题,如果我们想要打印列表中连续一个区间怎么办?通过循环遍历,然后打印区间里的每一个元素?这样做也行,不过Python给我们提供了更好的办法,那就是分片。

分片通过冒号相隔的两个索引来实现,第一个索引是需要提取部分的第一个元素的编号,第二个索引是需要提取的连续区间最后一个元素的下一个元素在列表中的编号。简而言之,就是左闭右开的思想。

3、加乘

两个列表可以通过‘+'来合并,一个列表也可以通过乘以一个整数表示输出这个列表多少次。

list函数:把参数转换为列表的表示形式,比如我们通过把不能修改的字符串转换为列表的话,就可以实现修改了。

基本的列表操作

元素赋值:改变列表是很容易的,我们用索引标记来为某个特定的、位置明确的元素赋值。

分片赋值:程序可以一次性为多个元素赋值,这样就很方便辣。

删除元素:从列表中删除元素也是很easy的,使用del语句轻松搞定。同理呀,我们也可以分片删除元素。

基本的列表方法

append:用于在列表末尾添加新的元素。

count:统计某个元素在列表中出现的次。

extend:可以在列表末尾一次性添加另一个列表的多个值,看起来很像连接操作。但是两者有区别的:连接操作不会改变原两个列表,而extend操作是扩展一个列表的,对原列表有影响滴。

index:用于从列表中查找某个元素第一次出现的索引位置。

insert:用于将元素插入到列表中某一个位置上。

pop:移除列表中某一个元素,默认为最后一个,并返回该元素的值。

remove:用于移除列表中某个元素的第一个匹配项,和pop的区别在于remove没有返回值。

reverse:将列表的元素翻转。

sort:当然是对列表中元素进行排序啦。

高级排序:对sort加入参数实现自定义排序。

四:元组

元组和列表一样,都是一种序列,不同的是,元组的元素不能修改(随便提一句,字符串也是一样的。不信?你可以试试啦)。元组是用圆括号把元素括起来的,元素之间用逗号分隔。空元组就是一对圆括号来表示。

注意一点:用元组存储一个元素的时候我们需要在唯一的这一个元素后面多加一个逗号。因为34和(34)在Python中是一样的,所以(34)不能表示为元组,而(34,)才是元组。

tuple函数:把参数转换为元组的形式,可以和list函数联系一下。

以上就是本文的全部内容,详细为大家介绍了Python数据类型,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles