文档介绍:Python3入门
*
PythonPython入门
目录
1
Python概述
2
变量和内建数据类型
3
语句
4
函数
5
标准库模块
6
程序调试
*
PythonPython入门
>>> print("Hello world!")
Hello world!
>>> 2**10
1024
>>> j=0
>>> for i in range(1,11,1):
... j = j+i
...
>>> print(j)
55
>>>
Python的安装
下载:
版本:windows、linux、源码
开发
集成开发环境:IDLE (Python GUI - 32 bit)
命令行:Python (command line - 32 bit)
手机:qpython
范例
*
PythonPython入门
Python语言构成
语法规则:注释#、缩进、“:”、单引号、双引号等
数据类型:int、str、float等(python3里面这些都是类)
语句:if、else、elif、while、def、import等
函数:print()、range()、help()等
对象:os、sys等
忘记了语句,不会用?——help()函数
不用了,要退出?——exit()函数
*
PythonPython入门
变量和数据类型
>>> a = 'Hello world!' # 赋值语句
>>> print (a)
'Hello world!'
>>> type(a) # 表达式: 在交互模式下输出值
<class 'str'>
>>> n = 12
>>> print (n)
12
>>> type(n)
<class 'int'>
>>> n =
>>> type(n)
<class 'float'>
变量在它们被赋值的时候创建
无需声明
变量名是大小写敏感的: 'val'和 'Val'不一样!
变量的类型由 Python 决定
变量可以在任何时候重新赋值为任何其他类型任何其他值
>>> n = 'apa'
>>> print (n)
'apa'
>>> type(n)
<class 'str'>
*
PythonPython入门
数字 number
整数: 12 0 -12987 0X1A2
class 'int'
十六进制是以 0X 开头, 数字范围为 0-9 和A-F
浮点数: 1E1 --21
class 'float'
精度和范围与 C语言的 double型相同
复数: 1+3J
class 'complex'
*
PythonPython入门
数字表达式
通常的数字表达式运算符: +, -, /, *, **, %, //
运算优先级和括号正常工作
>>> 12+5
17
>>> 12+5*2
22
>>> (12+5)*2
34
>>> a=12+5
>>> print (a)
17
>>> b = + a # a is converted to float automatically
>>> b
>>> print (b)
>>> 4 +
>>> 1 + **2
>>> 1+2j + 3-4j
(4-2j)
*
PythonPython入门
逻辑表达式(布尔表达式)boolean
True 和 False 为预定义值; 实际上是整数1 和 0
值 0 作为 False, 所有其他值是 True
常见的布尔表达式运算符: not, and, or
>>> True or False
True
>>> not ((True and False) or True)
False
>>> True * 12
12
>>> 0 and 1
0
比较运算符产生布尔值
比较运算符: <, <=, >, >=, ==, !=
>>> 12<13
True
>>> 12>13
False
>>> 12<=12
True
>>> 12!=13
True
*
PythonPython入门
字符串 string
单引号和双引号均可用于字符串
生成一样的字符串值
字符串中的特殊符号 : \n newline, \t tab, others
三引号(单、双均可)用于大块的文本内容
>>> a = 'Hello world!'
>>> b = "He