#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/10/27 22:46
# @Author : lijunjiang
# @File : Exercise.py """实现1-100的所有的和"""
num = 0
for i in xrange(1, 101):
num += i
print(num) """实现1-500所有奇数的和"""
num = 0
for i in xrange(1, 501):
if 0 != (i % 2):
num += i
print(num)
#####################################################
num = 0
for i in xrange(1, 501):
if 0 == (i % 2):
continue
else:
num += i
print(num) """求1 + 2! + 3! + 4! + ...+20!"""
num = 0
n = 1
for i in xrange(1, 21):
n *= i
num += n
print(num) """对指定一个list进行排序"""
a = [2, 32, 43, 453, 54, 6, 576, 5, 7, 6, 8, 78, 7, 89]
a.sort()
print(a) """复习字典\字符串\list\tuple常用方法""" """通用方法""" str1 = 'abcdefg'
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
tuple1 = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
dict1 = {"name":"li", "age":20, "addr":"beijing"} #type() 返回变量数据类型
print(type(str1))
print(type(list1))
print(type(tuple1)) #dir() 查看对象都有哪些属性和方法
print(dir(str1))
print(dir(list))
print(dir(tuple1)) #help() 查看函数\对象\模块用途的详细说明
print(help(str1))
print(help(list1))
print(help(tuple1)) """适用于字符串 list tuple的方法"""
#len() 求序列的长度
print(len(str1))
print(len(list1))
print(len(tuple1)) # + 连接2个序列
print(str1 + '123')
print(list1 + [123, 234])
print(tuple1 + (123, 234)) # * 重复序列元素
print(str1 * 4)
print(list1 * 4)
print(tuple1 * 4) # in 判断元素是否在序列中
print('a' in str1)
print('b' in list1)
print('g' in tuple1) # max() 返回最大值
print(max(str1))
print(max(list1))
print(max(tuple1)) # min() 返回最小值
print(min(str1))
print(min(list1))
print(min(tuple1)) str1 = 'abcabc'
tuple1 = ('a', 'b', 'c', 'a', 'b', 'c')
list1 = ['a', 'b', 'c', 'a', 'b', 'c']
# count() 返回值出现的次数
print(str1.count('b'))
print(tuple1.count('b'))
print(list1.count('b'))
# insdex() 返回值的下标
print(str1.index('a'))
print(tuple1.index('a'))
print(list1.index('a')) """字符串方法""" str1 = 'abcabc' # find() 字符串中查找字符串,返回查找字符串中第一个字符的下标,未找到返回-1
print(str1.find('ca')) # replace(str1,str2) 替换 将str1替换为str2
print(str1.replace('ca', 'TT')) # split(srt) 分割 以str 为分割符,返回一个列表 类似shell中awk -F
print(str1.split('b')) # join(str) 以str连接字符串
print('||'.join(str1.split('b'))) # strip() 去除字符串两边的空格
# lstrip() 只去除左边空格 rstrip() 只去除右边空格
str1 = ' ab ca b '
print(str1)
print(str1.strip())
print(str1.rstrip())
print(str1.lstrip()) # format()
# print('str0 {0} str1{1}'.format(str0, str1)) 执行效率最高
print("hello {0}".format(str1)) """list 方法"""
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# list.append() 追加一个元素到列表最后
list1.append('append')
print(list1) # list.extend() 追加一个可迭代的对象到列表中
list1.extend(xrange(10, 15))
print(list1) # list.insert() 在某个下标前插入元素
list1.insert(1, 'insert')
print(list1) #list.remvoe() 删除某个元素
list1.remove("insert")
print(list1) # list.sort() 给列表排序
list1.sort()
print(list1) # list.reverse() 倒序
list1.reverse()
print(list1) # list.pop()
# 删除并返回某个下标的元素(不指定下标默认为最后一个)
list1.pop()
print(list1)
list1.pop(0)
print(list1) """字典的方法"""
dict1 = {"name":"li", "age":20, "addr":"beijing"} # get() 根据键获取值
print(dict1.get('name')) # setdefault() 设置默认值,当KEY有值时,保持原值,当key没有值则设置该key的值
print(dict1.setdefault('name'))
print(dict1.setdefault('name'), 'jiang')
print(dict1.setdefault('ip', 'localhost'))
print(dict1) # keys() 获的字典的Key (一次性取出所有)
print(dict1.keys()) # iterkeys() 获得key的值 (对象 且逐个取出 效率高)
print(dict1.iterkeys())
print(type(dict1.iterkeys()))
# for keys in dict1.iterkyes():
# print(keys) # values() 获取所有key的值(一次性取出所有)
print(dict1.values()) # iteritems() 获得一个对象(逐个取出) 效率高
print(dict1.iteritems())
print(type(dict1.iteritems())) # pop() 删除并返回某个键的值,必须输入一个存在的key,否则会报错
print(dict1.pop('ip'))
print(dict1) # fromkeys()
list1 = ["a", "b", "c", "d"]
k = {}
n = k.fromkeys(list1, 123)
print(n) # zip()
### 1
list1 = ["a", "b", "c", "d"]
list2 = [12, 234, 32, 23]
n = zip(list1, list2)
print(n)
### 2 列表转换成字典 list1 = ["a", "b", "c", "d"]
list2 = [12, 234, 32, 23]
n = dict(zip(list1, list2))
print(n) #dict.updata() dict1.update(n)
print(dict1) # sorted() 指定项目排序
print sorted(dict1.iteritems(), key = lambda d: d[1], reverse=True) #按valuse值排序
print sorted(dict1.iteritems(), key = lambda a: a[0], reverse=True) #按key值排序

Python 复习-1的更多相关文章

  1. 用Python复习离散数学(一)

    最近要复习离散数学,不想挂啊,但是又想编程,大家知道啦,程序员离不开代码啊,所用想边复习边写代码,所以就自己用代码去实现一下离散的知识点,当做复习,自知自己的Python很渣,也想借此巩固一下基础,哈 ...

  2. 九九乘法表的python复习

    九九开始的复习 这周复习之前的学的知识关于range函数,gormat函数,print的使用总结一下 从一个小例子开始,开始我的回顾吧, 大家都是从那个九九乘法表开始的数学之旅,从一一得一,开始了我们 ...

  3. Python复习 一

    Python回炉复习 1 变量 Python的变量和C语言的变量书写方式类似: 书写要求 python程序编写结构利用缩进表示,抛弃花括号: 结构 分支: if(条件语句1): 执行语句块 else ...

  4. 后端程序员之路 20、python复习

    Welcome to Python.orghttps://www.python.org/ 怎么用最短时间高效而踏实地学习 Python? - 知乎https://www.zhihu.com/quest ...

  5. python复习

    1.input和raw_input的区别 input假设输入的都是合法的python表达式,如输入字符串时候,要加上引号,而raw_input都会将所有的输入作为原始数据 2.原始字符串前面加上r,e ...

  6. Python复习笔记-字典和文件操作

    抽时间回顾2年前自己做过的python工具,突然感觉不像自己写的,看来好久没用过python的字典和文件操作了,查询资料和网页,整理如下: 一.字典 键值对的集合(map) 字典是以大括号“{}”包围 ...

  7. 用Python复习离散数学(二)

    这次复习的是计数问题,立刻走起吧! 1.乘法原理 如果一项工作需要t步完成的,第一步有n1种不同的选择,第二步有n2种不同的选择,……,第t步有nt中不同的选择,那么完成这项工作所有可能的选择种数为: ...

  8. python复习。知识点小记

    1.对于单个字符的编码,Python提供了ord()函数获取字符的整数表示,chr()函数把编码转换为对应的字符: >>> ord('A') >>> ord('中' ...

  9. python复习1

    比如常用的数学常数π就是一个常量.在Python中,通常用全部大写的变量名表示常量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对 ...

随机推荐

  1. 【TP】TP如何向模板中的js传变量

    <input type="hidden" class= "val" value = "{$value}" /> <scri ...

  2. 数据结构-二叉树(Binary Tree)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  3. python2与python3的区别,以及注释、变量、常量与编码发展

    python2与python3的区别 宏观上: python2:源码不统一,混乱,重复代码太多. python3:源码统一标准,能去除重复代码. 编码上: python2:默认编码方式为ASCII码. ...

  4. 使用JFreeChart生成报表

    1.JFreeChart简介    JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications,servlets以及JSP等使用所设计.  ...

  5. hdu 1257最少拦截系统

    最少拦截系统 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的 ...

  6. 使用python实现简单爬虫

    简单的爬虫架构 调度器 URL管理器 管理待抓取的URL集合和已抓取的URL,防止重复抓取,防止死循环 功能列表 1:判断新添加URL是否在容器中 2:向管理器添加新URL 3:判断容器是否为空 4: ...

  7. 关于RelativeLayout设置垂直居中对齐不起作用的问题

    直接上代码 1.原有代码:(红色字体部分不起作用,无法让RelativeLayout中的textview居中) <RelativeLayout Android:id="@+id/aut ...

  8. CMD命令简介

    cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一般Wind ...

  9. win7装python3.6提示api-ms-win-runtime-1-1-0.dll丢失

    win7为MSDN下的旗舰版,没有servicepack1那个,刚开始安装python3.6提示必须得安装servicepack1,于是乎到微软官网下了个900mb大小的安装包. https://ww ...

  10. CMD 下运行python的unittest测试脚本无输出

    正常情况下windows的命令行执行python脚本命令: python 脚本名.py 我这样做了,看截图可以看到,并没有期待中那样有一堆高大上的信息输出,反而毛都没有!!!! 于是,我想起了度娘,但 ...