• Day_1_Python_循环和格式化


    一、解释器
    若需要提高python的运行速度,可以使用不同的解释器,比如Cpython、pypy、Jpython等可以在不同的语言下进执行
     
    二、python环境
    #!/usr/bin/env python (在你的环境下找python环境,一般都设置为这种模式)
    后装的python一般都是在 /usr/local/bin/python
    所以你要设置 #!/usr/bin/python 如果你安装了其他版本的python则会找不到你的环境
     
     
    三、书写规范
    一边用下划线的方式或驼峰式
    常量的话 一搬 都是 大写
     
    四、常识
    name1 = ["Alex Li"]  nam2=["Alex Li"] 
    name11 = "Alex" name22="Alex" 
    分别看这这两次的内存id,用list的时候是不一样的,用str的时候id是一样的,说明list、dict是可变变量,str、tuple是不可变变量
     
    五、字符编码
    早期 二进制表示数字,用ASCII 2的8次方255个字符1bytest(占8位)代表字母、用GB2312(支持7千中文)表示中文、升级GBK兼容GB2312(2万多中文),升级GB180(2万7千多汉字)。
    中期 Unicode(固定占2个字节bytes)代表全球统一的不同编码
    后期 可变长的字符集 UTF-8 (存英文1个字节bytes、存中文用3个字节bytes)
     
    六、用户输入
    在python3中已经没有 raw_input 所以,在比如使用的是int类型的input的时候需要人工转一下 
    int_input = int(input("Plz input your number "))
    input和raw_input
    raw_input可以接受任何格式的输入 只在python2.7里有
    input会强制转换成str
     
    七、格式化输出
    使用format格式化进行输出
    user_input = input("plz input name : ")
    username = user_input
    info2 = """ info of {Username}""".format(Username=username)
    print (info2)
     
    或 使用{}来占位 
    user_input = input("plz input name : ")
    age_input = input("Plz input age : ")
    username = user_input
    age =age_input
    info2 = """ info of {},age is {}""".format(age,age)
    print (info2)
     
    通过 list 下标来占位
    p=['kzc',18]
    print ('{0[0]},{0[1]}'.format(p))
     
     
    格式化小练习
     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 __author__ = 'zouyi'
     4 
     5 """" input """
     6 
     7 # user_input = input("plz input name : ")
     8 # age_input = input("Plz input age : ")
     9 # username = user_input
    10 # age =age_input
    11 # info2 = """ info of {username},age is {age}""".format(username,age)
    12 # print (info2)
    13 
    14 
    15 """" class """
    16 # class Person:
    17 #     def __init__(self,name,age):
    18 #         self.name,self.age = name,age
    19 #         def __str__(self):
    20 #             print  ("THIS name is {},age is {}".format(self.name,self.age))
    21 
    22 """" listput """
    23 
    24 p=['kzc',18]
    25 print ('{0[0]},{0[1]}'.format(p))
     
     
     
    八、getpass库(pycharm下不好使)
    import getpass
    password = getpass.getpass("plz inpt your pass : ")
    

      

    九、小练习(while循环猜数字项目)

    这里主要是随机Radom产生一个数字,每次来采取这个数字,当持续到3次的时候询问是否继续

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 __author__ = 'zouyi'
     4 import random,time
     5 
     6 num = random.randint(1,99)
     7 flag = True
     8 count = 0
     9 
    10 while flag:
    11     starttime = time.clock()
    12     count = count +1
    13     if count ==3:
    14         contniue_confirm = input("Do you want continue y/n ")
    15         if contniue_confirm =="y":
    16             print ("ok CONTNIUE GAME..Try to guess...")
    17             count=0
    18         else:
    19             print ("Exit..")
    20             break
    21     print ("your guess count : %s"%(count))
    22     try:
    23         guess_input = int(input("plz input your guess (1-100): "))
    24     except Exception as e:
    25         print('hehe..have wrong...')
    26     else:
    27         if guess_input == num:
    28             print ("you got it..
    ")
    29             endtime = time.clock()
    30             flag = False
    31             print ("""spend time {} sec""".format(int(endtime-starttime)))
    32         elif guess_input>num:
    33             print ("think smaller
    ")
    34             continue
    35         elif guess_input<num:
    36             print("think bigger
    ")
    37             continue
    38         elif guess_input ==str(guess_input):
    39             print("Need input type int ..")
    40         else:
    41             "Timeout...."
  • 相关阅读:
    redis quick start
    Distributed processing
    DocFetcher 本机文件搜索工具
    ZeroTier One
    windows下搭建voip服务器
    在公司上wifi
    屏幕录制软件
    openresty vs golang vs nodejs
    DISC测试
    How to implement a windbg plugin
  • 原文地址:https://www.cnblogs.com/zoee/p/5700155.html
Copyright © 2020-2023  润新知