python基础1(未完成版,及时更新)

变量

变量命名规则

  1. 只能包含数字、字母、下划线
  2. 不能以数字开头
  3. 不能使用python关键字或保留字(如print)
  4. 应具有一定描述性含义
  5. 谨慎使用小写l和大写O, 容易与数字1和0混淆
  6. 可使用驼峰命名法StudentName, 或下划线

基本数据类型

字符串

1. 用法:

  • 单引号双引号连接

2. 字符串常用方法

  • a.title()
  • a.upper()
  • a.lower()
  • a.strip() a.lstrip() a.strip()

3. 字符串拼接

  • 字符串a + 字符串b

4. 常用转义字符

  • /n : 换行符
  • /t : 制表符

数字

1. 整数int

  • 计算
    • 幂运算:2**3
    • 整除:10//3
    • 取余数:10%3

2. 浮点数float

列表

1. 列表的定义:

​ 列表由一系列按特定排列组成。用方括号[]标示列表。

1
bicycles = ['trek', 'cannondale', 'redline', 'specialized']

2. 列表的增删改查

    • 通过索引访问

      1
      2
      bicycles[0]		 #访问列表第一个元素
      bicycles[-1] # 访问列表最后一个元素
    • 通过索引覆盖修改

      1
      2
      3
      4
      motorcycles = ['honda', 'yamaha', 'suzuki']
      print(motorcycles)
      motorcycles[0] = 'ducati'
      print(motorcycles)
    • 列表末尾追加元素

      • a.append(object)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      # 已有列表末尾增加元素
      motorcycles = ['honda', 'yamaha', 'suzuki']
      print(motorcycles)
      motorcycles.append('ducati')
      print(motorcycles)

      # 可用于动态创建列表
      motorcycles = []
      motorcycles.append('honda')
      motorcycles.append('yamaha')
      motorcycles.append('suzuki')
      print(motorcycles)
    • 列表中插入元素

      • a.insert(index, object)
      1
      2
      3
      4
      5
      # 根据索引向列表中插入元素
      motorcycles = ['honda', 'yamaha', 'suzuki']
      print(motorcycles)
      motorcycles.insert(0, 'ducati')
      print(motorcycles)
    • 根据索引删除元素

      • del a[index]
    • 返回值为删除的元素。 默认删除最后一个。

      • a.pop(index)
      1
      2
      3
      4
      5
      6
      # 删除列表末尾元素,返回值为删除的元素
      motorcycles = ['honda', 'yamaha', 'suzuki']
      print(motorcycles)
      popped_motorcycle = motorcycles.pop()
      print(motorcycles)
      print(popped_motorcycle)
    • 根据值删除元素

      • a.remove(object) 只删除第一个指定的值,如果有多个相同值在列表中需要循环删除

数据类型转换

字符串-数字转换:

python2与python3区别

1. print函数用法不同
  • python2: print “hello world”
  • python3: print(“hello world”)
2. 整数相除计算结果不同
  • python2: 3/2 result: 1
  • python3: 3/2 result: 1.5
3.

注释

  • 编写目的:阐述代码要实现的功能以及如何实现。
  • 注释原则:清晰,简洁。
  • 注释用#号标识

python之禅

The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!