python—day02_基本数据类型
1,字符串
- 移除空白
- 分割
- 长度
- 索引
- 切片
1)移除空白
"""
S.strip([chars]) -> str Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
name = " gong fan "
print("***%s***" % (name))
print("***%s***" % type(name))
new_name = name.strip()
print("***%s***" % (new_name))
C:\Users\\AppData\Local\Programs\Python\Python35\python.exe D:/PycharmPro/day01/hello.py
*** gong fan ***
***<class 'str'>***
***gong fan*** Process finished with exit code
2)分割
"""
S.split(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
""" 常用
>>> u = "www.doiido.com.cn" #使用默认分隔符
>>> print u.split()
['www.doiido.com.cn'] #以"."为分隔符
>>> print u.split('.')
['www', 'doiido', 'com', 'cn'] #分割0次
>>> print u.split('.',)
['www.doiido.com.cn'] #分割一次
>>> print u.split('.',)
['www', 'doiido.com.cn'] #分割两次
>>> print u.split('.',)
['www', 'doiido', 'com.cn'] #分割两次,并取序列为1的项
>>> print u.split('.',)[]
doiido #分割最多次(实际与不加num参数相同)
>>> print u.split('.',-)
['www', 'doiido', 'com', 'cn'] #分割两次,并把分割后的三个部分保存到三个文件
>>> u1,u2,u3 = u.split('.',)
>>> print u1
www
>>> print u2
doiido
>>> print u3
com.cn
3)长度
比如a = 'abc'
print len(a)
4)索引
info = 'abcd'
start = info[0]##开头的第一个字母也就是a
zhongjian = info[1:2]##中间的字母b
end = info[-1]##结尾的第一个字母d
5)切片
>>> name = "gongfan"
>>> str = name[:]
>>> print str
gong
>>>
2,列表
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 循环
- 包含
1)追加
""" L.append(object) -> None -- append object to end """
>>> shop_list = ["apple", "water"]
>>> shop_list.append("meet")
>>> print shop_list
['apple', 'water', 'meet']
>>>
2)删除
li = [1,2,3,4,5,6]
# 1.使用del删除对应下标的元素
del li[2]
# li = [1,2,4,5,6]
# 2.使用.pop()删除最后一个元素
li.pop()
# li = [1,2,4,5]
# 3.删除指定值的元素
li.remove(4)
# li = [1,2,5]
3)循环
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit
4)包涵
>>> l = ['aa','bcb', 'bcc']
>>> b = 'bbc'
>>> b in l
False
>>> c = 'aa'
>>> c in l
True
>>
3,元祖
- 索引
- 切片
- 循环
- 长度
- 包含
4,字典
- 索引
- 新增
- 删除
- 键、值、键值对
- 循环
- 长度
1)索引
>>> mydict={'ada':'', 'Bill':'', 'Candy':''}
>>> mydict['Bill']
''
>>>
2)新增
>>> dict1['a']= #第一种
>>> dict1
{'a': }
#第二种:setdefault方法
>>> dict1.setdefault('b',) >>> dict1
{'a': , 'b': }
3)删除
#删除指定键-值对
>>> dict1
{'a': , 'b': }
>>> del dict1['a'] #也可以用pop方法,dict1.pop('a')
>>> dict1
{'b': }
#清空字典
>>> dict1.clear()
>>> dict1 #字典变为空了
{}
#删除字典对象
>>> del dict1
>>> dict1
Traceback (most recent call last):
File "< interactive input>", line , in < module>
NameError: name 'dict1' is not defined
4)循环
In []: for key, value in d.items():
...: print key, 'corresponds to', value
5)长度
len(dict)
python—day02_基本数据类型的更多相关文章
- python 基础之数据类型
一.python中的数据类型之列表 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 二.列表常用操作 >切片>追加>插入>修改& ...
- Python学习 之 数据类型(邹琪鲜 milo)
1.Python中的数据类型:数字.字符串.列表.元组.字典 2.数字类型包括整型.长整型.浮点型.复数型 type(number):获取number的数据类型 整型(int):范围:-2,147,4 ...
- (八)python的简单数据类型和变量
什么是数据类型? 程序的本质就是驱使计算机去处理各种状态的变化,这些状态分为很多种. 例如英雄联盟游戏,一个人物角色有名字,钱,等级,装备等特性,大家第一时间会想到这么表示 名字:德玛西亚------ ...
- Python基础之数据类型
Python基础之数据类型 变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值 ...
- Python学习之数据类型
整数 Python可以处理任意大小的整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 用十六进制表示整数比较方便,十六进制用0x前缀和0-9,a-f表示,例如: ...
- python的组合数据类型及其内置方法说明
python中,数据结构是通过某种方式(例如对元素进行编号),组织在一起数据结构的集合. python常用的组合数据类型有:序列类型,集合类型和映射类型 在序列类型中,又可以分为列表和元组,字符串也属 ...
- python学习第九讲,python中的数据类型,字符串的使用与介绍
目录 python学习第九讲,python中的数据类型,字符串的使用与介绍 一丶字符串 1.字符串的定义 2.字符串的常见操作 3.字符串操作 len count index操作 4.判断空白字符,判 ...
- python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍
目录 python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍.md 一丶字典 1.字典的定义 2.字典的使用. 3.字典的常用方法. python学习第八讲,python ...
- python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍
目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...
随机推荐
- 用Python来揭秘吃瓜群众是如何看待罗志祥事件的
前言 最近娱乐圈可以说得上是热闹非凡,前有霸道总裁爱小三,正宫撕逼网红女,后有阳光大男孩罗志祥,被周扬青扒的名声扫地.贵圈的爱情故事,常人是难以理解的,正如贾旭明张康这段相声所说的这样,娱乐圈的爱情总 ...
- tcpdump常用抓包命令
主要语法 过滤主机/IP: tcpdump -i eth1 host 172.16.7.206 抓取所有经过网卡1,目的IP为172.16.7.206的网络数据 过滤端口: tcpdump -i ...
- 3年前的一个小项目经验,分享给菜鸟兄弟们(公文收发小软件:小技能 SmallDatetime)...
为什么80%的码农都做不了架构师?>>> 这个系统中的数据库有100多M,里面当然有很多表,我的每个表里,有几个字段,都是一样的例如 CreateUserID.CreateDat ...
- 父级元素绑定定mouseout和mouseover,移过子元素是都会触发
2019独角兽企业重金招聘Python工程师标准>>> mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标 ...
- CodeForces - 1245 C - Constanze's Machine
Codeforces Round #597 (Div. 2) Constanze is the smartest girl in her village but she has bad eyesigh ...
- Entity framework 加载多层相关实体数据
Entity framework有3种加载数据的方式:懒汉式(Lazy loading),饿汉式(Eager loading),显示加载(Explicit loading).3种加载方式有各自的优缺点 ...
- 带"反悔"的贪心-超市
题面:https://www.acwing.com/problem/content/description/147/ 超市里有N件商品,每个商品都有利润pi和过期时间di,每天只能卖一件商品,过期商品 ...
- C. Four Segments 前缀后缀
C. Four Segments 这种分成了三个节点一般都可以处理一下前缀处理一下后缀,或者处理一下前面的这个点,处理一下后面的这个点,然后再枚举中间这个点. 如果和中间这个点有关的,那么就可以换一下 ...
- N - 寿司晚宴 HYSBZ - 4197 状压dp
N - 寿司晚宴 HYSBZ - 4197 推荐题解 这个题目我觉得还是很难的,借助题解写出来的,题解还看了很久,现在还是不是很理解. 首先这个数比较大有500,如果直接就像这个题目S - Query ...
- HTTP GET | POST | DELETE请求
依赖: <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp& ...