Python(Package,Numeric,String,List,Tuple)

Package

  1. 自带(built-in)package和外部(external)package
    1.1 自带package举例: os; os.getwd()

  2. 外部package以及管理系统介绍: easy_install, pip (comes with Python 3.4)

  3. 环境变量中配置easy_install, pip

  4. 使用easy_install, pip安装package举例

import requests
import os

#os模块就是对操作系统进行操作,使用该模块必须先导入模块
#getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹)
print(os.getcwd())

#尝试获取某个网页
#名为 r 的 Response 对象
r=requests.get("http://www.baidu.com")

print(r.url) 
print(r.encoding)
print(r.text)

数据类型 Numeric & String

1.Python数据类型

1.1 总体:
numerics, sequences, mappings, classes, instances, and exceptions

1.2 Numeric Types:
int (包含boolean), float, complex

1.3
int: unlimited length;
float: 实现用double in C, 可查看 sys.float_info;
complex: real(实部) & imaginary(虚部),用z.real 和 z.imag来取两部分

1.4 具体运算以及法则参见:
https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

1.5 例子

import sys

a=3
b=4

c=5.6
d=8.0

e=complex(c,d)
f=complex(float(a),float(b))

print("a is type:",type(a)) #a is type: <class 'int'>
print("c is type:",type(c)) #c is type: <class 'float'>
print("e is type:",type(e)) #e is type: <class 'complex'>

print(a+b) #7
print(d/c) #1.4285714285714286
print(b/a) #1.3333333333333333
print(b//a)  #1
print(e)  #(5.6+8j)
print(e+f)  #(8.6+12j)

print(sys.float_info)
#sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, 
#min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, 
#mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

2.字符串:
一串字符
显示或者打印出来文字信息
导出
编码:# -- coding: utf-8 -- (放最最上面)
单引号,双引号,三引号
不可变(immutable)
Format字符串
age = 3
name = “Tom”
print(“{0} was {1} years old”.format(name, age))
联合:+: print(name + ” was ” + str(age) + ” years old”)
换行符: print(“What’s your name? \nTom”)

3.字面常量(literal constant):

可以直接以字面的意义使用它们:
如:6,2.24,3.45e-3, “This is a string”
常量:不会被改变

4.变量:
储存信息
属于identifier
identifier命名规则:
第一个字符必须是字母或者下划线
其余字符可以是字母,数字,或者下划线
区分大小写
如:合法:i, name_3_4, big_bang
不合法:2people, this is tom, my-name, >123b_c2

5.注释: #

6.缩进(Indentation)


数据结构列表 List

  1. print中的编码:
    编码:# -- coding: utf-8 --

  2. print中的换行
    print(“What’s your name? \nTom”)

  3. List
    创建
    访问
    更新
    删除
    脚本操作符
    函数方法

Code:

 # -*- coding: utf-8 -*-

    #创建一个列表

    number_list = [1, 3, 5, 7, 9]

    string_list = ["abc", "bbc", "python"]

    mixed_list = ['python', 'java', 3, 12]


    #访问列表中的值

    second_num = number_list[1]

    third_string = string_list[2]

    fourth_mix = mixed_list[3]

    print("second_num: {0} third_string: {1} fourth_mix: {2}".format(second_num, third_string, fourth_mix))

    #更新列表
    print("number_list before: " + str(number_list))

    number_list[1] = 30

    print("number_list after: " + str(number_list))

    #删除列表元素
    print("mixed_list before delete: " + str(mixed_list))

    del mixed_list[2]

    print("mixed_list after delete: " + str(mixed_list))

    #Python脚本语言

    print(len([1,2,3])) #长度 3
    print([1,2,3] + [4,5,6]) #组合
    print(['Hello'] * 4) #重复
    print(3 in [1,2,3]) #某元素是否在列表中 True

    #列表的截取
    abcd_list =['a', 'b', 'c', 'd'] 
    print(abcd_list[1]) #b
    print(abcd_list[-2]) #c 从右数第二个
    print(abcd_list[1:]) #['b','c','d']

    # 列表操作包含以下函数:
    # 1、cmp(list1, list2):比较两个列表的元素 
    # 2、len(list):列表元素个数 
    # 3、max(list):返回列表元素最大值 
    # 4、min(list):返回列表元素最小值 
    # 5、list(seq):将元组转换为列表 
    # 列表操作包含以下方法:
    # 1、list.append(obj):在列表末尾添加新的对象
    # 2、list.count(obj):统计某个元素在列表中出现的次数
    # 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
    # 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
    # 5、list.insert(index, obj):将对象插入列表
    # 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
    # 7、list.remove(obj):移除列表中某个值的第一个匹配项
    # 8、list.reverse():反向列表中元素
    # 9、list.sort([func]):对原列表进行排序

元组(tuple)

与list最大的区别:一旦创建,元素不能被删除和更改,但可以整体删除tuple

 创建
 访问
 删除
 脚本操作符
 函数方法
number_tuple=(1,2,3,4,5)
string_tuple=("aa","bb","cc","dd")
mixed_tuple=('python','java',12,3)

second_num=number_tuple[1]
third_string=string_tuple[2]
fourth_mixed=mixed_tuple[3]
print("second_num:{0} third_string:{1} forth_mixed:{2}".format(second_num,third_string,fourth_mixed))

#TypeError: 'tuple' object doesn't support item deletion
#del number_tuple[2]

del mixed_tuple

print(len((1,2,3))) #长度 3
print((1,2,3) + (4,5,6)) #组合 (1, 2, 3, 4, 5, 6)
print(('Hello') * 4) #重复 HelloHelloHelloHello
print(3 in (1,2,3)) #某元素是否在列表中 True

abcd_list =('a', 'b', 'c', 'd') 
print(abcd_list[1]) #b
print(abcd_list[-2]) #c 从右数第二个
print(abcd_list[1:]) #('b','c','d')

对比tuple.list

#创建只有一个元素的tuple,需要用逗号结尾消除歧义
a_tuple = (2,)

#tuple中的list
mixed_tuple = (1, 2, ['a', 'b'])

print("mixed_tuple: " + str(mixed_tuple))

mixed_tuple[2][0] = 'c'
mixed_tuple[2][1] = 'd'

print("mixed_tuple: " + str(mixed_tuple))

Tuple 是不可变 list。 一旦创建了一个 tuple 就不能以任何方式改变它。

Tuple 与 list 的相同之处

定义 tuple 与定义 list 的方式相同, 除了整个元素集是用小括号包围的而不是方括号。
Tuple 的元素与 list 一样按定义的次序进行排序。 Tuples 的索引与 list 一样从 0 开始, 所以一个非空 tuple 的第一个元素总是 t[0]。
负数索引与 list 一样从 tuple 的尾部开始计数。
与 list 一样分片 (slice) 也可以使用。注意当分割一个 list 时, 会得到一个新的 list ;当分割一个 tuple 时, 会得到一个新的 tuple。

Tuple 不存在的方法

您不能向 tuple 增加元素。Tuple 没有 append 或 extend 方法。
您不能从 tuple 删除元素。Tuple 没有 remove 或 pop 方法。
然而, 您可以使用 in 来查看一个元素是否存在于 tuple 中。

用 Tuple 的好处

Tuple 比 list 操作速度快。如果您定义了一个值的常量集,并且唯一要用它做的是不断地遍历它,请使用 tuple 代替 list。
如果对不需要修改的数据进行 “写保护”,可以使代码更安全。使用 tuple 而不是 list 如同拥有一个隐含的 assert 语句,说明这一数据是常量。如果必须要改变这些值,则需要执行 tuple 到 list 的转换。

Tuple 与 list 的转换

Tuple 可以转换成 list,反之亦然。内置的 tuple 函数接收一个 list,并返回一个有着相同元素的 tuple。而 list 函数接收一个 tuple 返回一个 list。从效果上看,tuple 冻结一个 list,而 list 解冻一个 tuple。

print(list(number_tuple)) # [1, 2, 3, 4, 5]
print(tuple([1,2,3])) #(1, 2, 3)

Tuple 的其他应用
一次赋多值
python
v = ('a', 'b', 'e')
(x, y, z) = v

解释:v 是一个三元素的 tuple, 并且 (x, y, z) 是一个三变量的 tuple。将一个 tuple 赋值给另一个 tuple, 会按顺序将 v 的每个值赋值给每个变量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值