Python3学习策略

自学Python要点

【来自:http://www.cnblogs.com/shsxt/p/9138950.html】html

1.找一本浅显易懂,例程比较好的教程,从头至尾看下去。 不要看不少本,专一于一本。把里面的例程都手打一遍,搞懂为何。我当时看的是《简明python教程》,不过这本书不是很是适合零基础初学者。零基础推荐《与孩子一块儿学编程》,或者看我写的教程 Crossin的编程教室 - Python入门。前端

2.去找一个实际项目练手。 我当时是由于要作一个网站,不得已要学python。这种条件下的效果比你平时学一门新语言要好不少。因此最好是要有真实的项目作。能够找几个同窗一块儿作个网站之类。注意,真实项目不必定非要是商业项目,你写一个只是本身会用的博客网站也是真实项目,关键是要核心功能完整。Crossin:Python 的练手项目有哪些值得推荐?python

3.找到一个已经会python的人作老师。git

问他一点学习规划的建议(上知乎也是个途径),而后在遇到卡壳的地方找他指点。这样会事半功倍。可是,要学会搜索,学会如何更好地提问。没人愿意帮你写做业或是回答“一搜便知”的问题。

然而,别人的经验未必能彻底复制。好比我没有说的是,在自学python以前,我已在学校系统学习过其余的编程语言。对于彻底没有编程经验的初学者,在学习python的时候,面对的不只仅是python这门语言,还须要面临“编程”的一些广泛问题,好比:

从零开始,不知道从何入手,找了本编程教材发现第二章开始就看不懂了; 缺乏计算机基础知识,被一些教程略过的“常识性”问题卡住; 遇到问题不知道怎么寻找解决方案。看懂语法以后不知道拿来作什么,学完一阵子就又忘了;

缺乏数据结构、设计模式等编程基础知识,只能写出小的程序片断。

4.其它的经验程序员

- 首先要有信心。虽然可能你看了几个小时也没在屏幕上打出一个三角形,或者压根儿就没能把程序运行起来。但相信我,几乎全部程序员一开始都是这么折腾过来的。

- 选择合适的教程。有些书很经典,但未必适合你,可能你写了上万行代码以后再看它会比较好。 三、写代码,而后写更多的代码。光看教程,编不出程序。从书上的例程开始写,再写小程序片断,而后写完整的项目。

- 除了学习编程语言,也兼顾补一点计算机基础,和英语。

- 不但要学写代码,还要学会看代码,更要会调试代码。读懂你本身程序的报错信息。再去找些github上的程序,读懂别人的代码。

- 学会查官方文档,用好搜索引擎和开发者社区。

5.Python学习路线大纲github

- 经常使用操做系统基础

- Python基础及进阶

- 数据库SQL

- 前端及移动开发

- web全栈

- 爬虫及搜索

- 大数据分析

- 机器学习

- 深度学习:TensorFlow、Caffe、CNN/RNN实战、人脸识别、文本挖掘等

Python自动化流程结构

【来自:http://www.cnblogs.com/wendj/p/9141942.html】web

import keyword
for i,j in enumerate(keyword.kwlist):
    print(i,j,end = ' ')
0 False 1 None 2 True 3 and 4 as 5 assert 6 break 7 class 8 continue 9 def 10 del 11 elif 12 else 13 except 14 finally 15 for 16 from 17 global 18 if 19 import 20 in 21 is 22 lambda 23 nonlocal 24 not 25 or 26 pass 27 raise 28 return 29 try 30 while 31 with 32 yield
num = 50 
if num>18:
    print('num大于18')
    
print('------无论条件是否知足都要继续往下执行----')
num大于18
------无论条件是否知足都要继续往下执行----
num = 9
if num>18:
    # 条件知足执行的代码块
    print('num大于18')
else:
    # 条件不知足
    print('num小于18')
 
print('-------------代码继续往下执行----------------')
num小于18
-------------代码继续往下执行----------------
score = -5
if score>=90 and score<=100:
    print('本次考试,等级为A')
elif score>=80 and score<90:
    print('本次考试,等级为B')
elif score>=70 and score<80:
    print('本次考试,等级为C')
elif score>=60 and score<70:
    print('本次考试,等级为D')
elif score>=0 and score<60:
    print('本次考试,等级为E')
else:
    print('上述条件均布知足的时候执行此处,你的成绩已经超越等级制度')

print('-------------代码继续往下执行----------------')
上述条件均布知足的时候执行此处,你的成绩已经超越等级制度
-------------代码继续往下执行----------------
i = 1
while i <= 5:
    i+=1
    print('bright! hello.')
print('---代码继续往下执行---')
bright! hello.
bright! hello.
bright! hello.
bright! hello.
bright! hello.
---代码继续往下执行---
i = 1
while i <= 9:   # 判断行数,一共有8行
    j = 1
    while j <= i:  # 断定列数,列数根据i肯定
        print('@ ', end='')
        j += 1
    print('\n')
    i += 1
@ 

@ @ 

@ @ @ 

@ @ @ @ 

@ @ @ @ @ 

@ @ @ @ @ @ 

@ @ @ @ @ @ @ 

@ @ @ @ @ @ @ @ 

@ @ @ @ @ @ @ @ @
i = 1
while i <= 9:   # 判断行数,一共有8行
    j = 1
    while j <= i:  # 断定列数,列数根据i肯定
        print('{:d}X{:d}={:2d} '.format(j,i,i*j), end='')
        j += 1
    print('\n')
    i += 1
else:  # 循环正常结束,没有break语句时,执行此处的代码。
    print('九九乘法表打印完毕!欢迎小学生背诵')
1X1= 1 

1X2= 2 2X2= 4 

1X3= 3 2X3= 6 3X3= 9 

1X4= 4 2X4= 8 3X4=12 4X4=16 

1X5= 5 2X5=10 3X5=15 4X5=20 5X5=25 

1X6= 6 2X6=12 3X6=18 4X6=24 5X6=30 6X6=36 

1X7= 7 2X7=14 3X7=21 4X7=28 5X7=35 6X7=42 7X7=49 

1X8= 8 2X8=16 3X8=24 4X8=32 5X8=40 6X8=48 7X8=56 8X8=64 

1X9= 9 2X9=18 3X9=27 4X9=36 5X9=45 6X9=54 7X9=63 8X9=72 9X9=81 

九九乘法表打印完毕!欢迎小学生背诵
name = 'bright'

for x in name:
    print('----')
    if x == 'h':
        break
    print(x)
else:
    print('循环状况是否正常')

print('---代码继续往下执行---')
----
b
----
r
----
i
----
g
----
---代码继续往下执行---
i = 0
while i<10:
    i += 1
    print(i, end=" ")
    
print('\n---代码继续往下执行---')
1 2 3 4 5 6 7 8 9 10 
---代码继续往下执行---
nums = [i for i in range(1,11)]
print("第二种方法:%s" % nums)
第二种方法:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = 0
for i in range(1,101):
    sum += i
print('1到100的和:%d' % sum)
1到100的和:5050
for i in range(1, 51):
    if i % 2 == 0:
        print(i, end=" ")
print('\n---代码继续往下执行---')
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 
---代码继续往下执行---
nums = [i for i in range(1,51) if i % 2 != 0]
print(nums)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
# 求1-2+3-4+5 ... 99的全部数的和
sum = 0
jishu = 0
oushu = 0
for i in range(1, 101):
    if i % 2 != 0:
        jishu += i
    if i % 2 == 0:
        oushu += i
sum = jishu - oushu
print("第一种方法:%s" % sum)
第一种方法:-50

Python高级用法

【来自】http://www.cnblogs.com/Python1234/p/9139517.html面试

def fibonacci_generator():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a+b
for i in fibonacci_generator():
    if i > 200:
        break
    print(i, end=' ')
0 1 1 2 3 5 8 13 21 34 55 89 144
x = (k * k for k in range(20))
print(type(x))
<class 'generator'>

collections包是标准库的一个模块,主要目的是用来扩展容器相关的数据类型,数据库

咱们经过dir查看collections包有哪些模块:编程

import collections
for i in dir(collections):
    print(i, end=',')
AsyncGenerator,AsyncIterable,AsyncIterator,Awaitable,ByteString,Callable,ChainMap,Collection,Container,Coroutine,Counter,Generator,Hashable,ItemsView,Iterable,Iterator,KeysView,Mapping,MappingView,MutableMapping,MutableSequence,MutableSet,OrderedDict,Reversible,Sequence,Set,Sized,UserDict,UserList,UserString,ValuesView,_Link,_OrderedDictItemsView,_OrderedDictKeysView,_OrderedDictValuesView,__all__,__builtins__,__cached__,__doc__,__file__,__loader__,__name__,__package__,__path__,__spec__,_chain,_class_template,_collections_abc,_count_elements,_eq,_field_template,_heapq,_iskeyword,_itemgetter,_proxy,_recursive_repr,_repeat,_repr_template,_starmap,_sys,abc,defaultdict,deque,namedtuple,
from collections import Counter

a = Counter('blue')
b = Counter('yellow')

print(a)
print(b)
print((a + b).most_common(3))
Counter({'b': 1, 'l': 1, 'u': 1, 'e': 1})
Counter({'l': 2, 'y': 1, 'e': 1, 'o': 1, 'w': 1})
[('l', 3), ('e', 2), ('b', 1)]
from collections import defaultdict

my_dict = defaultdict(lambda: 'Default Value')
my_dict['a'] = 42

print(my_dict['a'])
print(my_dict['b'])
42
Default Value
from collections import defaultdict
import json

def tree():
 """
 Factory that creates a defaultdict that also uses this factory
 """
 return defaultdict(tree)

root = tree()
root['Page']['Python']['defaultdict']['Title'] = 'Using defaultdict'
root['Page']['Python']['defaultdict']['Subtitle'] = 'Create a tree'
root['Page']['Java'] = None

print(json.dumps(root, indent=4))
{
    "Page": {
        "Python": {
            "defaultdict": {
                "Title": "Using defaultdict",
                "Subtitle": "Create a tree"
            }
        },
        "Java": null
    }
}
 
from itertools import permutations
for p in permutations([1,2,3,4]):
    print(p)
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)
from itertools import combinations
for c in combinations([1,2,3,4],2):
    print(c)
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
from itertools import chain
for c in chain(range(3), range(12,15)):
    print(c)
0
1
2
12
13
14

Python面试知识点

sys模块主要是用于提供对python解释器相关的操做
os模块是Python标准库中的一个用于访问操做系统功能的模块,使用os模块中提供的接口,能够实现跨平台访问,在Linux和Windows下均可以运行。

  1. os和sys都是干什么的?
  2. 你工做中都用过哪些内置模块?
  3. 有没有用过functools模块?

【来自】http://www.cnblogs.com/ManyQian/p/9139856.html

# 1. os和sys都是干什么的?
import os  #经常使用的一些方法
BASE_DIR = os.getcwd()  # 取当前文件的绝对路径
print('1', BASE_DIR)
path = os.path.join(BASE_DIR, "abc")  # 在当前父目录下拼接一个abc的文件夹路径
print('2', path)
path = BASE_DIR + "\\abc"  # 不推荐使用硬编码的形式拼接路径
print('3', path)
1 C:\Users\Future\Desktop\TEMPython
2 C:\Users\Future\Desktop\TEMPython\abc
3 C:\Users\Future\Desktop\TEMPython\abc
# 2. 你工做中都用过哪些内置模块?
import sys
# sys.path.append()  # 向当前运行的环境变量中添加一个指定的路径

# 3. 你工做中都用过哪些内置模块?

# re json hashlib time datetime socket thread functools

#functools模块 
from functools import partial 

def f(a, b):
    return a + b

f(1, 2)
f(100, 200)
f3 = partial(f,3)  # 利用partial和f生成一个默认加3 的一个f3函数
ret = f3(100)  # 默认加3
print(ret)
103
相关文章
相关标签/搜索