Python学习笔记一:数据类型与基本操作
2015-06-23 10:27
986 查看
Python学习笔记一:数据类型与基本操作
一、Python字符串
1.1 可以在三种引号内表示字符串
'This is a string'
"This is a string"
"""This is a string"""
1.2 使用print函数
print("Hello World! ")
Hello World!
1.3 转义字符\后的特殊字符会被看成字符
>>> print("I said, \"Don't do it")
I said, "Don't do it
1.4 可是使用三引号分行打印,使用单引号或双引号则不行
>>> print("""Rose are red
... Violets are blue
... I just printed multiple lines
... And you did too!""")
Rose are red
Violets are blue
I just printed multiple lines
And you did too!
1.5 使用双引号与换行转义字符实现分行打印
>>> print("Roses are red \n Violets are blue \n I just printed multiple lines \n And you did too!")
Roses are red
Violets are blue
I just printed multiple lines
And you did too!
1.6 连接字符串
>>> "John" + "Everyman"
'JohnEveryman'
>>> "John" "Everyman"
'JohnEveryman'
1.7 使用格式说明符%s连接字符串
>>> "John Q. %s" % ("Public")
'John Q. Public'
>>> "John %s%s" % ("Every","man")
'John Everyman'
1.8 %s连接字符串并限定长度
>>> "%-10s %s %10s" % ("John","Every","Man")
'John Every Man'
第一个字符串在右边填充6个字符,第三个字符串在左右填充7个字符。
1.9 将其他类型转化为字符串
>>> "This is a string" + str(4)
'This is a string4'
二、Python数值与计算
2.1 使用type函数查看数值类型
>>> type(1)
<type 'int'>
>>> type(1.1)
<type 'float'>
2.2 数字格式说明符
>>> "Including an integer works with %%d like this : %d" % 10
'Including an integer works with %d like this : 10'
>>> "An integer converted to a float with %%f: %f" % 5
'An integer converted to a float with %f: 5.000000'
>>> "A normal float with %%f: %f" % 1.2345
'A normal float with %f: 1.234500'
>>> "A really large number with %%E: %E" % 6.789E10
'A really large number with %E: 6.789000E+10'
>>> "Controlling the number of decimal places shown: %.02f" % 25.101010101
'Controlling the number of decimal places shown: 25.10'
最后一个例子表示四舍五入到小数点后面两位。
2.3 除法与取余运算
>>> 6 / 4
1
>>> 6 % 4
2
2.4 除法结果处理实例
>>> print("%f" % (5/3))
1.000000
>>> print("%.2f" % (5/3))
1.00
>>> print("%.2f" % (415 * 20.2))
8383.00
>>> print("%0.f" % (415 * 20.2))
8383
2.5 真除法与取底除法
>>> from __future__ import division
>>> 10/4
2.5
>>> 10//4
2
2.5 二进制、八进制与十六进制
2.5.1 表示方法
>>> abin=0b0011
>>> abin
3
>>> aoct=0o12
>>> aoct
10
>>> ahex=0xE
>>> ahex
14
2.5.2 格式说明符
>>> print('Octal uses the letter "o" lowercase. %d %o' % (10,10))
Octal uses the letter "o" lowercase. 10 12
>>> print('Hex uses the letter "x" or "X". %d %x %X' % (10,10,10))
Hex uses the letter "x" or "X". 10 a A
2.6 转换函数
>>> oct(64),hex(64),bin(64)
('0100', '0x40', '0b1000000')
>>> int(0x40),float(0b1000000)
(64, 64.0)
三、变量、元组、列表及字典
3.1 变量名称只是实际对象的一个指针
>>> a=1
>>> b=a
>>> print(a)
1
>>> print(b)
1
>>> a=a+1
>>> print(a)
2
>>> print(b)
1
3.2 元组——不可更改的数据序列
3.2.1 创建和使用元组
>>> a = ("string","filled","by a","tuple")
>>> print(a)
('string', 'filled', 'by a', 'tuple')
3.2.2 元组的索引(从0开始)及长度
>>> a = ("first", "second", "third")
>>> print("The first element of the tuple is %s" % a[0])
The first element of the tuple is first
>>> print("%d" % len(a))
3
3.2.3 嵌套元组及索引
>>> a = ("first", "second", "third")
>>> b = (a, "b's second element")
>>> print ("%s" %b[1])
b's second element
>>> print("%s" % b[0][0])
first
>>> print("%s" % b[0][1])
second
>>> print("%s" % b[0][2])
3.2.4 创建一个元素的元组(必须加",")
>>> single_element_tuple = ("the sole element",)
3.2 列表——可更改的数据序列
3.2.1 创建使用列表
>>> breakfast = ["coffee", "tea", "toast" , "egg"]
>>> count = 0
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is coffee
>>> count = 1
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is tea
>>> count = 2
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is toast
>>> count = 3
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is egg
3.2.2 添加一个元素
>>> breakfast = ["coffee", "tea", "toast" , "egg"]
>>> breakfast.append("waffles")
>>> count = 4
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is waffles
3.2.3 为列表添加多个元素
>>> breakfast
['coffee', 'tea', 'toast', 'egg', 'waffles']
>>> breakfast.extend(["juice", "decaf", "oatmeal"])
>>> breakfast
['coffee', 'tea', 'toast', 'egg', 'waffles', 'juice', 'decaf', 'oatmeal']
3.3 字典——键值存储数据
3.3.1 创建与使用字典
>>> menus_specials = {}
>>> menus_specials["breakfast"] = "Canadian ham"
>>> menus_specials["lunch"] = "tuna surprise"
>>> menus_specials["dinner"] = "Cheesebruger Deluxe"
or
>>> menu_specials = {"breakfast" : "sausage and eggs",
... "lunch" : "split pea soup and garlic bread",
... "dinner" : "2 hot dogs and onion rings"}
>>> print(menu_specials["breakfast"])
sausage and eggs
3.3.2 获取字典的所有键以及所有值
>>> hungry = menu_specials.keys()
>>> print(hungry)
['lunch', 'breakfast', 'dinner']
>>> starving = menu_specials.values()
>>> print(starving)
['s
bba1
plit pea soup and garlic bread', 'sausage and eggs', '2 hot dogs and onion rings']
字典中键都是不同的,只能通过键找值,如果需要通过值找键,则需要测试所有键。
3.3.3 字典中定义相同的键,值将被覆盖
>>> menu2 = {"breakfast" : "spam", "breakfast" : "ham", "dinner" : "hotdog"}
>>> menu2.get("breakfast")
'ham'
3.3.4 其他类型
None:仅仅是一个名称,没有指向任何对象。
True False:1 0 的特殊表示。
3.4 序列(字符串、元组或列表)的共有属性
3.4.1 访问最后一个元素
sequence[len(sequence)-1]或者sequence[-1]
>>> sequence = (1,2,3,4,5)
>>> sequence[-1]
5
>>> sequence[-2]
4
3.4.2 序列分片
>>> s="abcde"
>>> s[-1]
'e'
>>> s[1:5]
'bcde'
>>> l = [0,1,2,3,4]
>>> l[1:3]
[1, 2]
3.4.3 append方法与extend方法
>>> t = ("a", "b", "c")
>>> l = []
>>> l.append(t)
>>> l
[('a', 'b', 'c')]
>>> t = ("a", "b", "c")
>>> l = []
>>> l.extend(t)
>>> l
['a', 'b', 'c']
3.4.4 列表的pop方法
>>> l = ["a", "b", "c", "d", "e"]
>>> l.pop()
'e'
>>> l
['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>> l.pop(1)
'c'
>>> l
['b', 'd']
3.4.5 set方法删除列表的重复元素
>>> l
['a', 'b', 'c', 'b', 'c']
>>> l2 = list(set(l))
>>> l2
['a', 'c', 'b']
一、Python字符串
1.1 可以在三种引号内表示字符串
'This is a string'
"This is a string"
"""This is a string"""
1.2 使用print函数
print("Hello World! ")
Hello World!
1.3 转义字符\后的特殊字符会被看成字符
>>> print("I said, \"Don't do it")
I said, "Don't do it
1.4 可是使用三引号分行打印,使用单引号或双引号则不行
>>> print("""Rose are red
... Violets are blue
... I just printed multiple lines
... And you did too!""")
Rose are red
Violets are blue
I just printed multiple lines
And you did too!
1.5 使用双引号与换行转义字符实现分行打印
>>> print("Roses are red \n Violets are blue \n I just printed multiple lines \n And you did too!")
Roses are red
Violets are blue
I just printed multiple lines
And you did too!
1.6 连接字符串
>>> "John" + "Everyman"
'JohnEveryman'
>>> "John" "Everyman"
'JohnEveryman'
1.7 使用格式说明符%s连接字符串
>>> "John Q. %s" % ("Public")
'John Q. Public'
>>> "John %s%s" % ("Every","man")
'John Everyman'
1.8 %s连接字符串并限定长度
>>> "%-10s %s %10s" % ("John","Every","Man")
'John Every Man'
第一个字符串在右边填充6个字符,第三个字符串在左右填充7个字符。
1.9 将其他类型转化为字符串
>>> "This is a string" + str(4)
'This is a string4'
二、Python数值与计算
2.1 使用type函数查看数值类型
>>> type(1)
<type 'int'>
>>> type(1.1)
<type 'float'>
2.2 数字格式说明符
>>> "Including an integer works with %%d like this : %d" % 10
'Including an integer works with %d like this : 10'
>>> "An integer converted to a float with %%f: %f" % 5
'An integer converted to a float with %f: 5.000000'
>>> "A normal float with %%f: %f" % 1.2345
'A normal float with %f: 1.234500'
>>> "A really large number with %%E: %E" % 6.789E10
'A really large number with %E: 6.789000E+10'
>>> "Controlling the number of decimal places shown: %.02f" % 25.101010101
'Controlling the number of decimal places shown: 25.10'
最后一个例子表示四舍五入到小数点后面两位。
2.3 除法与取余运算
>>> 6 / 4
1
>>> 6 % 4
2
2.4 除法结果处理实例
>>> print("%f" % (5/3))
1.000000
>>> print("%.2f" % (5/3))
1.00
>>> print("%.2f" % (415 * 20.2))
8383.00
>>> print("%0.f" % (415 * 20.2))
8383
2.5 真除法与取底除法
>>> from __future__ import division
>>> 10/4
2.5
>>> 10//4
2
2.5 二进制、八进制与十六进制
2.5.1 表示方法
>>> abin=0b0011
>>> abin
3
>>> aoct=0o12
>>> aoct
10
>>> ahex=0xE
>>> ahex
14
2.5.2 格式说明符
>>> print('Octal uses the letter "o" lowercase. %d %o' % (10,10))
Octal uses the letter "o" lowercase. 10 12
>>> print('Hex uses the letter "x" or "X". %d %x %X' % (10,10,10))
Hex uses the letter "x" or "X". 10 a A
2.6 转换函数
>>> oct(64),hex(64),bin(64)
('0100', '0x40', '0b1000000')
>>> int(0x40),float(0b1000000)
(64, 64.0)
三、变量、元组、列表及字典
3.1 变量名称只是实际对象的一个指针
>>> a=1
>>> b=a
>>> print(a)
1
>>> print(b)
1
>>> a=a+1
>>> print(a)
2
>>> print(b)
1
3.2 元组——不可更改的数据序列
3.2.1 创建和使用元组
>>> a = ("string","filled","by a","tuple")
>>> print(a)
('string', 'filled', 'by a', 'tuple')
3.2.2 元组的索引(从0开始)及长度
>>> a = ("first", "second", "third")
>>> print("The first element of the tuple is %s" % a[0])
The first element of the tuple is first
>>> print("%d" % len(a))
3
3.2.3 嵌套元组及索引
>>> a = ("first", "second", "third")
>>> b = (a, "b's second element")
>>> print ("%s" %b[1])
b's second element
>>> print("%s" % b[0][0])
first
>>> print("%s" % b[0][1])
second
>>> print("%s" % b[0][2])
3.2.4 创建一个元素的元组(必须加",")
>>> single_element_tuple = ("the sole element",)
3.2 列表——可更改的数据序列
3.2.1 创建使用列表
>>> breakfast = ["coffee", "tea", "toast" , "egg"]
>>> count = 0
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is coffee
>>> count = 1
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is tea
>>> count = 2
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is toast
>>> count = 3
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is egg
3.2.2 添加一个元素
>>> breakfast = ["coffee", "tea", "toast" , "egg"]
>>> breakfast.append("waffles")
>>> count = 4
>>> print("Today's breakfast is %s" % breakfast[count])
Today's breakfast is waffles
3.2.3 为列表添加多个元素
>>> breakfast
['coffee', 'tea', 'toast', 'egg', 'waffles']
>>> breakfast.extend(["juice", "decaf", "oatmeal"])
>>> breakfast
['coffee', 'tea', 'toast', 'egg', 'waffles', 'juice', 'decaf', 'oatmeal']
3.3 字典——键值存储数据
3.3.1 创建与使用字典
>>> menus_specials = {}
>>> menus_specials["breakfast"] = "Canadian ham"
>>> menus_specials["lunch"] = "tuna surprise"
>>> menus_specials["dinner"] = "Cheesebruger Deluxe"
or
>>> menu_specials = {"breakfast" : "sausage and eggs",
... "lunch" : "split pea soup and garlic bread",
... "dinner" : "2 hot dogs and onion rings"}
>>> print(menu_specials["breakfast"])
sausage and eggs
3.3.2 获取字典的所有键以及所有值
>>> hungry = menu_specials.keys()
>>> print(hungry)
['lunch', 'breakfast', 'dinner']
>>> starving = menu_specials.values()
>>> print(starving)
['s
bba1
plit pea soup and garlic bread', 'sausage and eggs', '2 hot dogs and onion rings']
字典中键都是不同的,只能通过键找值,如果需要通过值找键,则需要测试所有键。
3.3.3 字典中定义相同的键,值将被覆盖
>>> menu2 = {"breakfast" : "spam", "breakfast" : "ham", "dinner" : "hotdog"}
>>> menu2.get("breakfast")
'ham'
3.3.4 其他类型
None:仅仅是一个名称,没有指向任何对象。
True False:1 0 的特殊表示。
3.4 序列(字符串、元组或列表)的共有属性
3.4.1 访问最后一个元素
sequence[len(sequence)-1]或者sequence[-1]
>>> sequence = (1,2,3,4,5)
>>> sequence[-1]
5
>>> sequence[-2]
4
3.4.2 序列分片
>>> s="abcde"
>>> s[-1]
'e'
>>> s[1:5]
'bcde'
>>> l = [0,1,2,3,4]
>>> l[1:3]
[1, 2]
3.4.3 append方法与extend方法
>>> t = ("a", "b", "c")
>>> l = []
>>> l.append(t)
>>> l
[('a', 'b', 'c')]
>>> t = ("a", "b", "c")
>>> l = []
>>> l.extend(t)
>>> l
['a', 'b', 'c']
3.4.4 列表的pop方法
>>> l = ["a", "b", "c", "d", "e"]
>>> l.pop()
'e'
>>> l
['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>> l.pop(1)
'c'
>>> l
['b', 'd']
3.4.5 set方法删除列表的重复元素
>>> l
['a', 'b', 'c', 'b', 'c']
>>> l2 = list(set(l))
>>> l2
['a', 'c', 'b']
相关文章推荐
- 4-python学习——数据操作
- 3-python学习——变量
- 2-python学习——hello world
- [转] Python风格规范
- 1 python学习——python环境配置
- 端午节前一周周记
- Python 函数式编程学习
- Python中线程编程之threading模块的使用详解
- Python os模块常用方法1
- python
- python3.4学习笔记(三) idle 清屏扩展插件
- python3.4学习笔记(二) 类型判断,异常处理,终止程序
- python特殊函数(id, dir, type, isinstance, issubclass, is)
- web python -- WSGI接口POST请求
- web python -- WSGI接口GET请求
- python相关面试题
- web python -- WSGI接口响应
- Python数据类型之数字类型、字符串类型
- python日志输出—logging直接代码配置
- 首部讲Python爬虫电子书 Web Scraping with Python