一步一步学Python-基础篇

1、安装

  地址:https://www.python.org/downloads/windows/

  安装完成过后,配置环境变量,比如:path后面计入;C:Python27(可能需要重启一下)

  然后cmd输入python,显示如下,说明安装成功

  一步一步学Python-基础篇

2、基础知识(记录写特列)

  0、空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。

  1、字符串:用单引号或双引号都行,比较特殊的是三个单引号是多行文本,中间可以换行

  2、如果字符串包含很多转移的字符,可以用r'23wer2'可以加r前缀,相当于c#里的@

  3、如果打印有乱码可以使用print u'中文'加u前缀,以Unicode表示的字符串

  4、u、r这些可以结合起来用,比如:
    ur'''Python的Unicode字符串支持"中文",         

      "日文",         

      "韩文"等多种语言'''

  5、py文件可以填一行# -*- coding: utf-8 -*-告诉Python解析器,以utf-8编码读取源码

  6、if else语法

if age >= 18:
     print 'adult'
else:
     if age >= 6:
          print 'teenager'
     else:
          print 'kid'

  7、循环

L = ['Adam', 'Lisa', 'Bart']
  for name in L:
       print name
N = 10
x = 0
while x < N:
    print x
    x = x + 1
sum = 0
x = 1
while True:
    sum = sum + x
    x = x + 1
    if x > 100:
        break
print sum
for x in L:
    if x < 60:
        continue
    sum = sum + x
    n = n + 1
for x in ['A', 'B', 'C']:
    for y in ['1', '2', '3']:
        print x + y

  8、字典

# 字典特点 dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样。而list的查找速度随着元素增加而逐渐下降。不过dict的查找速度快不是没有代价的,dict的缺点是占用内存大,还会浪费很多内容,list正好相反,占用内存小,但是查找速度慢。由于dict是按 key 查找,所以,在一个dict中,key不能重复
# 定义字典
d = {
    'Adam': 95,
    'Lisa': 85,
    'Bart': 59
}
# 访问
print d['Adam']
# 判断key是否存在
if 'Paul' in d:
    print d['Paul']
#使用dict本身提供的一个 get 方法,在Key不存在的时候,返回None
print d.get('Bart')
#更新
 d['Paul'] = 72
#遍历
for key in d:
    print key
#遍历value
for v in d.values():
    print v

  9、元祖

t = ('Adam', 'Lisa', 'Bart')
t[0] = 'Paul'
#因为()既可以表示tuple,又可以作为括号表示运算时的优先级
>>> t = (1,)
>>> print t
(1,)
# 元祖可包含list
 t = ('a', 'b', ['A', 'B'])
L = t[2]
>>> L[0] = 'X'
>>> L[1] = 'Y'
>>> print t
('a', 'b', ['X', 'Y'])

 10、set

#由于set存储的是无序集合,所以我们没法通过索引来访问。
 s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
#用 in 操作符判断,返回True
'Bart' in s 
#遍历
s = set(['Adam', 'Lisa', 'Bart'])
for name in s:
print name
#添加
s = set([1, 2, 3])
s.add(4)
#不存在remove会报错
s.remove(7)

  11、函数

# 返回多值
import math
def move(x, y, step, angle):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny
#调用
x, y = move(100, 100, 60, math.pi / 6)
print x, y
151.961524227 70.0
#递归
def fact(n):
    if n==1:
        return 1
    return n * fact(n - 1)
#默认参数
def power(x, n=2,c=4):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
#跳过调用
power(1,c=3)
#可变参数,直接把变量 args 看成一个 tuple 就好了
def fn(*args):
    print args

def func(a, b=5, c=10):
    print 'a is', a, 'and b is', b, 'and c is', c

func(3, 7)
func(25, c=24)
func(c=50, a=100)

  11、DocStrings

def printMax(x, y):
    '''打印我的文档内容Prints the maximum of two numbers.
The two values must be integers.'''
    x = int(x) # convert to integers, if possible
    y = int(y)

    if x > y:
        print x, 'is maximum'
    else:
        print y, 'is maximum'

printMax(3, 5)
print printMax.__doc__ 

 11、切片

L = ['Adam', 'Lisa', 'Bart', 'Paul']
#取前三个元素,值是负数的话就是倒序取值
L[0:3]
#如果第一个索引是0,还可以省略
L[:3]
#只用一个 : ,表示从头到尾,相当于复制了一个
L[:]
#切片操作还可以指定第三个参数
 L[::2] # 表示没两个元素取一个,第三个参数表示每N个取一个,上面的 L[::2] 会每两个元素取出一个来,也就是隔一个取一个。
L[:2:2] #表示前两个元素没两个取一个,也就是Adam
#对字符串切片
>>> 'ABCDEFG'[:3]
'ABC'
>>> 'ABCDEFG'[-3:]
'EFG'
>>> 'ABCDEFG'[::2]
'ACEG'
#range()函数可以创建一个数列
range(1, 101)
[1, 2, 3, ..., 100]

 r = []
 n = 3
 for i in range(n):
     r.append(L[i])

说明:部分内容来自慕课网和Python手册内容