python 练习一

来源:互联网 发布:数控车床编程实例 简单 编辑:程序博客网 时间:2024/05/17 13:42

1. 你理解的python是什么?为什么会使用python?

python是一个脚本语言,python 拥有强大的第三方库,开发的周期短,并且可移植较强

2. 解释python第一行怎么写?写的内容是做什么的?怎么写可移植性强?为什么?

#!/usr/bin/python       #!/usr/bin/env python

第一行写的内容都是指定解释器的路径,不同的是,第二种制定方式能够适应不同的安装方式所安装的解释的路径

3. 解释编码格式ASCII,Unicode和utf-8的不同点?

ASCII 是美国制定的编码,只适用于英文字母,不支持其他语言的文字,Unicode能够适用于其它各种语言的文字,utf-8同样能够支持各种文字,而且能根据不同的符号而变化字节长度。

4. raw_input和input的区别

raw_input 是输入字符型 input 是输入整型

5. 三个双引号号(或者三个单引号的)可以用来做什么?

三个双引号可以编辑内容的格式 ,三个单引号可以多行注释

6. python格式化输出(包含变量)的方法有哪些?并举例列出?

[root@news ipython]# pythonPython 2.7.5 (default, Oct 11 2015, 17:47:16) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> A = "word">>> print "hello" ,Ahello word>>> >>> A= 1>>> B= 2>>> print "the numner is",A,Bthe numner is 1 2>>> >>> A=1>>> B=2>>> print "the first num is %d ,the second num is %d" %(A,B)the first num is 1 ,the second num is 2>>> >>> A=1>>> B= "hello">>> print "the num is {} ,the string is {} " .format(A,B)the num is 1 ,the string is hello >>> 

编程练习

    1. 用户登陆v1:
      1). 假设系统中的用户名”root”,密码为”westos”;
      2). 如果用户输入用户名和密码均正确显示”login ok”
      如果用户名错误,显示”user is not exist”
      如果密码错误,显示”password is no ok”
      3). 只有三次登陆机会,超过三次,显示”count is bigger than 3”
#!/usr/bin/env python#coding:utf-8"""file:.pydate:2017/8/25 0:06author:    peakdescription:"""for i in range(1,4):    user=raw_input("login name:")    password=raw_input("password:")    if user =="root" and password == "westos" :        print "login ok"        exit()    elif user =="root":        print "password is not ok"    else:        print "user is not exit"print "count is bigger than 3"

测试一

测试二

  • 编写乘法表
#!/usr/bin/env python#coding:utf-8"""file:.pydate:2017/8/25 0:06author:    peakdescription:"""for i in range(1,10):    for n in range(1,i+1):        print "{}*{}={}" .format(i,n,i*n)    print " "

这里写图片描述

这里写图片描述