zoukankan      html  css  js  c++  java
  • 01、python基础知识

    修改字符串的大小写:
    首字母大写
    >>> name = "gao ziqiang"
    >>> print(name.title())
    Gao Ziqiang
    全部大写或者全部小写
    >>> print(name.upper())
    GAO ZIQIANG
    >>> print(name.lower())
    gao ziqiang
     
    合并(拼接)字符串
    >>> first_name = "ada"
    >>> last_name = "loveLace"
    >>> full_name = first_name + " " + last_name
    >>> print(full_name)
    ada loveLace
     
    使用制表符或换行符来添加空白
    >>> print(" hello python")
    hello
    python
     
    删除空白
    >>> print("asdad aadasdas asdasd ".rstrip())
    asdad aadasdas asdasd结尾
    >>> print(" asdad aadasdas asdasd a ".lstrip())
    asdad aadasdas asdasd a 开头
    >>> print(" asdad aadasdas asdasd a ".strip())
    asdad aadasdas asdasd a两端
    >>> a = " a b c "
    >>> a.replace(" ", "")
    'abc'全部(实现的方法很多)
     
    2的3,4次方
    >>> 2 ** 3
    8
    >>> 2 ** 4
    16
     
    整数除法
    >>> 3 / 2
    1.5
    >>> 3 // 2
    1
    0
    >>> int(0.2 + 0.1)
    0
    >>> int(0.3 + 0.2)
    0
    >>> 0.3 + 0.2
    0.5
    >>> int(0.8 + 0.1)
    0
     
    str()
    >>> age = 23
    >>> message = str(23) + "岁"
    >>> print(message)
    23岁
     
    列表
    >>> bicycles = ['trek', 'cannondale', 'redline', 'specialized']
    >>> print(bicycles)
    ['trek', 'cannondale', 'redline', 'specialized']
    访问列表元素
    >>> print(bicycles[0])
    trek
    索引从 0 而不是 1 开始
    访问最后一个元素
    >>> print(bicycles[-1])
    specialized
    修改列表元素
    >>> bicycles[0] = 'asdasd'
    >>> print(bicycles)
    ['asdasd', 'cannondale', 'redline', 'specialized']
    添加元素
    >>> bicycles.append('new')
    >>> print(bicycles)
    ['asdasd', 'cannondale', 'redline', 'specialized', 'new']
    插入元素
    >>> bicycles.insert(0, 'ducati')
    >>> print(bicycles)
    ['ducati', 'asdasd', 'cannondale', 'redline', 'specialized', 'new']
    >>> bicycles.insert(2, '2')
    >>> print(bicycles)
    ['ducati', 'asdasd', '2', 'cannondale', 'redline', 'specialized', 'new']
    删除元素
    0
     
    0
     
    0
     
    0
     
    0
    组织列表
    list.sort()(按字母排序)
    list.sort(reverse=True)(和上面的相反)
    要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数 sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。
    sorted(list)(不会影响原来的序列)
    反转列表:reverse(list)
    确定列表长度:len(list)
    操作列表
    遍历整个列表
    list = ['a','b','c']
    for temp in list:
    print(temp)
    0
    numbers = list(range(1,6)) [1, 2, 3, 4, 5]
    even_numbers = list(range(2,11,2)) [2, 4, 6, 8, 10]
    squares = []
    for value in range(1,11):
    square = value**2
    squares.append(square)
    print(squares) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    0
    squares = [value**2 for value in range(1,11)]
     
    列表切片
     
    列表复制
    my_foods = ['pizza', 'falafel', 'carrot cake']
    friend_foods = my_foods[:]
    >>> list1=['a','b']
    >>> list2=list1 这样不是复制,只是让两个变量名指向一个列表而已
    >>> print(list2)
    ['a', 'b']
    >>> list1.append('c')
    >>> print(list2)
    ['a', 'b', 'c']
     
    元组
    列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网 站的用户列表或游戏中的角色列表至关重要。然而,有时候你需要创建一系列不可修改的元素,元组可以满足这种需求。Python将不能修改的值称为不可变的,而不可变的列表被称为元组。
    dimensions = (200, 50)
    修改元组的 操作是被禁止的,因此Python指出不能给元组的元素赋值
    dimensions = (200, 50)
    for dimension in dimensions:
    print(dimension)
    0
     
    if语句
    cars = ['audi', 'bmw', 'subaru', 'toyota']
    for car in cars:
    if car == 'bmw':
    print(car.upper())
    else:
    print(car.title())
    and or
    if elif else
     
    字典
    alien_0 = {'color': 'green', 'points': 5}
    print(alien_0['color'])
    print(alien_0['points'])
    字典是一种动态结构,可随时在其中添加键—值对。要添加键—值对,可依次指定字典名、用 方括号括起的键和相关联的值。
    注意,键—值对的排列顺序与添加顺序不同。Python不关心键—值对的添加顺序, 而只关心键和值之间的关联关系。
    要修改字典中的值,可依次指定字典名、用方括号括起的键以及与该键相关联的新值。
    对于字典中不再需要的信息,可使用del语句将相应的键—值对彻底删除。使用del语句时, 必须指定字典名和要删除的键。
    0
     
    0
     
    0
     
    0
     
    0
     
    输入
    函数input()
    raw_input() python2.7
    int() ‘23’-》23
     
    while循环
    0
     
    函数:
    定义函数:
     
    0
    向函数传递信息:
     
    0
     
     
    0
     
    0
    函数参数可以添加默认值
     
  • 相关阅读:
    逻辑回归
    异常
    内部类
    接口
    多态
    final关键字(最终的)
    不能被继承的父类成员
    对象初始化的过程
    方法重写
    abstract关键字
  • 原文地址:https://www.cnblogs.com/koss/p/14921234.html
Copyright © 2011-2022 走看看