python 教程 第二章、 类型
第二章、 类型
常量
5,1.23,9.25e-3,’This is a string’,”It’s a string!”
1) 数
整数:2
长整数:
浮点数:3.23,52.3E-4
复数:-5+4j,2.3-4.6j
ac =-8.33 +1.2j
print ac.real #-8.33
print ac.imag #1.2
print ac.conjugate() #(-8.33-1.2j)
二进制:0b1000
八进制:0o307
十六进制:0xFF
2) 字符串
单引号(')
'Quote me on this'
双引号(")
"What's your name?"
三引号('''或""") 可以在三引号中自由的使用单引号和双引号
'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''
原始字符串(前缀r或R)raw strings
r"Newlines are indicated by \n"。
Unicode字符串(前缀u或U)
u"This is a 中文 string."
Byte strings(in 3.0)
S = b'spam'
字符串的方法
name = 'Swaroorp' # This is a string object
print name.encode('latin-1') #Swaroorp
print name.startswith('Swa') #True
print name.endswith('spam') #False
print name.find('war') #1
print name.find('warx') #-1
print name.rstrip() #remove whitespace #Swaroorp
print name.isdigit() #False
print name.replace('oo', 'xx') #Swarxxrp
print name.upper() #swaroorp
print name.lower() #swaroop
print name.split('r') #['Swa', 'oo', 'p']
print map(ord, name) #[83, 119, 97, 114, 111, 111, 114, 112]
print map(chr, (map(ord, name))) #['S', 'w', 'a', 'r', 'o', 'o', 'r', 'p']
print '-'.join(name) #S-w-a-r-o-o-r-p
print 'spam' in name #False
for x in name: print(x) #s w a r o o r p)
print [c * 2 for c in name] #['SS', 'ww', 'aa', 'rr', 'oo', 'oo', 'rr', 'pp']
了解这些方法的完整列表,请参见help(str)。
注意:
1.字符串不可变
2.字符串自动连接. 'What\'s' 'your name?'= "What's your name?"。
3.没有Char类型
4.一定要用自然字符串处理正则表达式,否则会需要使用很多的反斜杠
Format方法
类似参数传递
>>> template = '{0}, {1} and {2}' # By position
>>> template.format('spam', 'ham', 'eggs')
'spam, ham and eggs'
>>> template = '{motto}, {pork} and {food}' # By keyword
>>> template.format(motto='spam', pork='ham', food='eggs')
'spam, ham and eggs'
>>> template = '{motto}, {0} and {food}' # By both
>>> template.format('ham', motto='spam', food='eggs')
'spam, ham and eggs'
3) 转义符(\)
'What\'s your name?'
行末的单独一个反斜杠表示字符串在下一行继续
4) 布尔型
True False
"spam" True
"" False
[] False
{} False
1 True
0.0 False
None False
5) 变量
标识符命名
1. 字母或下划线开头.
2. 其他为字母,下划线,数字。
3.大小写敏感。
python 教程 第二章、 类型的更多相关文章
- 学习opencv中文版教程——第二章
学习opencv中文版教程——第二章 所有案例,跑起来~~~然而并没有都跑起来...我只把我能跑的都尽量跑了,毕竟看书还是很生硬,能运行能出结果,才比较好. 越着急,心越慌,越是着急,越要慢,越是陌生 ...
- javascript进阶教程第二章对象案例实战
javascript进阶教程第二章对象案例实战 一.学习任务 通过几个案例练习回顾学过的知识 通过案例练习补充几个之前没有见到或者虽然讲过单是讲的不仔细的知识点. 二.具体实例 温馨提示 面向对象的知 ...
- [ABP教程]第二章 图书列表页面
Web应用程序开发教程 - 第二章: 图书列表页面 关于本教程 在本系列教程中, 你将构建一个名为 Acme.BookStore 的用于管理书籍及其作者列表的基于ABP的应用程序. 它是使用以下技术开 ...
- [Python笔记][第二章Python序列-tuple,dict,set]
2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...
- [python笔记][第二章Python序列-list]
2016/1/27学习内容 第二章 Python序列-list list常用操作 list.append(x) list.extend(L) list.insert(index,x) list.rem ...
- [Python笔记][第二章Python序列-复杂的数据结构]
2016/1/27学习内容 第二章 Python序列-复杂的数据结构 堆 import heapq #添加元素进堆 heapq.heappush(heap,n) #小根堆堆顶 heapq.heappo ...
- Cobalt Strike系列教程第二章:Beacon详解
上周更新了Cobalt Strike系列教程第一章:简介与安装,文章发布后,深受大家的喜爱,遂将该系列教程的其他章节与大家分享,提升更多实用技能! 第二章:Beacon详解 一.Beacon命令 大家 ...
- [Learn Android Studio 汉化教程]第二章:Android Studio概述(一)
[Learn Android Studio ]第二章:Android Studio概述(一) Android Studio是一个视窗化的开发环境.为了充分利用有限的屏幕空间,不让你束手束脚,Andro ...
- python基础教程-第二章-列表和元组
本章将引入一个新的概念,:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在 一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构 ...
随机推荐
- mysql 序列号生成器 (自定义函数)
https://yq.aliyun.com/articles/42600 http://bbs.csdn.net/topics/360203885 http://www.tuicool.com/art ...
- C++中回调(CallBack)的使用方法(其实就是类方法指针,我觉得你的方法易用性不好,虽然原理正确)
回调函数是一个很有用,也很重要的概念.当发生某种事件时,系统或其他函数将会自动调用你定义的一段函数.回调函数在windows编程使用的场合很多,比如Hook回调函数:MouseProc,GetMsgP ...
- CoreData使用方法二:NSFetchedResultsController实例操作与解说
学习了NSFetchedResultsController.才深深的体会到coredata的牛逼之处.原来Apple公司弄个新技术.不是平白无故的去弄,会给代码执行到来非常大的优点.coredata不 ...
- php中如何获取数组长度
php获取数组的长度的方法 一.总结 一句话总结:count方法和sizeof方法 二.php获取数组的长度的方法 php获取数组长度的方法: 一). 获取一维数组的方法: 1.count.sizeo ...
- Android 设置图片 Bitmap任意透明度
两种思路,第一种思路是通过对Bitmap进行操作,将Bitmap的像素值get到一个int[]数组里,因为在android里Bitmap通常是ARGB8888格式,所以最高位就是A通道的值,对齐进行改 ...
- 切换-5.7-传统复制切换成GTID复制
1.基本环境: Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.5 ...
- centos / Linux 服务环境下安装 Redis 5.0.3
原文:centos / Linux 服务环境下安装 Redis 5.0.3 1.首先进入你要安装的目录 cd /usr/local 2.下载目前最新稳定版本 Redis 5.0.3 wget http ...
- 【u022】车的放置
[问题描述] [题解] 先考虑一个最简单的情况.如一个n*n的棋盘.然后要放k个车. 我们可以先选出k行即C(n,k); 然后在列上对这k个棋子进行一次全排列即A(n,k); 比如k = 4;N=5 ...
- Effective C++ 条款14
在资源管理器中小心copying行为 上节是对资源的管理说明.有时候我们不能依赖于shared_ptr或者auto_ptr,所以我们须要自己建立一个资源管理类来管理自己的资源. 比如建立一个类来管理M ...
- TensorFlow 学习(十三)—— tf.app.flags
flags = tf.app.flags FLAGS = flags.FLAGS flags.DEFINE_integer('num_hidden_layers', 3, 'number of hid ...