设为首页 加入收藏

TOP

python16_day01【介绍、基本语法、流程控制】
2017-10-10 12:07:43 】 浏览:10431
Tags: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'\xe4\xb8\xad\xe5\x9b\xbd'

 

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

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在Linux上Ruby的安装 下一篇在Mac OS X上安装 Ruby运行环境

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目