Python 基础

1.Python介绍

1.1python各个版本发布时间
Python 2.6 - October 1, 2008
Python 2.6.1 - October 1, 2008
Python 2.6.6 - October 1, 2008
Python 3.0 - December 3, 2008

Python 2.7 - July 3, 2010 #目前业内主流使用的工业版本依然是2.7
In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

总结: python2.x 是遗产, python3.x是现在和未来的语言

1.2.1官方对于python版本的介绍

Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010,

with a statement of extended support for this end-of-life release. The 2.x branch will see no new major releases after that.

3.x is under active development  and has already seen over five years of stable releases, including version 3.3 in 2012,

3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only

available by default in Python 3.x.

Python 3.0 于2008年发布,2代的最终版本2.7已于2010年中旬发布,并且声明延长2代版本的最终支持,但是2代版本从2.7开始不再有新的版本发布。

3代版本正在活跃的开发当中,并且在五年以上的时间内都已经有了稳定的版本发布,包括2012年发布的3.3版本,2014年发布的3.4版本和2015年发布的3.5版本。

这意味着近期标准库的升级和改进将只能默认在3代版本上使用。

Guido van Rossum (the original creator of the Python language) decided决定 to clean up清理 Python 2.x properly适当的, with less regard不管 for backwards向后 compatibility兼容 than is the case for new releases in the 2.x range. The most drastic激进的 improvement is the better更好的 Unicode(是一个编码)中文支持 support (with all text字符 strings being Unicode by default) as well as saner bytes/Unicode separation.

Besides另外, several几个 aspects方面 of the core核心 language (such as print and exec being statements, integers using floor division) have been adjusted调整为 to be easier更简单的 for newcomers新手 to learn and to be more consistent持续的 with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x).

 

 

 1.2.2总结

 python 2 vs 3
  1. 默认支持中文
  2. 不兼容2.x
  3. 核心语法调整,更易学
  4. 新特性默认只在3.x上有


2.Python 2 or 3?

2.1安装

windows

1
2
3
4
5
6
7
1、下载安装包
    https://www.python.org/downloads/
2、安装
    默认安装路径:C:\python27
3、配置环境变量
    【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
    如:原来的值;C:\python27,切记前面有分号

linux、Mac

1
 
2
无需安装,原装Python环境
 
ps:如果自带2.6,请更新至2.7


2.2交互器模式
开始--》cmd --> cd c:\ -->dir
cd = change directory
dir = 查看当前目录文件列表

cd .. 返回上一层目录
cd ../.. 返回上上一层目录
cd ../../.. 返回上上上一层目录
cd ../../../.. 返回上上上上一层目录

cmd下 tab按键补全

#c:\hello.txt .txt 代表文件的扩展名(后缀名),

拓展名用于区分文件类型
.txt 记事本文本文件
.doc word文件
.xls excel文件
.ppt PPT文件
.exe 可执行文件
.jpg .png .jpeg 图片
.gif 动态图片
.pdf PDF文件
.mp4 .avi 视频
.py python文件
.java java 文件
.c .h c源码
.php php文件
.js javascript

 

3.Hello World程序

3.1变量

 

3.1.1变量的定义

变量 是 为了存储 程序运算过程中的一些中间 结果,为了方便日后调用

官方定义:

Variables(变量) are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information(如果我们把变量看作成是一个保存信息的容器是更容易理解的). Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

 

3.1.2变量的命名规则

1. 要具有描述性(让别人看到变量名能够了解变量是干什么的)
2. 变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
3. 不能以中文为变量名
4. 不能以数字开头
5. 保留字符是不能被使用(例如:python中print不能作为变量名)

变量名例子:
student_number = 30 #python 官方建议

studentNumber = 30 #驼峰体

 

3.2Python文件执行

执行py程序方式为:
1 交互器,缺点程序不能永久保存,主要用与简单的语法测试相关

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print('hello world!')
hello world!
>>>

2. 文件执行

print('hello world!')


3.3 常量

常量 :不变的量

例如:pie = 3.141592653.... 

在py里面所有的变量都是可变的 ,所以用全部大写的变量名来代表次变量为常量

 

3.4Python运行内存问题

内存何时释放?
Python 自动回收内存,不用担心变量定义太多。

Python中删除变量名,释放内存的方法
1.del 变量名
2.变量名=‘新的变量’, 原来变量会被Python自动删除


3.5字符编码

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。

为了处理汉字,程序员设计了用于简体中文的GB2312和用于繁体中文的big5。

支持中文的第一张表就叫 GB2312

1980 gb2312 6700+
1995 gbk1.0 20000
2000 gb18030 27000 现在主流使用的中文编码。Windows默认编码
big5 台湾

unicode 万国码 支持所有国家和地区的编码
2**16 = 65535 = 存一个字符 统一占用2个字节


UTF-8 = unicode 的扩展集,可变长的字符编码集

 

3.6总结:
1.

   Assic -->Gb2312 ->gbk1.0-->gb18030 中文编码的发展过程

   Assic -->unicode -->utf-8 /utf-16 世界编码的发展过程

2.

   2.x = 默认编码 =ASSIC =不支持中文

   3.x = 默认编码 =UNICODE =默认支持中文

3.

  Python2.x中输入中文需要增加声明

   #!-*- coding:utf-8 -*- 

   #coding:utf-8 

4.   

   为什么Unicode的程序,能够在Windows的cmd(使用GBK编码)下显示中文?

   unicode 是向下兼容gb2312 , gbk 


5.

   为什么看不到UTF-8 的中文?

   因为UTF-8将编码重新编排,而Unicode是将编码直接整合。

 

5.注释

单行注释 用#
多行注释用三个单引号或三个双引号 '''被注释的内容'''


6.用户输入

变量=input('xxx:')

#事例代码计算剩余年龄

death_age = 80 #设定一个变量,为最终死亡年龄

print (type(death_age)) #显示变量death_age的数据类型

name = input("your name:")  #用户输入姓名
age = input ("your age:") #用户输入年龄
#input接受的所有数据都是字符串,即便你输入的是数字,但依然会被当成字符串来处理。

print (type(age))  #输出age变量的数据类型

print('Your name:',name )
print ('You can still live for',death_age - int(age),'years ...') 
print ('You can still live for'+str(death_age - int(age))+'years ...') #字符串之间用+拼接 
#operand 运算符
#int integer = 整数 把字符串转成数字用int(被转的数据)
#str string=字符串 把数据转成字符串用str(被转的数据)

 

 7.表达式if ...else语句

(必须连接在一起使用,属于一个整体)

 

7.1示例代码

age_of_princal = 56
guess_age = int( input('>>:'))

if guess_age == age_of_princal:
  print('Yes')
elif guess_age < age_of_princal:
  print('Try bigger...')
else:
  print('Try smaller...')

 

注意:

Python中确定不同级别的代码需要缩进

缩进原因:条件执行完成后,要确定执行哪段代码。

缩进级别必须保持一致 (几个空格没关系,重要的是一直,但是官方规定4个空格)

 

 

补充:
IndentationError缩进错误
invalid syntax 语法错误
tab != 4个空格

 

系统位数

32bit =内存的最大寻址空间是2**32, 4GB 
64bit, =2**64 但实际上支持不到这莫大的内存,2**4x, 目前主板支持的最大的内存是100多GB


4gb ==64位系统 会不会比32位系统快? = 一样的 

硬盘:
5400转 = 每分钟 =骑自行车
7200转 = 每分钟 =骑电动车
10000转 = 每分钟 =骑快速电动车
15000转 = 每分钟 =骑摩托车 机械硬盘最快的

SSD = Tesla 

posted @ 2017-08-02 23:06  Jimmyshao  阅读(226)  评论(0)    收藏  举报