zoukankan      html  css  js  c++  java
  • python16_day01【介绍、基本语法、流程控制】

    一、day01

    1.二进制运算

      60 & 13  =12
    
      60 |  13  =61
    
      60 ^ 13  =49
    
      60<<2   =240
    
      60>>2   =15

    2.逻辑运算符

      and   or  not

    3.关系运算符

      in  not in

    4.验证运算符

      is  is not

    5.字符编码

     1    ASCII只用了8bit,即1byte只能表示255个字符,对于欧美足够用.但是其它非英语国家的语言文字不能包括,比如中国就制定gb2312,各国都有自己的标准。
     2 
     3   因此unicode出现,可以包括所有国家编码文字,通常2bytes,特殊时需要4bytes.
     4 
     5   最后utf-8本着节约精神,结合了ascii 和unicode,属于可变长编码,也是是我们大家常用的。
     6 
     7   ascii->unicode->utf-8
     8 
     9   注意:在计算机内存中,统一使用unicode,当需要保存到硬盘或者传输时,就转换成utf-8.      
    10 
    11        当用记事本编辑的时候,从文件读取的 UTF-8 字符被转换为 Unicode 字符到内存里,编辑完 成后,保存的时候再把 Unicode 转换为 UTF-8 保存到文件.
            ascii-->GB2312-->GB18030-->GBK-->unicode-->UTF8可变长

      Python的ascii转换执令:

        >>> ord('A') 
    
        65
    
        >>> chr(65) 
    
        'A'

      python2.7中可以使用

        >>> u'abc'.encode('utf-8')    #unicode --> utf-8
    
        'abc'
    
        >>> 'abc'.decode('utf-8')      #utf-8 --> unicode
    
        u'abc'

      python3.x中则统一使用unicode,加不加u都一样,只有以字节形式表示字符串则必须前面加b, 即b"abc"

        >>>a='tom'  
    
        >>>a.encode('utf-8')
    
        b'tom'
    
        >>> a='中国'
    
        >>> a
    
        '中国'
    
        >>> a.encode('utf-8')
    
        b'xe4xb8xadxe5x9bxbd'

    6.list列表(python的精随)

      nameList=['tom','apple','cat','tom']
    
      dir(nameList)
    
      nameList.append('tom')
    
      nameList.index('tom')
    
      nameList.count('tom')
    
      nameList.remove('tom')
    
      nameList.sort()
    
      nameList.reverse()
    
      nameList.pop()
    
      nameList.insert(2,'tom')
    
      nameList.clear()
    
      namelist[:]
    
      nameList.extend(otherList)
    
      if 'tom' in nameList:
    
        print('ok')
    
      for i in range(nameList.count('tom')):
    
          nameList.remove('tom')

     7.交互

    1 # -*- coding:utf8 -*-
    2 import getpass
    3 name=input("What is your name?")
    4 pwd=getpass.getpass("What is your passwd?")

     8.for loop

     1 for i in range(10):
     2     print("loop:", i )
     3 
     4 for i in range(10):
     5     if i<5:
     6         continue #不往下走了,直接进入下一次loop
     7     print("loop:", i )
     8 
     9 for i in range(10):
    10     if i>5:
    11         break #不往下走了,直接跳出整个loop
    12     print("loop:", i )

     9.while loop

    1 count = 0
    2 while True:
    3     print("",count)
    4     count +=1
    1 count = 0
    2 while True:
    3     print("你是风儿我是沙,缠缠绵绵到天涯...",count)
    4     count +=1
    5     if count == 100:
    6         print("去你妈的风和沙,你们这些脱了裤子是人,穿上裤子是鬼的臭男人..")
    7         break

     10.猜年龄

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3  
     4  
     5 my_age = 28
     6  
     7 count = 0
     8 while count < 3:
     9     user_input = int(input("input your guess num:"))
    10  
    11     if user_input == my_age:
    12         print("Congratulations, you got it !")
    13         break
    14     elif user_input < my_age:
    15         print("Oops,think bigger!")
    16     else:
    17         print("think smaller!")
    18     count += 1 #每次loop 计数器+1
    19 else:
    20     print("猜这么多次都不对,你个笨蛋.")

    11.三元运算

    a=10
    result = 10 if a=10 else 20
    #如果a=10,则result=10,否则result=20
  • 相关阅读:
    浏览器返回错误汇总分析
    黄金投资品种众多 个人如何投资黄金
    .NET设计模式系列文章 (转自TerryLee's Tech Space)
    .NET设计模式(7):创建型模式专题总结(Creational Pattern)
    一个很经典的下拉式菜单(附效果)
    .NET设计模式(16):模版方法(Template Method)
    搜索引擎优化基础(转并整理添加)
    .NET设计模式(18):迭代器模式(Iterator Pattern)
    【ASP.NET】网页中嵌入视频的三种方法
    .NET设计模式(15):结构型模式专题总结
  • 原文地址:https://www.cnblogs.com/weibiao/p/5095036.html
Copyright © 2011-2022 走看看