python3 基本数据类型_1
不得已,要学习python3了,之前了解到py2与py3有很大不同,不过学起来才能感觉到,比如print。
不过,同样的代码,可以使用py3,py2执行,结果也相似,大家可以看看。
大概因为初学,还未找到巨大差异处,比如有些函数、方法在py3中已经被弃用了
代码如下:
#!urs/bin/python3
#coding:utf-8 #定义变量a,b,c并赋值
a,b,c=1,5.3,"sub2020"
#输出变量赋值类型
print (type(a),type(b),type(c)) #输出字符串c的长度
print ("len(c):" ,len(c))
#输出c
print (c)
#输出c的 第一个[0] 到: 倒数第二个[-1] 之间的字符
print ("(c[0:-1]):" ,(c[0:-1]))
#输出第一个字符
print ("(c[0]):" ,(c[0]))
#输出c 索引[1]-[6]之间的字符
print ("(c[1:6]):", (c[1:6])) #Python中的字符串有两种索引方式,从左往右以0开始,从右往左以-1开始
#输出 索引[-1]-[-4]之间的字符
print ("(c[-1:-4]):", (c[-1:-4]))
#输出 索引[-4]-[-1]之间的字符
print ("(c[-4:-1]):", (c[-4:-1]))
#输出索引[2]以后的字符
print ("(c[2:]):", (c[2:]))
#输出2次c
print ("(c*2):", (c*2))
#输出两个字符串链接后的结果
print ('(c+"WWW"):', (c+"WWW")) print ("*"*60)
list1=['sub',2020,'sub2020',20.20,'http']
list2=['www',[1,2,3]] print ("list1 :" ,list1)
print ("list2 :" ,list2)
print ("len(list1) :" , len(list1))
print ("len(list2) :", len(list2))
print ("(list1[0:4]) :", (list1[0:4]))
# print ("(list1[-1]) :" (list1[-1])) 输出错误,list无法用负值索引
print ("list2*2 :", list2*2)
print ("list1+list2 :", list1+list2) #List中的元素是可以改变的
list1[4]="new"
print ("new list1 :" ,list1) print ("*"*60)
tuple1=('sub',2020,'sub2020',20.20,'http')
tuple2=('www',[1,2,3]) print ("tuple1 :" ,tuple1)
print ("tuple2 :" ,tuple2)
print ("len(tuple1) :" , len(tuple1))
print ("len(tuple2) :", len(tuple2))
print ("(tuple1[0:4]) :", (tuple1[0:4]))
#元组有两种索引方式,从左往右以0开始,从右往左以-1开始
print ("tuple1[-1] :" ,(tuple1[-1]))
print ("tuple2*2 :", tuple2*2)
print ("tuple1+tuple2 :", tuple1+tuple2) #tuple中的元素不可改变
#tuple1[4]="new"
#print ("new tuple1 :" ,tuple1)
py3 output:
<class 'int'> <class 'float'> <class 'str'>
len(c): 7
sub2020
(c[0:-1]): sub202
(c[0]): s
(c[1:6]): ub202
(c[-1:-4]):
(c[-4:-1]): 202
(c[2:]): b2020
(c*2): sub2020sub2020
(c+"WWW"): sub2020WWW
************************************************************
list1 : ['sub', 2020, 'sub2020', 20.2, 'http']
list2 : ['www', [1, 2, 3]]
len(list1) : 5
len(list2) : 2
(list1[0:4]) : ['sub', 2020, 'sub2020', 20.2]
list2*2 : ['www', [1, 2, 3], 'www', [1, 2, 3]]
list1+list2 : ['sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]]
new list1 : ['sub', 2020, 'sub2020', 20.2, 'new']
************************************************************
tuple1 : ('sub', 2020, 'sub2020', 20.2, 'http')
tuple2 : ('www', [1, 2, 3])
len(tuple1) : 5
len(tuple2) : 2
(tuple1[0:4]) : ('sub', 2020, 'sub2020', 20.2)
tuple1[-1] : http
tuple2*2 : ('www', [1, 2, 3], 'www', [1, 2, 3])
tuple1+tuple2 : ('sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3])
py2 output
(<type 'int'>, <type 'float'>, <type 'str'>)
('len(c):', 7)
sub2020
('(c[0:-1]):', 'sub202')
('(c[0]):', 's')
('(c[1:6]):', 'ub202')
('(c[-1:-4]):', '')
('(c[-4:-1]):', '')
('(c[2:]):', 'b2020')
('(c*2):', 'sub2020sub2020')
('(c+"WWW"):', 'sub2020WWW')
************************************************************
('list1 :', ['sub', 2020, 'sub2020', 20.2, 'http'])
('list2 :', ['www', [1, 2, 3]])
('len(list1) :', 5)
('len(list2) :', 2)
('(list1[0:4]) :', ['sub', 2020, 'sub2020', 20.2])
('list2*2 :', ['www', [1, 2, 3], 'www', [1, 2, 3]])
('list1+list2 :', ['sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]])
('new list1 :', ['sub', 2020, 'sub2020', 20.2, 'new'])
************************************************************
('tuple1 :', ('sub', 2020, 'sub2020', 20.2, 'http'))
('tuple2 :', ('www', [1, 2, 3]))
('len(tuple1) :', 5)
('len(tuple2) :', 2)
('(tuple1[0:4]) :', ('sub', 2020, 'sub2020', 20.2))
('tuple1[-1] :', 'http')
('tuple2*2 :', ('www', [1, 2, 3], 'www', [1, 2, 3]))
('tuple1+tuple2 :', ('sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]))
Traceback (most recent call last):
File "basic_data_type.py", line 66, in <module>
tuple2[1]=[1,2,3,4]
TypeError: 'tuple' object does not support item assignment ***Repl Closed***
quote:http://www.runoob.com/python3/python3-data-type.html
python3 基本数据类型_1的更多相关文章
- Python3 基本数据类型注意事项
Python3 基本数据类型 教程转自菜鸟教程:http://www.runoob.com/python3/python3-data-type.html Python中的变量不需要声明.每个变量在使用 ...
- Python3 的数据类型
Python3 的数据类型 整形,浮点型,布尔类型 类型转换 int() 整形 采用截断的方式即向下取整,比如 a=5.5 int (a) 返回值为5 怎样才能使int()按照"四舍五入&q ...
- Python3 常见数据类型的转换
Python3 常见数据类型的转换 一.数据类型的转换,你只需要将数据类型作为函数名即可 OCP培训说明连接:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16H ...
- 3. Python3 基本数据类型
Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型& ...
- python003 Python3 基本数据类型
Python3 基本数据类型Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建.在 Python 中,变量就是变量,它没有类型,我们所说的"类型&qu ...
- 【Python学习】Python3 基本数据类型
参考学习地址:https://www.runoob.com/python3/python3-data-type.html Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用 ...
- Python3 基本数据类型
Python中的变量不需要声明,每个变量使用前必须赋值,变量赋值后才会被创建,在Python中变量就是变量,它没有类型.我们所说的"类型"是变量所指的内存中对象的类型. 等号(=) ...
- python3基本数据类型
python3的基本数据类型: Number(数字).String(字符串).List(列表).Tuple(元组).Set(集合).Dictionary(字典) 不可变数据类型(3 个):Number ...
- python3 bytes数据类型探讨
python3中str和bytes分开了,那么bytes与str之间到底是什么关系呢?下面从表现形式.处理方式.存储形式三个方面来阐述其区别 1. 在字符串前面加上b,就表示bytes数据类型 s1 ...
随机推荐
- 怎样减少 Android 应用包 60% 的大小?
简评: 应用的大小也是用户体验的一个重要方面,而减少 Android 应用安装包大小其实一点也不复杂. 对于移动应用来说,应用安装包的大小当然是越小越好.特别是对于一些欠发达地区,你不希望用户因为手机 ...
- (转) oracle清空数据库脚本
在开发过程中,可能经常需要重新初始化数据库,在初始化之前,我们肯定希望不再有以前的老表.存储过程等用户对象,用下面的教本就可以做到这一点: BEGIN FOR rec IN (SELE ...
- Zen Cart 138 在PHP5.3环境下出现的Fatal error: Cannot redeclare date_diff()
Zen Cart 138 在PHP5.3环境下出现的Fatal error: Cannot redeclare date_diff() in includes/functions/functions_ ...
- UILabel的行间距,字间距处理
啥都不说了,直接上代码,做了一个Category #import <UIKit/UIKit.h> @interface UILabel (ChangeLineSpaceAndWordSpa ...
- ZROI 19.08.12模拟赛
传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. "我发现问题的根源是大家都不会前缀和."--敦爷 A 敦爷spj写错了,差点把蒟蒻swk送走 \(50pts:\) ...
- 一种循环C字符数组的骚操作
#include <stdio.h> #include <stdlib.h> int main() { char wenwa[] = "程劲小盆友在做什么" ...
- JMeter 功能挖掘之 WEB 文件导出
前言 自从写从0构建自动化测试平台(一)之技术选型开始,在工作中Get新技能就非常想郑重的记录下来,方便自己查阅:相信很多人都有这种感触:平时问题解决后,没有及时记录,下次遇到类似问题,需要花同等的成 ...
- 两种常用的数据交换格式:XML和JSON
不同编程语言之间的数据传输,需要一种通用的数据交换格式,它需要简洁.易于数据储存.快速读取,且独立于各种编程语言.我们往往传输的是文本文件,比如我们都知道的csv(comma seperated va ...
- 通过PPA存储库在UBUNTU或LINUX MINT中安装ORACLE JAVA 8 [JDK8]
http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html sudo add-apt-repository ...
- Spring Cloud Stream教程(二)主要概念
Spring Cloud Stream提供了一些简化了消息驱动的微服务应用程序编写的抽象和原语.本节概述了以下内容: Spring Cloud Stream的应用模型 Binder抽象 持续的发布 - ...