1.python基础

python介绍

  • python的出现

1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器。
他希望这个新的叫做Python的语言,能符合他的理想:
创造一种C和shell之间,功能全面,易学易用,可拓展的语言。

  • python的主要应用

云计算、web开发、人工智能、系统运维、金融、图形GUI

python是什么类型的语言

  • python是一门动态解释型强类型定义语言。
  • 编译型和解释型
    • 编译型

将源代码编译成机器码,并以二进制文件形式保存。
* 优点
一次编译,运行时计算机直接执行,程序执行效率高。
* 缺点
修改源代码后需要重新编译。
由于编译成的机器码依赖于操作系统环境,不同的操作系统之间移植会有问题,
需要根据运行的操作系统环境编译不同的机器码。
开发速度慢。
* 解释型
程序执行时才一条一条的解释成机器码给计算机执行,所以执行速度不如编译型。
* 优点
移植性好,只需在运行的系统上装上解释器(虚拟机)。
修改源代码,立即生效。
开发速度快。
* 缺点
每次执行程序时都要解释一遍,执行效率不如编译型。

  • 动态语言(动态类型语言)和静态语言(静态类型语言)
    • 动态语言

在程序执行时才去做数据类型检查,也就是说定义变量时不用指定数据类型,
该语言会自动识别数据类型。
* 静态语言
在程序编译的时候做数据类型检查,所以定义变量时需要声明变量的数据类型。

  • 强类型定义语言和弱类型定义语言(是否允许隐式转换)
    • 强类型

一旦一个变量被声明了数据类型,不经过强制转换,那么它就永远是这个数据类型。
* 弱类型(隐式转换)
一个变量可以赋不同数据类型的值。

python的优缺点

  • 优点

1.python的定位是“优雅”、“明确”、“简单”,初学者入门容易。
2.开发效率非常高,Python有非常强大的第三方库,
基本上你想通过计算机实现任何功能,Python官方库里都有相应的模块进行支持。
3.可移植性好。
4.可扩展性,可以在python代码中调用C/C++。

  • 缺点

1.执行效率不高。
2.代码不能加密。
3.线程不能利用多CPU(GIL全局解释器锁)。

python的种类(各种语言实现的python解释器)

  • cpython(c实现)
  • ipython(基于cpython的一个交互式解释器)
  • pypy(python实现)
  • jython(java实现)
  • ironpython(c#实现)

python2和python3

  • 宏观上的区别
    • python2

    源码重复率高,不规范

    • python3

    规范化

python基础

  • 运行python代码
    • 交互式
	➜  ~ python
	Python 2.7.12 (default, Dec  4 2017, 14:50:18)
	[GCC 5.4.0 20160609] on linux2
	Type "help", "copyright", "credits" or "license" for more information.
	>>> print('hello world')
	hello world
* 执行.py文件
	# hello.py

	print('hello word')
	➜  ~ python hello.py
	hello world
  • .py文件中指定解释器
# hello.py

#!/usr/bin/env python
print('hello world')

➜  ~ chmod 755 hello.py
➜  ~ ./hello.py
hello world
  • 内容编码
    • ascii(一个字节, 只能显示英文,特殊字符,数字)
    • unicode(至少两个字节,万国码,最开始2个字节,之后中文不够变4个字节)
    • utf-8是对unicode编码的压缩和优化(ascii码字符1个字节,欧洲字符2个字节,亚洲的字符3个字节)
    • gbk(只是国内使用,一个中文2个字节)
    • python2默认对内容进行ascii编码(可以指定编码)
	# -*- coding:utf-8 -*-
	# 添加在文件头部
* python3默认对内容进行utf-8编码
  • 注释
    • 单行注释
	# 注释内容
* 多行注释
	"""
	注释
	内容
	"""

	'''
	注释
	内容
	'''
  • 变量
    • 用来存储程序运行中临时结果,以便后续的代码使用。
    • 变量命名规则:数字、字母和下划线组合,不能以数字开头
    • 变量名应该有意义,不能和python关键字冲突
  • 常量
    • 程序执行过程中不变的值
    • 建议常量名全部大写字母
  • 程序的交互
    • 输入(input接收的所有输入默认都是字符串格式)
	username = input('username:')
	password = input('password')
* 输出
	print(username)
	print(password)
  • 基础数据类型
    • 数字类型
      • int
      • long(python3中没有)
      • float
	i = 2
	print(i)
	print(type(i)) # 查看数据类型
	f = 3.14
	print(f)
	print(type(f)) # 查看数据类型
* 字符串类型(str)(字符串可以进行"相加"、和整数"相乘")
	s1 = 'abc'
	print(s1)
	print(type(s1))
	s2 = "Ab" + "c"
	print(s2)
	print(type(s2))
	s3 = '''aBc''' * 2
	print(s3)
	print(type(s3))
	s4 = """abC"""
	print(s4)
	print(type(s4))
* 布尔类型(bool)
	print(True)
	print(type(True))
	print(3 > 2)
	print(False)
	print(type(False))
	print(1 > 2)
* 列表(list)
	l = [1, 2, 3, [1, 2]]
	print(l)
	print(type(l))
* 元组(tuples)
	t = (1, 2, 3, [1, 2])
	print(t)
	print(type(t))
* 字典(dict)
	d = {'name': 'ret', 'gender': 'male'}
	print(d)
	print(type(d))
* 集合(set)
	s = {'red', 'green', 'blue'}
	print(s)
	print(type(s))
  • 数据类型划分
    • 可变数据类型(不可哈希)
      • 列表(list)
      • 字典(dict)
      • 集合(set)
    • 不可变数据类型(可哈希)
      • 字符串(str)
      • 布尔(bool)
      • 数字(int)
      • 元组(tuple)
  • 类型转换
    • int-->str
>>> s = str(123)
>>> s
'123'
>>> type(s)
<class 'str'>
* str-->int
>>> i = int('123')
>>> i
123
>>> type(i)
<class 'int'>
* int-->bool
>>> b = bool(1)
>>> b
True
>>> type(b)
<class 'bool'>
>>> b = bool(0)
>>> b
False
>>> type(b)
<class 'bool'>
* bool-->int
>>> i = int(True)
>>> i
1
>>> type(i)
<class 'int'>
>>> i = int(False)
>>> i
0
>>> type(i)
<class 'int'>
* str-->bool
>>> b = bool('')
>>> b
False
>>> type(b)
<class 'bool'>
>>> b = bool('0')
>>> b
True
>>> type(b)
<class 'bool'>
  • 格式化输出(占位符)
    • %d代表整数
    • %s代表字符串
    • %f代表浮点数
    • %e代表科学记数法
    • %%输出%号
	print('my name is %s' % ('ret'))
  • 基本运算符
    • 算术运算符
	+
	-
	*
	/
	% # 取余
	** # 幂
	// # 取整除
* 比较运算符
	==
	!=
	<> # 不等于
	>
	<
	>=
	<=
* 赋值运算符
	=
	+=
	-=
	*=
	/=
	%=
	**=
	//=
* 逻辑运算符
	* 优先级()>not>and>or
		and
		or
		not

		3>4 or 4<3 and 1==1
		1 < 2 and 3 < 4 or 1>2 
		2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
		1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
		1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
		not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
	* x or y , x为真,值就是x,x为假,值是y
	* x and y, x为真,值是y, x为假,值是x
		8 or 4
		0 and 3
		0 or 4 and 3 or 7 or 9 and 6
* 成员运算符
	in
	not in

	'a' in 'abc'
	'a' not in 'abc'
  • 流程控制-选择
    • 单分支
	if condition:
		pass
* 双分支
	if condition:
		pass
	else:
		pass
* 多分支
	if condition1:
		pass
	elif condition2:
		pass
	elif condition3:
		pass
	else:
		pass
  • 流程控制-循环
    • 循环
	while condition:
		pass
* continue终止本次循环,继续下次循环
	while condition1:
		if condition2:
			pass
			continue	
		pass
* break终止整个循环
	while condition1:
		if condition2:
			break
		pass
* while...else...(while循环正常执行完,才执行else下的代码)
	#!/usr/bin/env python
	i = 5
	while i > 0:
	    if i == 0:
	        break
	    print(i)
	    i = i - 1
	else:
	    print('whole')
posted @ 2018-07-03 22:58  ret  阅读(187)  评论(0)    收藏  举报