python基本知识小记1

1. 数字:

decimal.Decimal('1.1')

是1.1,不会是1.10000000001

 

2. Print:

print  等于 print '\n'

>>> print “good!” if 8 == number else “bad!”

 

字典可以用变量名直接格式化输出
table = {‘Jack’: 4098,}
print ‘Jack: %(Jack)d’ % table

 

3. 在循环中修改列表的内容,用副本修改

>>> a = [“cat”, “dog”]
>>> for x in a[:]:
…     if len(x) < 6:
…         a.insert(0,x)


4. 对于List:
extend(L) 追加列表
count(x) x出现次数
[::-1] 倒序

 

5. 创建字典:

tel.fromkeys([1, 2], 0)  =>  {1: 0, 2: 0}

 

6. 函数参数

func(*para, **paras)   元组, 字典
eg: test(“yes”, 1, 2, me = “xxx”, where = “China”)

 

7. eval() 对字符串参数运算求值
exec() 将字符串参数作脚本执行,execfile()

 

8. 装饰器:

@A def B: pass  ==  def B: pass B = A(B)


eg: 测试运行时间

from time import time
def cost_time(func):
    def result(*args, **dic):
        begin = time()
        func(*args, **dic)
        print “cost time: “, time() – begin
    return result

@cost_time
def show(n):
    for x in range(n):
        print x


9. 生成器表达式

eg:
unique_words = set(word for line in file for word in line.split())

 

10. Filter:

filter(func, seq)  返回seq中使func返回True的值
eg:
a = [1, 2, 3, 4]
filter(lambda x : x % 2, a)   =>  [1, 3]

 

11. Map:

 eg:
map(lambda x, y, z: x + y + x, range(1, 3), range(3, 5), range(5, 7))  =>  [9, 12]

 

12. Reduce:

 eg:
reduce(lambda x, y: x + y, range(3), 99)   ==  sum(range(3), 99)
过程:99 + 0 = 99 -> 99 + 1 = 100 -> 100 + 2 = 102

 

13. 多个seq的循环:
questions = [“name”, “quest”]
answers = [“rae”, “ok”]
for q, a in zip(questions, answers):
 print “what is your %s? It is %s” % (q, a)

 

14. 列表循环输出索引位置和对应的值:
for i, v in enumerate([‘a’, ‘b’, ‘c’]):
 print i, v

 

16. 包:

生成包,目录下有__init__.py, 该文件中有__all__ = [“file1”, “file2”, ]

 

17. 编码:
# -*- coding: utf-8 -*-
网络编程可用gbk

 

18. bisect 二分查找实现
bisect.insort(list, element)  快速插入有序序列

 

参照
python基础学习:
http://wiki.woodpecker.org.cn/moin/PyAbsolutelyZipManual

《Python核心编程(第二版)》

 

C程序员转Python程序员需要注意:


1. python不支持++和--操作

2. 有bool型,C++也有

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下几种途径学习Python基本知识和技能: 1. 参考Python基础教程,如《Python 基础(二):基本语句》、《Python 基础(三):我是一个数字》等。这些教程将会教你Python的基本语法、数据类型、序列、列表和元组、字典和集合、函数、模块和包等基础知识。 2. 《Python 学习手册》是一本权威的Python学习资源,其中包含了Python的全面介绍和深入讲解,同时还附带了许多经典实用的练手项目和面试题。 3. 对于数据分析方面的知识,你可以学习《Python 数据分析(一):NumPy 基础知识》、《Python 数据分析(二):Matplotlib 绘图》、《Python 数据分析(三):初识 Pandas》等教程。这些教程将会教你如何使用Python进行数据处理、可视化和分析。 4. 除了书籍和教程,你还可以通过参加在线课程、观看教学视频和参与社区讨论等方式来学习Python的基础知识。 总结起来,学习Python基础知识的最佳途径是结合理论学习和实践练习,通过阅读教材、参考文档和进行实际项目开发来提升自己的编程能力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python 基础(一):入门必备知识](https://blog.csdn.net/ityard/article/details/102807071)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值