• Python学习


    python课程
    1、Python 安装程序下载 “python-2.7.5.amd64.msi”
    download:https://www.python.org/downloads/windows/

    2、Python 基本框架 输入、处理、输出
    str1 = raw_input("plz input a string;"); //输入字符串
    n = len(str1) //计算字符串长度

    3、Python基本输入输出语句
    3.1 raw_input 使用

    >>> age =raw_input('your age:')
    your age:38
    >>> print age
    38
    >>> age = int(age)
    >>> age
    38
    >>> age = age +1
    >>> age
    39

    3.2 类型转换
    str、int、float

    4、Python变量解析
    4.1 Python变量的“变”指向,id
    赋值是指 变量的指向。 设置x = 12 , id(x)得到x指向地址:30992320L

    4.2 Python数据类型不需要指定type
    Python变量类似于C语言的指针。 变量是指向到内存的某个地址。当x值发生改变时,指向地址会发生变化。
    Python变量无数据类型(无需提前定义类型)。根据赋值动态变化。
    4.3 对比一下C变量和Python变量的不同

    5、Python函数库分类及函数标准库示例

    5.1函数
    引入函数库: import math
    >>> math.pow(5,5) 3125.0
    获取math的sin帮助,help(math.sin)
    val = math.sin(math.pi/6) >>> print val 0.5

    5.2系统函数
    currentdir = os.getcwd()
    ldirs = os.listdir(currentdir)
    print ldirs
    result:['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'tcl', 'Tools']

    5.3 socket
    通过名称获得某主机的IP地址
    baiduIp = socket.gethostname('www.baidu.com')
    print baiduIp result:14.215.177.39
    socket系统函数 help(socket)

    6、Python使用第三方函数库及简单网页爬虫示例
    6.1系统库提供内部函数

    6.2第三方提供的函数 httplib2
    1.安装第三方函数库
    http://blog.csdn.net/woaipaiqiu/article/details/43539685
    OK !

    示例:通过爬虫爬取 163网页
    说明:默认已经安装好httplib2库。
    import urllib (引入urllib库)
    import webbrowser (as web:别名) (引入webbrowser库)
    url = 'http://www.163.com' (设置url变量)
    content = urllib.urlopen(url).read() (设置content变量,读取url地址)
    open('163.com.html','w').write(content) (写入读取的url地址到C:Python27目录下)

    至此,已经把目标网址下载到本地文件夹中了。

    2.下载网页内容打开浏览器浏览网页内容
    //打开本地网页
    webbrowser.open_new_tab('163.com.html')

    6.3自定义函数
    1.Python函数自定义 语法结构
    def(函数库) function_name(parameters): (注意冒号:) 可以无参数
    (tab)statement1
    (tab)statement2
    (tab)statement3
    etc.


    1.定义函数必须在函数名后面加 冒号;
    2.函数定义需要增加tab
    3.函数体不需要增加{}

    def test_a():
    print 'hello zm'
    print 'www.cctv.com'

    def test_b(val1,val2):#函数形参
    print val1
    print val2

    print '---------------------------------------'
    test_a()
    test_b(12,13) #函数实参
    print '---------------------------------------'

    保存函数 C:Python27 est1.py

    2.有返回值的函数

    def test_c(n1,n2):#函数形参
    print n1
    print n2
    n = n1+ n2
    return n

    sum = test_c(50,52)
    print 'sum=',sum

    3.有多个值返回
    def test_d(n1,n2):#函数形参
    print n1
    print n2
    n = n1+ n2
    m = n1*n2
    return n,m

    sum1 ,multi = test_d(30,33)
    print sum1,multi

    显示结果:
    30
    33
    63 990

    re = test_d(50,80)
    print re

    显示结果:
    50
    80
    (130, 4000)

    4.有形参和无形参解析
    Python可以声明有型参数和无型台参数。

    5.有预定值的形参

    def test_e(n1,n2 = 10):#函数形参
    print n1,
    print n2
    n = n1+ n2
    return n
    有预定值的参数放到后面;

    7、Python分支语句if
    7.1条件分支if
    if condition:
    (tab) statement
    (tab) statement
    (tab) etc.

    exp:
    count = int(raw_input("Plz input your math record:"))
    if count >80:
    print 'welcomt to china!'

    result:
    Plz input your math record:79


    exp:

    7.2双分支if else
    count = int(raw_input("Plz input your math record:"))
    if count >80:
    print 'welcomt to china!'
    else:
    print 'sorry'

    result: 根据输入条件打印

    7.3多分支if elif
    exp:
    count = int(raw_input("Plz input your result:"))
    if count >90:
    print 'a'
    elif count >80:
    print 'b'
    elif count >70:
    print 'c'
    elif count >60:
    print 'd'
    else:
    print 'e'

    result:根据输入数值打印结果。

    7.4 if分支语句表达式构成 or and
    exp:
    sex = raw_input('sex')
    if sex == 'nan' or sex == 'xx':
    print 'nan'
    else:
    print 'women'

    8、while循环
    while condition:
    statement
    statement
    statement
    etc.

    Exp:

    numbers = [1,2,3,4,5,6,7,8]
    even = []
    odd =[]
    while len(numbers) >0:
    number = numbers.pop()
    if(number % 2 == 0):
    even.append(number)
    print 'even:', even

    else:
    odd.append(number)
    print 'odd:',odd

    print 'bye'


    con = 0
    while (con < 9):
    print 'print con is:', + con
    con = con +1
    print 'byebye'

    9、for循环
    for循环体

    Exp:
    fruits = ['banana', 'apple', 'mango']
    for index in range(len(fruits)):
    print '当前水果 :', fruits[index]

    print "Good bye!"

    10、Python字符串基础
    定义:一有序的字符序列集合,常量。

    10.1 转义字符串
    字符串长度 len(s1)
    10.2 raw 字符串
    关闭转义机制
    10.3 unicode
    10.4 格式化字符串
    lesson1
    "age %d"%(28) result:'age 28'
    Exp:"age %d record % .2f name %s"%(28,98.3567834,"zm") result:'age 28 record 98.36 name zm'

    lesson2
    定义:一有序的字符序列集合,常量。

    10.5字符串基本操作
    1)+ 连接
    s1 = 'www.cctv' s2 = '.com' s = s1+ s2 print s
    如果字符和数据连接需要通过str(n1)作转型
    2)* 重复 n个加
    n1 =10 n2 =20 n =n1*n2 print n
    3)s[i] 索引 index
    访问字符串的某个元素
    string_name[index]
    4) s[i:j] 切片 slice
    s1 = 'abcabcabcabc' sub = s1[2:8] print sub
    切点的位数可以只写起点或者切点
    5)for 循环遍历

    10.6字符串函数


    Python的格式化字符串写入

    格式:
    Str ="xxx%dyyy%s "%('28','zm')
    file_obj.write(Str)

    Exp:
    fd = open('format.txt','w')
    head = "%10s%10s%10s "%('ID','Name','Record')
    item1 = "%10s%10s%10.2f "%('78901','zhangsan',90.88)
    item2 = "%10s%10s%10.2f "%('78902','lisi',79.55)
    item3 = "%10s%10s%10.2f "%('78903','wangwu',56.77)
    fd.write(head)
    fd.write(item1)
    fd.write(item2)
    fd.write(item3)
    fd.close()

    Python项目:
    利用Pathon抓取某网页内容

    <a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102egl0.html">
    地震思考录
    </a>

  • 相关阅读:
    一例千万级pv高性能高并发网站架构[原创]
    Download SymmetricDS Data Sync Software for Free
    阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房)
    青云QingCloud业内率先支持云端全面透明代理功能 | SDNLAB | 专注网络创新技术
    UCloud EIP 你真的懂得如何使用么?
    MySQL高可用性大杀器之MHA | 火丁笔记
    Zookeeper、Solr和Tomcat安装配置实践
    Best Premium Private Proxy Service | Lime Proxies
    突破LVS瓶颈,LVS Cluster部署(OSPF + LVS)
    Nodejs负载均衡:haproxy,slb以及node-slb
  • 原文地址:https://www.cnblogs.com/monjeo/p/7767843.html
Copyright © 2020-2023  润新知