Python 第1周

编程入门 行业动态 更新时间:2024-10-06 04:06:08

<a href=https://www.elefans.com/category/jswz/34/1770869.html style=Python 第1周"/>

Python 第1周

内容

  1. Python介绍
  2. 安装
  3. Hello World程序
  4. 变量
  5. 用户输入
  6. if..else流程判断
  7. while循环 
  8. for循环
  9. 作业需求

 

 3 Python入门

 在后面的操作中,主要以python 3.4 版本为主:

Python脚本执行

1 在linux 下创建一个文件叫hello.py,并输入:
2print("hello world ")
3然后执行命令:python hello.py ,输出:
4$ python hello.py
5Hello World!

 

 指定解释器

上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。

如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py ,那么就需要在 hello.py 文件的头部指定解释器

1#!/usr/bin/python env
2print ("hello world")

执行前:chmod +x hello.py

就可以如下执行:./hello.py 即可。

 

在交互器中执行

可以直接调用python自带的交互器运行代码 :

 

4、变量\字符编码

 1、声明变量:

#_*_coding:utf-8_*_

name = 'Chen"
print name

 解析:#_*_coding:utf-8_*_ 指定字符编码 ,name  为变量名, 变量名的值为:“Chen” ;print 打印

 

2、变量的赋值:

1name  = "chen"
2name2 = name
3 
4print ("1、" ,name,name2)
5 
6name = "ChcnChangQing"
7print ( "2、",name,name2)

结果打印:

 解析:给name 赋一个值常量固定值,name2等于name 的值,同时python内存池也给name 分配了一个模块值, name、name2的指针指向同一个值。 所以第一次打印,name、name2的值是一样的;第二次重新指定name的值是,python会划分一个地址池,name的指针发生改变指向新的地址池,这个时候name2并不会随着name的改变而改变,还是指向原理的地址池。所以第二次打印出来的结果值,name与name2不一样。

 

3、变量定义规则:

  • 变量名只能是字母、数字或下划线的任意组合,注意变量名称数字不能放在前面 。如:2ff = "chen"  格式错误;_2ff = "chen" ; ff2 = "chen"  这种类格式是可以正常识别的
  • 变量名的第一个字符不能是数字
  • 以下关键字不能做为变量名(python规定):

   ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

 

4、字符编码与二进制:

A、数字与二进制的换算:

描述:定位数字的值,往前去累加。当当前数值大于排列数字就在下方标1 如:165 小于256 大于128,所以在128下标1,再往前推算,排列的数据小于剩下的数值 (165-128),下方标零,截止排列数据的和等于165=128+32+4+1,从中推算出二进制为:10100101

 

B、字符编码

 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认Ascill)

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

 

 

 关于中文的发展(摘录):

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

GB2312(1980年)一共收录了7445个字符,包括6763个汉字和682个其它符号。汉字区的内码范围高字节从B0-F7,低字节从A1-FE,占用的码位是72*94=6768。其中有5个空位是D7FA-D7FE。

GB2312 支持的汉字太少。1995年的汉字扩展规范GBK1.0收录了21886个符号,它分为汉字区和图形符号区。汉字区包括21003个字符。2000年的 GB18030是取代GBK1.0的正式国家标准。该标准收录了27484个汉字,同时还收录了藏文、蒙文、维吾尔文等主要的少数民族文字。现在的PC平台必须支持GB18030,对嵌入式产品暂不作要求。所以手机、MP3一般只支持GB2312。

从ASCII、GB2312、GBK 到GB18030,这些编码方法是向下兼容的,即同一个字符在这些方案中总是有相同的编码,后面的标准支持更多的字符。在这些编码中,英文和中文可以统一地处理。区分中文编码的方法是高字节的最高位不为0。按照程序员的称呼,GB2312、GBK到GB18030都属于双字节字符集 (DBCS)。

有的中文Windows的缺省内码还是GBK,可以通过GB18030升级包升级到GB18030。不过GB18030相对GBK增加的字符,普通人是很难用到的,通常我们还是用GBK指代中文Windows内码

 

ASCII码无法将世界上的各种文字和符号全部表示,所以新出一种可以代表所有字符和符号的编码,即:Unicode

Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,

所以,python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),如下代码:

报错:Ascii码无法表示中文

1 2 3 #!/usr/bin/env python    print  "你好,世界"

改正:应该显示的告诉python解释器,用什么编码来执行源代码,即:

1 2 3 4 #!/usr/bin/env python # -*- coding: utf-8 -*-    print  "你好,世界"

 

5、用户输入

 1、注释分两种:

  • 注释当行: # 被注释内容
  • 注释多行:"""被注释的内容""" 

举例:可以利用多行注释,进行多行打印 :(博客园添加不了表格。。。。)

example = """

OS = "linux\windows"     TOOL = "Redis\Mysql" """ print (example)

执行效果:

OS = "linux\windows"     TOOL = "Redis\Mysql"

 

 2、 用户输入:Iput使用

A、 格式化输出的几种形式 

  备注:+  号拼接;%s/%d/%f 赋值 ; format 赋值; 使用{} 按照数字值拼接。举例如下:

 a、+号的拼接:

举例: 

name = input( "name:") income = input( "income:") info = '''---informartion of: ''' + name + """ income:""" + income print (info)

 结果:

name: chen income: 555 ---informartion of: chen income:555

解析:一个个字段的相加打印

 

b、% 赋值(%就相当于shell中的$)

举例: 

name = input( "name:") income = input( " income:") job =  input( "job:") info = """     -------informaion  of  name:%s-----     name:%s     income:%s     job:%s """ % (name,name,income,job) print (info)

结果:

name: chen income: 5000 job: IT -------informaion  of  name:chen----- name:chen income:5000 job:IT

 解析:%s 针对于字符串,如果是是int 类型对应的是 %d,浮点数为 %f。注意数据类型的转换。

判断一个值的类型  type('值') ,会得出这个值的类型 <type 'str'> 。str 字符串、int 数字、dict 字典、 list  列表

举例: 

name = raw_input( "name:")

print type(name)

 返回结果

name: chen

<type 'str'>  得出参数值的类型为字符串

 

 c、 format 赋值

举例: 

name = input( "name:") income = int( input( "income:")) job =   input( "job:") info = ''' -------informaion  of {_name}----- name:{_name} income:{_income} job:{_job} ''' .format ( _name=name, _income=income,  _job=job) print (info)

结果:

name: CQ Chen income: 55000 job: IT -------informaion  of CQ Chen----- name:CQ Chen income:55000 job:IT

 解析:使用format赋值,不用考虑数据类型

 

 d、 使用{} 按照数字顺序拼接

举例: 

name = input( "name:") income = int(input( "income:")) job =  input( "job:") info = ''' -------informaion  of {0}----- name:{1} income:{2} job:{3} ''' % (name,name,income,job) 注意数字对应每个参数值 print (info)

 结果:

name: Chen income: 55000 job: IT -------informaion  of Chen----- name:Chen income:55000 job:IT

 

B、使用getpass模块隐藏密码输入

举例:密码可见 

username = input( "name:") password = input( "passwd:") print (username,password)

 结果:

name: CQ passwd: chen123  (手动输入时,密码可见) CQ chen123

 

 举例:密码不可见 (使用getpass)

1 #/usr/bin/env python
2 #--*-- code:utf-8 --*--

4import getpass
5username = input("name:")
6 
7password = getpass.getpass("请输入密码:")
8
9print (username,password)

结果:

1找到执行所在的目录
2 C:\Users\chen>cd /d D:\python\51CTO\day1
3 执行文件、输入用户名、输入密码、并打印
 D:\python\51CTO\day1>python hello.py 
5 name:chen
6 passwd:
7 ('chen', 'chen')                  

getpass 有待验证,在PyCharm中测试不成功。需要在liunx 系统中、window系统的cmd中验证。

 

C、Input 在 python2与python3 中的区别:

 python2  有input\raw_input;pyhton3 只有Input。py2 中的raw_input功能等同于py3中的input。要避免在PYthon2 中使用Input ,raw_input与及python3中input 输入的永远是字符串。

 Pyhton2 的Input输入的是什么格式,就是什么格式。在PY2中的测试:

举例:错误

name = input( "name:")

print name

返回结果:

name:Abc Traceback (most recent call last): File "D:/python/51CTO/day1/py2input.py", line 2, in <module> name = input("name:") File "<string>", line 1, in <module> NameError: name 'Abc' is not defined

执行结果:Abc 没有被定义

 

 举例:改正 (把Abc给定义一个数值)

Abc = 123456 name = input( "name:") print name

返回结果:

name:Abc  

123456

输入Abc 把 Abc 的值给打印出来。结论:Python2 中要使用Input 要先把输入值给定义好。

 

六、if..else语句 流程判断

 1、判断用户登录验证

 举例 if..else 结构:

#Author:ChenChangQing  name =  "chen" passwd =  "chen123" username = input( "name:") password = input( "passwd:") #同时满足用户、密码相等才能登陆 if username == name  and password == passwd: print ( "Welcome login in !!!! {_name} ".format( _name=name) ) else: print ( "User or passwd Error")

结果:

错误打印

name: chen passwd :123 User or passwd Error

正确打印

name: chen passwd: chen123 Welcome login in !!!! chen 

 

2、猜数字:

举例 if..elif..else 结构:

#定义固定数字

number =  25 #输入数字 guess_number =  int(input( "Please enter your number:")) #if判断 if guess_number == number: print ( "Your guess it!!") elif guess_number < number:   print ( "The number is smaller!!") else:  print ( "The number is bigger!!")

结果:

Please enter your number :26

The number is bigger!

 

七、while 循环

 1、死循环模式

举例: 

1#定义计数器
2count = 0
3while True:
4 print("count:",count)
5  #count += 1 等同于count = count + 1,计算步长
6

count +=1 

 结果无限打印,环境中杜绝死循环。

2、简单的从0打印到5:

举例: 

1 #定义计数器
2 count = 0
3 while count < 6:
4  print("count",count)
5  count +=1 

结果:

1count: 0
.. ..


3、接着猜数字:要求只允许用户猜三次,要求猜对直接退出。三次都都猜不对,打印文字告知用户错误。

举例: 

number =  25  #定义数值 count =  0 #计算步长 while count  < 3 :  #当count小于3执行以下代码 guess_number = int(input(" Please enter your number:")) if guess_number == number: print ("Your guess it!!") break  #猜对数字就直接退出 elif guess_number < number: print ("The number is smaller!!") else: print (" The number is bigger!!") count += 1 else: print (" Get Out!!!")  #三次猜不对直接退出

结果:

Please enter your number: 34 The number is bigger!! Please enter your number: 23 The number is smaller!! Please enter your number: 24 The number is smaller!! Get Out!!!

 

 4、接着上面的列子,猜数字。猜够三次还想继续玩: 

 举例:

#Author:ChenChangQing number = 25  #定义数值 count = 0  #计算步长 while count < 3 :  #当count小于3执行以下代码 guess_number = int(input(" Please enter your number:")) if guess_number == number: print (" Your guess it!!") break  #猜对数字就直接退出 elif guess_number < number: print (" The number is smaller!!") else: print (" The number is bigger!!") count += 1 #三次都猜不对,询问用户是否继续 if count == 3: continue_go_on = int(input( "Do you  want to guessing...?Please enter number:")) if continue_go_on != '10000000000':  #只要输入不等于10000000000继续执行 count = 0 else: print ("Get Out!!!")  #三次猜不对直接退出

 结果:

Please enter your number :4 The number is smaller!! Please enter your number :6 The number is smaller!! Please enter your number :8 The number is smaller!! Do you  want to guessing...?Please enter number :12 Please enter your number :25 Your guess it!!

 

八、for循环

rang(10)  #表示从零开始计算打印10个数:0-9

rang(1,10) # 表示从1开始打印,打印:1-9

rang(1,10,3) #表示从1开始打印,相隔3个数字打印一次,数字小于10

 

举例:

for i in range(5):     print ( "number1" ,i) for i in range(1,5):     print ( "number2 ",i)
for i in range(1,10,3):     print ( "number3" ,i)

输出:

number1 0 number1 1 number1 2 number1 3 number1 4   number2 1 number2 2 number2 3 number2 4   number3 1 number3 4 number3 7

 

循环中break and continue 的用法

备注:break结束本次循环、continue跳出本次循环进入下次循环。

 1、遇到小于6的循环次数就不走了,直接跳入下一次循环

for i in range(10):   if i <= 6:         continue        #不往下走了,直接进入下一次循环 print ( "number1",i)

输出:

 number1 7

number1 8 number1 9

 2、遇到大于4 的循环次数就不走了,直接退出

for i in range(10): if i > 4:         break        #不往下走了,直接跳出整个循环 print (" number2",i)

 输出:

number2 0 number2 1 number2 2 number2 3 number2 4

 

九、作业需求

 作业一:编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

 通过其他的方方式编写

 

 

 

转载于:.html

更多推荐

Python 第1周

本文发布于:2024-02-07 03:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!