温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python 基础

发布时间:2020-07-10 23:54:28 来源:网络 阅读:332 作者:飞天喜欢yu 栏目:编程语言

Python的解释器

  • cpython官方标准。动态的,边执行,边解释。
  • ipython
  • jpython,他是一种完整的语言,他是一个Python语言在Java中的完全实现。
  • PYPY ,他是用Python写的解释器,速度比cpychon快。
  • ironpython
    注意:在一般编程过程中,大写代表常量,而小写则代表变量
    x = 2
    y = 3
    z = x 
    x =5
    结果:x = 5,z = 2

    Python中的数据类型

    网址:http://www.yiibai.com/python/python_basic_operators.html
    python 基础

    字符编码(默认是ascall存)

    Python中使用的是Unicode字符,在Python中ord()可以查看ascall值。
    在存储字符的过程中,默认是使用ascall进行存储字符,如果这样写就是使用Unicode进行存储。

    
    >>> a = u'lala'
    >>> type(a)
    <type 'unicode'>

name =”飞天”
print name
飞天
name
'\xe9\xa3\x9e\xe5\xa4\xa9’
#这是utf-8(ascall是utf-8的一个子集),他灵活存储汉字和字母,当你是汉字的时候,使用3个字节,如果是英文就用一个字节。\xe9\xa3\x9e这就是飞字。存储在内存的过程是使用Unicode,因为不能同时使用两种字符集。在硬盘中存储是使用硬盘中,就是使用utf-8,因为能节省空间Unicode不管是汉字还是字母都是使用2个字节。

在Unicode转化成utf-8中间需要一个转换过程,转换过程就如下

name = u“飞天”
name.encode(‘utf-8’)  #转化成utf-8
#解开
b =name.encode(‘utf-8’)  #这是一个将他转化成字符串类型的
name.decode(‘utf-8’) #重新转化成Unicode

支持中文,在代码首页加入:* coding:utf-8 *#有四种方式,详见博客。

导入模块

有好几种方式:

  • import sys
    这样调用的话,就是要加上你模块的名称例如:sys.argv.
  • from sys import argv
    这样就直接 argv进行调用
  • import multiprocessing as multi
    将模块取一个别名,用来代替繁琐的模块名称
  • form sys import * #不建议使用
    把这个模块所有的方法都导进来,也是直接使用

    流程控制

  • if … elif …else #永远只有一个结果
  • for i in range(1,100).
  • while …else

    练习

    #猜年龄
    name = raw_input("Please input  your name:")
    info = 'This is var will be print out'
    age = input("Age:")
    job = raw_input("Job:")
    real_age = 29
    for i in range(1,10):
    if age > 29:
        print "think smaller!"
    elif age == 29:
        print '\033[32;1mGOOD! 10RMB!\033[0m'
    else:
        print "think bigger"
    print"you still get %s shots" %(10-i)
    age = input("Age:")
    lala =  """
    Name:%s
    Age:%s
    Job:%s
    """%(name,age,job)
    print "-----------------"
    #输出你想打印的数值
    count = 0
    print_num = input("which loop do you want to be print out ?")
    while count < 10000:
    if  count == print_num:
        choice = raw_input("Do you want to continue the Loop?(y/n)")
        if choice == "n":
            break
        else :
    #这里使用一个while循环也可以。
    #while print_num <= count
    #   print_num = input("which loop do you want to be print out ?")
    #   print print "%d已经过了,sx" %(count)
       print_num = input("which loop do you want to be print out ?")
           if print_num < count:
               print "%d已经过了,sx" %( print_num)
               print_num = count
    
    else :
        count +=1
        print 'Loop',count
    else:
    print "Loop",count

    作业

    编写登录接口

  • 输入密码和用户名
  • 认证成功后显示欢迎信息
  • 认错三次后锁定
    #代码
    import  sys
    retry_limit = 3
    retry_count = 0
    account_file = "accounts.txt"
    lock_file = "account_lock.txt"
    while retry_count < retry_limit:  #只要不超过3次及不断循环
            username = raw_input("\033[32;1mUsername:\033[0m")
            lock_check = file(lock_file,)
            #当用户输入用户名后,打开LOCK文件以检查此用户已经LOCK
            for line in lock_check.readlines():
                    line = line.split()
                    if username == line[0]:
                            sys.exit('033[31!1mUsername %s os locked!\033[0m' %username)
            password = raw_input('\033[32;1mPassword:\033[0m')
            f = file (account_file,'rb')
            match_flag =False
            for line in f.readlines():
                    user,passwd = line.strip('\n').split()
                    #去掉每行多余的,并把这一行按空格分成两列,分别赋值user,passwd两个变量
                    if username == user and passwd == password:
                            print "Match!",username
                            match_flag = True
                            break
            f.close()
            if match_flag == False:
                    print  "usrname"
                    retry_count +=1
            else :
                    print "welcome log the system!"
    else:
            print "your account is locked!"
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI