Python之基础2

1.Python2 和 Python3 的版本比较

py2.7是2.x系列的最后一个版本,已经停止开发,不再增加新功能。2020年终止支持。

所有的最新的标准库的更新改进,只会在3.x的版本里出现。

龟叔决定清理Python2.x ,并且不再兼容旧版本。 最大的一个改变就是使用Unicode作为默认编码。Pyhton2.x中直接写中文会报错,Python3中可以直接写中文了。

从开源项目看,支持py3的比例已经大大提高,知名的项目一般都支持py2.7和py3+。

py3比py2更规范统一、去掉了没必要的关键字

Python3.x还在持续改进,将来的开发会在这个版本上进行。

 

2.字符编码

  在python2.x中默认编码==Assic码,如果需要支持中文

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

    #coding:utf-8

  在python3.x中默认编码==unicode直接支持中文

  下面来详细写一下字符编码

在计算机发展之初,人机交互使用的是Assic编码,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号

支持中文的第一张表是GB2312(1980,6700+),之后扩展到 gbk1.0(1995,20000),Gb18030(2000,27000)

unicode万国码出现,为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,2 **16 = 65536存一个字符统一占用2字节

UTF-8=是unicode的扩展集,使用可变长的字符编码集,即原来英文字符依旧占8位一字节,欧洲语言占用2字节,亚洲语言占用3字节

 

3.用户输入

name = input("Your name : ")
print("hello",name)

 执行一下:
c:\test>python input.py
Your name : miao
hello miao

用户输入的数据类型默认为string,如果执行以下程序

death_age=100
age = input("Your age : ")print("You can live for ",death_age-age,"years")

 

会报错如下:
c:\test>python input.py
Your age : 1
Traceback (most recent call last):
  File "input.py", line 3, in <module>
    print("You can live for ",death_age-age,"years")
TypeError: unsupported operand type(s) for -: 'int' and 'str'

c:\test>

需要强制转换一下数据类型

death_age=100
age = int(input("Your age : "))
print("You can live for ",death_age-age,"years")

 

即可成功执行
c:\test>python input.py
Your age : 1
You can live for  99 years

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法(等待补充)

 

4.输出及字符串拼接

print() #打印回车,等价于 print(end="\n")
print("",end="")#不换行
print("I am ",age,"years old...")
print("I am "+age+"years old...")#看两者之间是有区别的,使用“,”分隔打印有空格,"+"则没有空格直接连起来

 

5.条件语句if...else...

  场景1:用户名密码登录

import getpass

name = input("请输入用户名...")
pwd = getpass.getpass("请输入密码...")

if name == "miao" and pwd =="123":
    print("welcome  miao!")
else:
    print("用户名或密码错误")

 

  场景2:让用户输入三个数字,比较大小,打印出最大值

number1 = input("please input number1: ") 
number2 = input("please input number2: ") 
number3 = input("please input number3: ") 

max_number = 0

if number1 > number2:
    max_number = number1
    if max_number > number3:
        print("the max number is : ",max_number)
    else:
        print("the max number is : ",number3)
else:
    max_number = number2
    if max_number > number3:
        print("the max number is : ",max_number)
    else:
        print("the max number is : ",number3)
    

  场景3:猜年龄

age_of_miao = 5
age = int (input("guess the age of miao :"))
if age == age_of_miao:
    print("yes,you got it")
else:
    print("No")

 

6.循环语句while

请注意结束条件,切勿死循环

场景1:打印0-100

i = 1
while i <= 100:
    print("i = ",i)
    i = i+1

  场景2:猜年龄游戏  给三次机会以及一直到猜对为止

age_of_miao = 5
times = 0
while times <3:
    age = int (input("guess the age of miao :"))
    if age == age_of_miao:
        print("猜对啦")
        break
    elif age < age_of_miao:
        print("小了,往大点猜。。")
    else:
        print("大了,往小了猜猜。。")
    times +=1
else:
    print("三次错误,你没有机会啦!")
age_of_miao = 5
flag = True
while flag:
    age = int (input("guess the age of miao :"))
    if age == age_of_miao:
        print("猜对啦")
        flag = False
    elif age < age_of_miao:
        print("小了,往大点猜。。")
    else:
        print("大了,往小了猜猜。。")
        
print("游戏结束!")

  循环嵌套

场景1:使用 #号输出一个长方形,用户可以指定宽和高,如果长为3,高为4,则输出一个横着有3个#号,竖着有四个#号的长方形

heigth = int(input("请输入高度:"))
width = int (input("请输入宽度:"))
num_heigth =0

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

场景2:打印

*****
****
***
**
*

line =5
while line >0:
    tmp =line
    while tmp>0:
        print("*",end="")
        tmp -=1
    line -=1
    print()

场景3:打印九九乘法表

i=1

while i<=9:
    j=1
    while j<=i:
        print(str(i)+"*"+str(j)+"="+str(i*j),end="\t")
        j+=1
    print()
    i+=1
    

 

 

 

 

posted @ 2016-08-20 12:59  *chunli*  阅读(142)  评论(0编辑  收藏  举报