zoukankan      html  css  js  c++  java
  • python学习笔记

    廖雪峰python教程

    如何直接运行py程序?

    #!/usr/bin/env python3
    print('hello, world')
    
    $ chmod a+x hello.py

    python 中 print()函数的设置细节?

    # print()会依次打印每个字符串,遇到逗号“,”会输出一个空格
    >>> print('a','b','c')
    a b c
    >>> print('a')
    >>> print('b')
    a
    b

    python如何从键盘输入数据?

    >>> name = input('input your name:')
    input your name:lizhixin
    >>> name
    'lizhixin'

    如何将notepad++中的Tab设置为4个空格?

    设置 => 首选项 =>制表符设置

    怎么设置强行不转义?

    >>> r'
    
    	'
    '\n\r\t'

    python中变量赋值的结果?

    >>> a = 3
    >>> b = a
    >>> a = 4
    >>> b
    3

    字符编码的基本知识?

    # 计算机只会处理数字,任何文本最终都会被转化为数字来进入cpu参与运算
    # 8 bit(比特) = 1 byte(字节)
    # 国际编码Unicode, 转化为'可变长编码' UTF-8, 内存中使用Unicode, 要传输和存储到硬盘就要保存为UTF-8

    如何获取单个字符的整数表示? 如何把编码转换为对应的字符?

    >>> ord('l')
    108
    >>> ord('#')
    35
    >>> chr(123)
    '{'
    >>> chr(20013)
    ''
    # Python的字符串类型是str,在内存中以Unicode表示,一个字符对应若干个字节。如果要在网络上传输,或者保存到磁盘上,就需要把str变为以字节为单位的bytes。

    如何把str转化为bytes?

    >>> 'lizhixin'.encode('ascii')
    b'lizhixin'
    >>> '李治鑫'.encode('utf-8')
    b'xe6x9dx8exe6xb2xbbxe9x91xab'

    怎么把bytes转化为str?

    >>> b'grge'.decode('ascii')
    'grge'
    >>> b'xe6x9dx8exe6xb2xbbxe9x91xab'.decode('utf-8')
    '李治鑫'

    如何格式化输出字符串?

    >>> 'Hi, %s, you have $%d.' % ('lizhixin', 10000000)
    'Hi, lizhixin, you have $10000000.'

    python有哪几种常用的数据结构?

    # 列表 list
    >>> a = [1, 2, 3]
    >>> a
    [1, 2, 3]
    # 元组 tuple
    >>> b = 1, 2, 3
    >>> b
    (1, 2, 3)
    # 字典 dict
    >>> c = {1:2, 3:4, 5:6}
    >>> c
    {1: 2, 3: 4, 5: 6}
    # 集合 set
    >>> d = {1, 2, 3, 4}
    >>> d
    {1, 2, 3, 4}

    python有哪几种循环?

    >>> a = [1, 3, 5, 7, 9]
    >>> for num in a:
        print(num)
    
        
    1
    3
    5
    7
    9
    >>> n = 0
    >>> while n <= 10:
        print (n)
        n = n + 1
    
        
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    python 中 的字典有哪些操作?

    # 很难一次写对, { }, [], ' ', 一个都不能错
    >>> names = {'Micheal':95, 'Bob':59, 'Tracy':85}
    >>> names['Micheal']
    95

    Python中有哪些类型转换函数?

    >>> int('123')
    123
    >>> int(12.6)
    12
    >>> float('12')
    12.0
    >>> str(1.23)
    '1.23'
    >>> str(100)
    '100'
    >>> bool(1)
    True

    如何为函数起别名?

    >>> a = abs
    >>> a(-100)
    100

    pass语句是干嘛用的?

    pass可以用来作为占位符,比如现在还没想好怎么写函数的代码,就可以先放一个pass,让代码能运行起来。

    python中的异常处理? raise的用法?

    python中的异常处理

    python中函数居然可以有多个返回值?

    >>> import math
    >>> def move(x, y, step, angle=0):
        nx = x + step * math.cos(angle)
        ny = y - step * math.sin(angle)
        return nx, ny  #实际上返回的是一个值,它是元组,所以造成了假象

    python中的函数有哪几种参数?

    有位置参数、默认参数、可变参数 和 关键字参数。

    # 位置参数
    def power(x, n):
    power(5, 3)
    
    # 默认参数 
    # 默认参数必须指向不变对象!
    def power(x, n = 2):
    power(5)
    
    # 可变参数  
    # 传入 列表 或 元组
    >>> def calc( *numbers ):
        sum = 0
        for n in numbers:
            sum = sum + n * n
        return sum
    
    >>> calc((1, 3, 5, 7))
    84
    
    # 关键字参数
    # 传入 字典
    >>> def person(name, age, **kw):
        print("name:", name, "age:", age, "other:", kw)
    
        
    >>> person('lizhixin', 25, gender='M', job='Engineer')
    name: lizhixin age: 25 other: {'gender': 'M', 'job': 'Engineer'}
     
    # 命名关键字参数

    >>> def person(name, age, *, city, job):
        print(name, age, city, job)

    >>> person('Jack', 24, city='Beijing', job='Engineer')
    Jack 24 Beijing Engineer

    函数的组合参数

    参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。

    递归函数如何用python实现?

    >>> def fact(n):
        if n==1:
            return 1
        return n * fact(n-1)
    
    >>> fact(5)
    120
  • 相关阅读:
    递归算法
    linux下如何使用split
    什么是OPTEE-OS
    ubuntu 18.04 64bit如何编译安装内核
    ubuntu 18.04 64bit没有声音如何解决
    如何解决ubuntu报的错误:You must put some 'source' URIs in your sources.list
    linux下如何安装解压工具rar
    如何将一个已有的仓库推送到一个空的新仓库中
    ubuntu 18.04 64bit下如何安装python开发工具jupyter
    ubuntu 18.04 64bit下如何安装python开发工具jupyterhub
  • 原文地址:https://www.cnblogs.com/leezx/p/5599382.html
Copyright © 2011-2022 走看看