python小试牛刀

python的基础
1.本节内容

操作系统发展历史

二进制

编码
ASSIC  每一个字符统一都需要8个bit来存储

计算机容量
1位 = 1bit
8bit = 1byte = 1字节
1024bytes = 1kbytes =1KB 1024个字符,小文档 ,几百k可以表示一张图片
1024KB = 1Million Bytes = 1MB = 1兆 , 几万字的文档, 大图片
1024MB = 1Gigabytes , 一个清晰的电影,不是高清,高清能达到数10个g
1024GB = 1TB ,
1024TB = 1PB,

00000000
00110001
00000001

编程语言介绍
什么是编程语言?
定义好一套与计算机交互的语法规则,这套规则 就可称为一门编程语言

	 我们听不懂日语是因为不懂日语的语法规则
	 
	 学编程 == 学语法规则
	 
	 编程能干什么?
		print hello world 
		eject cd 
		
		一堆指令的组合 == 》 软件 
		
	
	有很多套与计算机交互的语法规则, 
	600多种编程语言
	C = 各个操作系统的开发语言 1973
	C++ = C++是C语言的加强版   ,1983年,贝尔实验室的Bjarne Stroustrup在C语言基础上推出了C++[1]  。 C++进一步扩充和完善了C语言,是一种面向对象的程序设计语言。
	
	java = 1995 由sun 公司开发出来,java 虚拟机 支持跨平台 
	
	php = 1994, 纯web开发语言, 1994 Netscape 浏览器诞生了
	
	python =  1989年诞生, 刚开始被做为脚本语言, 开发小任务, 跟linux同年诞生,89,1991,苏联解体, 1991年正式版本
	
	
	C# = c sharpe =C#是微软公司发布的一种面向对象的、运行于.NET Framework之上的高级程序设计语言。并定于在微软职业开发者论坛(PDC)上登台亮相。C#是微软公司研究员Anders Hejlsberg的最新成果。C#看起来与Java有着惊人的相似;它包括了诸如单一继承、接口、与Java几乎同样的语法和编译成中间代码再运行的过程。但是C#与Java有着明显的不同,它借鉴了Delphi的一个特点,与COM(组件对象模型)是直接集成的,而且它是微软公司 .NET windows网络框架的主角。
	
	ruby = 
		Ruby, 一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp 语言。由 Ruby 语言本身还发展出了JRuby(Java平台)、IronRuby(.NET平台)等其他平台的 Ruby 语言替代品。Ruby的作者于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。因为Perl发音与6月诞生石pearl(珍珠)相同,因此Ruby以7月诞生石ruby(红宝石)命名。
		Ruby on rails web框架
	
	perl = Unix平台上开发出来的语言,做文字处理非常强大, 可以写出没人能看懂的代码
	
	shell = 脚本语言, 简单易学,基于unix,linux, 做一些简单的系统管理任务, 运维人员必学
	
	scalar = Scala是一门多范式的编程语言,一种类似java的编程语言[1] ,大数据开发
	
	erlang = 是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信,函数式编程
		
	go ===Go语言是谷歌2009发布的第二款开源编程语言。Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。
	
	javascript = 是当下使用最为广泛的语言,主要写前端的语言,
				nodejs =后端 全栈式的语言
				
	vb = 微软的脚本语言,bat脚本
	
	
	lua = nginx 的脚本语言, ngnix 是时下最nb web服务器

python初探
发展历史

编译型 = 全部翻译,再执行  ,翻译=编译  ,c,c++
解释型 = 边执行边翻译, python php java c# perl ruby javascript 










环境安装
第一个python程序

2.Python介绍
发展史
被解救的姜戈
2.4 50万行
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

print "hello world" #in 2.x
print("hello world") #in 3.x


In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

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

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.

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

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).

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

系统位数

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


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

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

Python 2 or 3?
安装
交互器模式

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


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




D:\>"d:\Program Files\Python35\python.exe" c:\hello.txt
Hello World!
	

#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 

环境变量

D:\Program Files\Python35\Scripts\;
D:\ProgramFiles\Python35\;
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
D:\Program Files\Python27


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

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

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.

变量的命名规则
1. 要具有描述性
2. 变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
3. 不能以中文为变量名
4. 不能以数字开头
5. 保留字符是不能被使用



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


内存何时释放?


字符编码
支持中文的第一张表就叫 GB2312 

1980 gb2312 6700+
1995 gbk1.0 20000
2000 gb18030 27000 
big5 台湾

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


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

Assic -->Gb2312 ->gbk1.0-->gb18030 
Assic -->unicode -->utf-8   /utf-16 


Python2.x == Assic 默认编码
	#!-*- coding:utf-8 -*- 
	#coding:utf-8 
	
python3.x == unicode默认编码 


unicode 是向下兼容gb2312 , gbk 

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

用户输入

模块初识
.pyc是个什么鬼?
数据类型初识
数据运算
表达式if …else语句
缩进 IndentationError: expected an indented block
^
IndentationError: unindent does not match any outer indentation level
SyntaxError: invalid syntax 语法错误
tab != 4个空格

缩进级别必须保持一致 
3.一些常见的例子
print(“hello world”)

变量 : 存储信息的,日后被调用、修改操作
常量: 固定不变的量,字母大写
命名规则:

  1. 字母数字下划线组成
  2. 不能以数字开头,不能含有特殊字符和空格
  3. 不能以保留字命名
  4. 不能以中文命名
  5. 定义的变量名应该有意义
  6. 驼峰式命、 下划线分割单词
  7. 变量名区分大小写
    a=1
    b=2
    if a<b:
    print(“Yes”)
    print(“Yes”)
    print(“Yes”)
    print(“Yes”)
    else:
    print(“No”)
    a=1
    b=2

if a>b:
print(“Yes”)

elif a==b:
print(“第三”)

else:
print(“any”)

if 条件1:
自拍
elif 条件2:

else:
跳舞

单行注释

‘’‘多行注释’’’
“”" 多行注释 “”"

input()

“abc” + “qwe”

file.py
文件的扩展名:
.py : python的程序文件
.txt : 文本文件
pdf chm html doc xml xls ppt

jpg png gif jpeg bmp
avi rmvb MP4 mkv 3gp
WMV MP3 flue mid

True 真 正确的
False 假 错误的

a
b = 100
c = 1000

if b <= a <= c:
print(“True”)

num number

num1 = intpu(“Num1:”)
num2 = intpu(“Num2:”)
num3 = intpu(“Num3:”)

输出三个数字中的最大值/最小值

if num1>num2>num3:
#num1最大

else:
#num1<num2

if num1>num2>num3 #num1最大
elif num1>num3>num2 #num1最大
elif num2>num1>num3 #num2最大
elif num2>num3>num1 #num2最大
elif num3>num2>num1 #num3最大
else

num3>num1>num2 #num3最大

num1 num2 num3

max_num =0

if num1>num2:
max_num= num1
if max_num > num3:
print(“Max NUM is”,max_num)
else:
print(“Max NUM is”,num3)
else:
max_num = num2
if max_num > num3:
print(“Max NUM is”,max_num)
else:
print(“Max NUM is”,num3)

num += 1 等价于 num = num + 1
num -= 1 等价于 num = num - 1
num *= 2 等价于 num = num * 2
num /= 2 等价于 num = num / 2
num //= 2 等价于 num = num // 2
num %= 2 等价于 num = num % 2
num **= 2 等价于 num = num ** 2

and 且,并且
只有两个条件全部为True(正确)的时候, 结果才会为True(正确)

条件1 and 条件2
5>3 and 6<2 True

or 或,或者
只要有一个条件为True,则结果为Ture,
5>3 or 6<2
真 或 假

not 不,雅蠛蝶

not 5>3 == False
not 5<3 == True

a>b and (c>d or (not f))

(not (not True)) or (False and (not True))

条件1 and 条件2
条件1 or 条件2
短路原则
对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式 的计算结果就一定为假,第二个条件就不会被计算

对于or
如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式 的计算结果就一定为真,第二个条件就不会被计算

True or True and False

猜年龄

age = 50

user_input_age = int(input(“Age is :”))

if …

while 循环

while 条件:
print(“any”)
print(“any”)

num = 1

while num<10: # 2
print(num) # 2
num+=1 # 3
if num == 9: # 3
break

num = 1

while num<=100: # num<=100 等价于 True
# while num<=100: 等价于 while True:
if num%2 == 0:
print(num)
num += 1

num = 1

while num<=100:
if num%2 == 1:
print(num)
num += 1

age = 50

#user_input_age = int(input(“Age is :”))

flag = True

while flag:
user_input_age = int(input(“Age is :”))
if user_input_age == age:
print(“Yes”)
flag =False
elif user_input_age > age:
print(“Is bigger”)
else:
print(“Is smaller”)

print(“End”)

break # 终止
age = 50

#user_input_age = int(input(“Age is :”))

#flag = True

break

while True:
user_input_age = int(input(“Age is :”))
if user_input_age == age:
print(“Yes”)
break
elif user_input_age > age:
print(“Is bigger”)
else:
print(“Is smaller”)

print(“End”)

continue 继续

if a>b and d<f or 5>3 and d == e:

while 条件:

else:

statement 语句

num = 1
while num <= 10:
num += 1
if num == 5:
break
print(num)
else:
print(“This is else statement”)

while 条件1:

while 条件2:

11=1
1
2=2 22=4
1
3=3 23=6 33=9

#print(“hello world.”,end="") # \n \r\n \r
#print(“hello world.”,end="
")
#print(“hello world.”,end="__")

num1 = 0

while num1<=5:
print(num1,end="_")
num2 = 0
while num2<=7:
print(num2,end="-")
num2+=1

num1+=1
print() #  print(end="\n")

#0_0-1-2-3-4-5-6-7-
#1_0-1-2-3-4-5-6-7-

Height 高度
width 宽度

height = int(input(“Height:”)) # 用户输入一个高度
width = int(input(“width:”)) # 用户输入一个宽度

num_height = 1
while num_height <=height:
num_width = 1
while num_width <= width:
print("#", end="")
num_width += 1
print()
num_height += 1

12345678
22345678
32345678
42345678

第一行的时候 8字符 8次循环
第二行的时候 8字符 8次循环
第三行的时候 8字符 8次循环
第四行的时候 8字符 8次循环

width = int(input(“width:”))

num_width = 1

while num_width<=width:
print("#", end="")
num_width +=1
print()

num_width = 1
while num_width<=width:
print("#", end="")
num_width +=1
print()

num_width = 1
while num_width<=width:
print("#", end="")
num_width +=1
print()

num_width = 1
while num_width<=width:
print("#", end="")
num_width +=1

#print("#",end="")
#print("#",end="")
#print("#",end="")
#print("#",end="")

print()

num = 4
while num>0:
    print("#", end="")
    num -= 1
print()

# @ == ####

height = int(input(“Height:”)) # 用户输入一个高度
width = int(input(“width:”)) # 用户输入一个宽度

#num2 = height

num2 = height # 第一步: 赋值
while num2 > 0: # 第二步 :num2 == 2

num1 = width     # 第三步: 赋值
while num1>0:   # 第四部:num1==2   # 第七步:num1 = 1 
    print("#", end="")  # 第五步: 不换行 打印一个#   第八步: 不换行 打印一个#
    num1 -= 1   #第六步: num1 = 1   第九步: num1 = 0
print()  # 第十步 : 只是换行
num2 -= 1  # 第十一步 : num2=1

print(“1*1=”,1)

“11=",1 == str(m)+"”+str(n)+"=",1

m = 2
n = 2

print( str(m)+ "" + str(n) + “=” , mn )

line = 5 # 第一步 : 赋值
while line>0: # 第二部 line=5

tmp = line  #  第三部 : tmp =5   tmp=4

while  tmp>0:  #  第四部 : tmp =5   #第七步 tmp=4    #第十部: tmp=3   第十三步 tmp=2
    print("*",end="") #第五步   #第八部        #第十一部               #第十四步
    tmp = tmp-1   # 第六步 tmp = 4  # 第九步 tmp=3  # 第十二步 tmp=2   第十五步 tmp= 1 
    
print()
#print(line)
line -= 1

first = 1
while first<=9:

sec = 1    
while sec <= first:
    print(  str(sec)+"*"+ str(first) +"="+str(sec * first), end="\t")
    sec += 1

    
print()


first += 1

猜你喜欢

转载自blog.csdn.net/weixin_43559339/article/details/83990305