(一)数字

Python3中的数字类型分为3种,分别是整型,浮点型以及复数。

Python2种的数字类型分为4种,分别是整型,长整型,浮点型以及复数。

其中长整型时Python2为应对位数较大的而设置的类型,整数最后是一个大写或小写的L。

主要函数:

int()  将字符串等类型转换成数字类型

(二)字符串

Python中的字符串使用 ' 或 “ 来创建。

主要函数:

1、replace()  使用新的字符串替换原来的字符串

#!/sur/bin/python3

str = "www.w3cschool.cc"
print("菜鸟教程旧地址:",str)
print("菜鸟教程新地址:",str.replace("w3cschool.cc","runoob.com")) str = "this is string example....wow!!!"
print(str.replace("is","was",)

2、find()  检查字符串中是否包含子字符串,如果有则返回该子字符串的索引值,没有则返回-1

#!/sur/bin/python3

str1 = "Runoob example....wow!!!"
str2 = "exam"; print(str1.find(str2))
print(str.find(str2,5))

3、join()  用于将序列中的元素以指定的字符连接生成新的字符串

#!/usr/bin/python3

s1 = "-"
s2 = ""
seq = ("h","e","l","l","o")
print(s1.join( seq ))
print(s2.join( seq ))

4、startswith()  检查字符串是否以制定字符串开头,是则返回true  否则返回false

#!/sur/bin/python3

str = "this is string example....wow!!!"
print(str.startswith( 'this' ))
print(str.startswith( 'sting',8 ))
print(str.startswith( 'this',2,4 ))

5、split()  通过指定字符对字符串进行切片

#!/syr/bin/python3

str = "this is string example...wow!!!"
print(str.split( ))
print(str.split('i',1))
print(str.split('w'))

6、format()  格式化字符串

#!/sur/bin/python

site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site))

7、upper以及lower

将字符串中的大小写互相转换

(三)列表

序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

格式如下:

#!/usr/bin/python3

list = ['Google', 'Runoob', 1997, 2000]

主要函数:

1、append()  在列表尾部添加新的对象

#!/sur/bin/python3

list1 = ['Google','Runoob','Taobao']
list1.append('Baidu')
print(list1)

2、extend()  用于在列表末尾一次性追加另一个序列中的多个值。

#!/usr/bin/python3

list1 = ['Google', 'Runoob', 'Taobao']
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2) # 扩展列表
print ("扩展后的列表:", list1)

3、insert()  用于将指定对象插入列表的指定位置。

#!/usr/bin/python3

list1 = ['Google', 'Runoob', 'Taobao']
list1.insert(1, 'Baidu')
print ('列表插入元素后为 : ', list1)

(四)元组

Python 的元组与列表类似,不同之处在于元组的元素不能修改。

格式如下:

#!/usr/bin/python3

tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )

(五)字典

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})

格式如下:

#!/usr/bin/python3

dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])

1、get()  返回指定键的值,如果值不在字典中返回默认值。

#!/usr/bin/python3 

dict = {'Name': 'Runoob', 'Age': 27}
print(dict.get(''Age))

2、update()  把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里。

#!/usr/bin/python3

dict = {'Name': 'Runoob', 'Age': 7}
dict2 = {'Sex': 'female' } dict.update(dict2)
print ("更新字典 dict : ", dict)

3、items()  以列表返回可遍历的(键, 值) 元组数组。

#!/usr/bin/python3

dict = {'Name': 'Runoob', 'Age': 7}

print ("Value : %s" %  dict.items())

(六)  集合

Python的集合类型是一个无序的不重复序列。

#!/sur/bin/python3

hobby = {'play','study','see movie'}
print(hobby)

Python基础(二)数据类型的更多相关文章

  1. python基础(二)----数据类型

    Python基础第二章 二进制 字符编码 基本数据类型-数字 基本数据类型-字符串 基本数据类型-列表 基本数据类型-元组 可变.不可变数据类型和hash 基本数据类型-字典 基本数据类型-集合 二进 ...

  2. python 基础二-----数据类型和控制语句

    一.数据类型: 1)数据类型 1.整数(int) 2.浮点数(float) 3.字符串(string) 4.列表(list) 5. 元组(tuple) 6.字典(dict): key和value是一一 ...

  3. Python基础之数据类型

    Python基础之数据类型 变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值 ...

  4. Python 基础 二

    Python 基础 二 今天对昨天学习的Python基础知识进行总结,学而不思则惘,思而不学则殆! 一.先对昨天学习的三大循环的使用情况进行总结: 1.while循环的本质就是让计算机在满足某一条件的 ...

  5. 第二章:python基础,数据类型

    """第二章:python基础,数据类型2.1 变量及身份运算补充2.2 二进制数2.3 字符编码每8位所占的空间位一个比特,这是计算机中最小的表示单位.每8个比特组成一 ...

  6. python基础一数据类型之字典

    摘要: python基础一数据类型之一字典,这篇主要讲字典. 1,定义字典 2,字典的基础知识 3,字典的方法 1,定义字典 1,定义1个空字典 dict1 = {} 2,定义字典 dict1 = d ...

  7. python基础二(基本数据类型)

    python的基本数据类型:数字.字符串.列表.元祖.字典.集合 一.基本数据类型 1.1 数字int 数字主要是用来计算用的,使用方法并不多. # bit_length() 当十进制用二进制表示的时 ...

  8. 进击的Python【第二章】:Python基础(二)

    Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...

  9. 第一节 Python基础之数据类型(整型,布尔值,字符串)

    数据类型是每一种语言的基础,就比如说一支笔,它的墨有可能是红色,有可能是黑色,也有可能是黄色等等,这不同的颜色就会被人用在不同的场景.Python中的数据类型也是一样,比如说我们要描述一个人的年龄:小 ...

  10. python 基础之数据类型

    一.python中的数据类型之列表 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 二.列表常用操作 >切片>追加>插入>修改& ...

随机推荐

  1. code vs 3376 符号三角形

    3376 符号三角形  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 如下图是由14个“+”和14个“-”组 ...

  2. N天学习一个Linux命令之top

    用途 查看机器负载以及进程资源占用情况,linux系统性能分析工具 用法 top -hv | -abcHimMsS -d delay -n iterations -p pid [, pid ...] ...

  3. [React Testing] Confidently Ship Production React Apps

    We want to make sure that when we ship new code, our users can use the application. The best way we' ...

  4. POJ题目1947 Rebuilding Roads(树形dp)

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9957   Accepted: 4537 ...

  5. codeforces 437D The Child and Zoo

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. 黑马程序猿——————java基础

    一.软件开发 软件是什么? 软件是简单的来说,计算机数据和指令的集合,数据(比方年龄,性别).指令及时告诉计算机怎样对他进行处理.计算机但是没有人那么聪明啊! 二.图形化界面(GUI),主要特点就是. ...

  7. Spring学习笔记——Spring事务仅仅对执行时异常回滚

    我们在使用Spring时候一般都知道事务在遇到异常的时候会回滚.岂不知Spring的事务默认仅仅有在发生执行时异常即:RunTimeException时才会发生事务,假设一个方法抛出Exception ...

  8. 哈理工2015暑假集训 zoj 2975 Kinds of Fuwas

    G - Kinds of Fuwas Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Subm ...

  9. light oj1074

    Description The people of Mohammadpur have decided to paint each of their houses red, green, or blue ...

  10. LeetCode208:Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...