Python基础教程笔记——基础知识

来源:互联网 发布:数据机房消防规范 编辑:程序博客网 时间:2024/05/16 17:18

基础知识

Table of Contents

1 除法

 

1.1 除号-/

  1. 说明: / 是除法符号,两个整型数相除,如果商不是整数,会显示小数。
  2. 注意:商虽然是小数,但是最后一位不会做四舍五入运算。
  3. 例子:
>>> 1/30.3333333333333333>>> 2/30.6666666666666666

1.2 除号-//

  1. 说明://是整除符号,两个浮点数相除,商也会只保留整数。
  2. 注意:
    • 整除  会进行四舍五入操作;
    • 如果除数和被除数都是整型,则结果也是整型;
    • 如果除数和被除数有其中一个是浮点数,则结果也是浮点数。
  3. 例子:
>>> 1 // 30>>> 2 // 30>>> 2.9 // 30.0>>> 3.1 // 31.0

2 幂运算符

  1. 说明:** 用于计算乘方。
  2. 注意:
    1. 幂运算比取反运算优先级高;
  3. 例子:
>>> 2 ** 38>>> 3 ** 29>>> -3 ** 2-9>>> (-3)** 29

3 长整型

  1. 在后面加 L

4 十六进制和八进制

  1. 说明:
    1. 十六进制:数字以 0x 开头
    2. 八进制:数字以 0o 开关
  2. 例子:
#十六进制>>> 0x1234567890ABCDEF1311768467294899695#八进制>>> 0o1234567342391

5 变量

  1. 使用变量前必须对其赋值。

6 语句

  1. 语句改变了事物(p12)

7 获取用户输入

  1. 说明:input 获取用户输入。raw_input已经不被python3支持
  2. 例子:
>>> num = input("Please input a number:")Please input a number:123>>> print("The number you input is:%r" % num)The number you input is:'123'

8 if 语句

  1. 判断语句
  2. 例子:
if 1 == 2:    print ("1 == 2")if 1 < 2:    print ("1 < 2")1<2

9 函数

  1. 说明:函数就是实现特定功能的小程序。
  2. 例子:
#幂函数>>> pow(2, 3)8>>> 10 + pow(2, 10) / 3351.3333333333333#绝对值函数>>> abs(10)10>>> abs(-10)10#四舍五入函数,将浮点数四舍五入为整数>>> round(3.444)3>>> round (3.5001)4

10 模块

  1. 说明:可以把模块想像成功能集。用 import关键字导入模块
    1. 导入模块的方法1:import 模块
    2. 导入模块的方法2:from 模块 import 函数
  2. 注意:应该多使用第一种模块导入的方法,这样就不会因为不同模块中有相 同函数而导致冲突。
  3. 例子:
#python 3.2中,math.floor直接返回整型,与int的效果相同#使用第一种模块导入方法>>> import math>>> math.floor(32.99999)32>>> int(32.99999)32#使用第二种模块导入方法>>> from math import sqrt>>> sqrt(169)13.0#cmath模块>>> import cmath>>> cmath.sqrt(-1)1j

11 字符串

 

11.1 单引号和双引号

  1. 说明:打印时,单引号和双引号都可以使用。
  2. 注意:在字符串中有单引号时,可以使用双引号,在字符串中有双引号时, 可以使用单引号
  3. 例子
>>> print ("This is a test!")This is a test!>>> print ('I said:"I will be there!"')I said:"I will be there!">>> print ("Let's go!")Let's go!

11.2 转义字符

  1. 说明:对字符串中的引号用 \ 进行转义
  2. 例子:
>>> print ("\"")">>> print ('\'')'

11.3 拼接字符串

  1. 说明:用加法符号拼接 + 字符串
  2. 例子:
>>> print ("Let's say:\"" + "Hello, world!\"")Let's say:"Hello, world!"

11.4 值被转换为字符串的两种机制

  1. 说明:两种机制
    • 用str转换,它会把字符串转换成合法形式的字符串,以便用户可以理 解。
    • 用repr转换,它会创建一个字符串,以合法的Python表达式的形式来表 示值。
#str与repr的区别>>> c = r"c:\nowhere">>> c'c:\\nowhere'>>> print(c)c:\nowhere>>> print(str(c))c:\nowhere>>> print(repr(c))'c:\\nowhere'

11.5 input和raw_input的区别

  1. 说明:在python3中,取消了raw_input函数.
  2. 例子:
>>> name = input("Your name: ")Your name: Bill Gunn>>> print("Hello " + name)Hello Bill Gunn

11.6 长字符串

  1. 说明:
    1. 用三个双引号来引用长字符串.
    2. 在字符串的行尾添加 \
  2. 注意:三个双引号的字符串会保留换行符,而反斜线不会保留,见示例
  3. 例子:
#三个引号的示例>>> print ("""... This is a very long string.... It continues here.... END.... """)This is a very long string.It continues here.END.#反斜线的示例>>> print ("This is a long string \... It continues here.\... This is the end!")This is a long string It continues here. This is the end

11.7 原始字符串

  1. 说明:在字符串前面添加字母 r , 这在正则表达式中非常有用!
  2. 注意:
    1. 原始字符串不能以反斜线结尾;
    2. 如果需要在原始字符串输出时以反斜线结尾,可以按最后一个例子的 方法实现。
  3. 例子:
#反斜线被当成转义字符打印出来>>> print ("C:\nowhere")C:owhere#直接打印反斜线>>> print (r"C:\nowhere")C:\nowher#不能在原始字符串结尾使用反斜线>>> c = r"test\"SyntaxError: EOL while scanning string literal#在原始字符串结尾输出反斜线的方法>>> print (r"C:\windows""\\")C:\windows\

11.8 Unicode字符串

  1. 说明:在字符串前加字母 u 来表示立unicode字符串,但是这个功能已 经在python3中废弃了。
  2. 例子:
>>> u'中文'SyntaxError: invalid syntax
0 0
原创粉丝点击