一 大纲

2 运算符

3 基本数据类型

  整型:int

  字符串:str

  列表:list

  元组:tuple

  字典:dic

4 for enumrate xrange range

 

1.1. 列表中的十六进制或者unicode展示位中文

attr=(['CN', '0', '\xe6\xb5\x99\xe6\xb1\x9f', '\xe6\x9d\xad\xe5\xb7\x9e', 'ALIBABA'], ['CN', '0', 'qita', '0', 'cn'], ['\xe4\xb8\xad\xe5\x9b\xbd', '0', '\xe9\xa6\x99\xe6\xb8\xaf', '*', '\xe5\x85\xb6\xe4\xbb\x96'], ['CN', '*', '\xe5\x8c\x97\xe4\xba\xac', '\xe5\x8c\x97\xe4\xba\xac', '\xe9\x98\xbf\xe9\x87\x8c\xe4\xba\x91/\xe7\x94\xb5\xe4\xbf\xa1/\xe8\x81\x94\xe9\x80\x9a/\xe7\xa7\xbb\xe5\x8a\xa8/\xe9\x93\x81\xe9\x80\x9a/\xe6\x95\x99\xe8\x82\xb2\xe7\xbd\x91'])

        for i in attrs:
                s = str([j.decode("utf-8") for j in i]).replace('u\'','\'')
                print s.decode("unicode-escape")

  

有这样一个列表:

list = [{'channel_id': -3, 'name': u'\u7ea2\u5fc3\u5146\u8d6b'}, {u'seq_id': 0, u'name_en': u'Personal Radio', u'channel_id': 0, u'abbr_en': u'My', u'name': u'\u79c1\u4eba\u5146\u8d6b'}]



其中name值是中文,如何讲其显示为中文?

s = str(self.channel_list).replace('u\'','\'')
print s.decode("unicode-escape")


成功显示:

[{'channel_id': -3, 'name': '红心兆赫'}, {'seq_id': 0, 'name_en': 'Personal Radio', 'channel_id': 0, 'abbr_en': 'My', 'name': '私人兆赫'}, ]


但此时类型为unicode

>>> type(s)
<type 'unicode'>

  

 上节内容回顾
  1、编程语言
  2、python、C#、java
  3、python:pypy,cpython,jpython..
  4、执行方式
解释器
文件
  5、指定解释器
  python xxx.py
  ./xxx.py   #!/usr/bin/env python
  6、ascii   unicode   utf-8
  7、
    2.7    # -*- coding:utf-8 -*-
    3.x    默认utf-8
  8、变量,代指
    变量名 = 值
    变量名要求:
    a.数字字母下划线
    b.数字不能开头
    c.不能和py关键字重复
    a = "alex"
    b = a
  9、条件
    if 条件,elif 条件,else
  10、while
    while 条件,
    从上到下执行一次
    (判断条件是否真)从上到下执行一次
    (判断条件是否真)从上到下执行一次
    (判断条件是否真)从上到下执行一次
    (判断条件是否真)从上到下执行一次 

 

 

 

 

 二  前天作业讲解:

 知识点:

  判断奇偶:除以2取模

1 %
2 3%2=1
3 2%2=0
4 4%2=0

  详细见 url http://www.cnblogs.com/liujianzuo888/articles/5440915.html 

 三 编码转换

 

unicode 可以编译成 UTF-U GBK

 

python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'Administrator'

a='刘建佐'        #默认是utf-8
a_unicod=a.decode('utf-8')  # decode是解码成unicode 括号是脚本内容的默认编码  即:将脚本内容的utf-8解码成unicode
a_gbk=a_unicod.encode('gbk') #encode是编码,将unicode的编码内容编码成指定的,这里是gbk
print(a_gbk)  #用于终端打印
#print(u"刘建佐")  #3里面是字符串  2里面是unicode 


# 3版本直接将utf-8编码成GBK 不需要先转成unicode了,因为3没有了

E:\py_test\s2_py>python3 test.py
Traceback (most recent call last):
File "test.py", line 6, in <module>
a_unicod=a.decode('utf-8') # decode是解码成unicode 括号是脚本内容的默认编码 即:将脚本内容的utf-8解码成unicode
AttributeError: 'str' object has no attribute 'decode'


python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

 

 

四 pycharm 安装 配置

   file-setings-editor- file && file encode template  输入 

  

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'liujianzuo'

 

五 pycharm 快捷键

   ctrl+/ 批量注释

  shift+方向键 选中 

  shift+tab 向左tab

六 运算符

算数运算:

python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

比较运算:

python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

赋值运算:

python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

逻辑运算:

python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

成员运算:

 python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

身份运算: 面向对象讲解

python 基础2  编码转换 pycharm 配置   运算符 基本数据类型int str list tupple dict   for循环  enumerate序列方法  range和xrange 列表中的十六进制或者unicode展示位中文

 

运算符

名称

说明

例子

+

两个对象相加

3 + 5得到8。'a' + 'b'得到'ab'。

-

得到负数或是一个数减去另一个数

-5.2得到一个负数。50 - 24得到26。

*

两个数相乘或是返回一个被重复若干次的字符串

2 * 3得到6。'la' * 3得到'lalala'。

**

返回x的y次幂

3 ** 4得到81(即3 * 3 * 3 * 3)

/

x除以y

4/3得到1(整数的除法得到整数结果)。4.0/3或4/3.0得到1.3333333333333333

//

取整除

返回商的整数部分

4 // 3.0得到1.0

%

取模

返回除法的余数

8%3得到2。-25.5%2.25得到1.5

<<

左移

把一个数的比特向左移一定数目(每个数在内存中都表示为比特或二进制数字,即0和1)

2 << 2得到8。——2按比特表示为10

>>

右移

把一个数的比特向右移一定数目

11 >> 1得到5。——11按比特表示为1011,向右移动1比特后得到101,即十进制的5。

&

按位与

数的按位与

5 & 3得到1。

|

按位或

数的按位或

5 | 3得到7。

^

按位异或

数的按位异或

5 ^ 3得到6

~

按位翻转

x的按位翻转是-(x+1)

~5得到-6。

<

小于

返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。

5 < 3返回0(即False)而3 < 5返回1(即True)。比较可以被任意连接:3 < 5 < 7返回True。

>

大于

返回x是否大于y

5 > 3返回True。如果两个操作数都是数字,它们首先被转换为一个共同的类型。否则,它总是返回False。

<=

小于等于

返回x是否小于等于y

x = 3; y = 6; x <= y返回True。

>=

大于等于

返回x是否大于等于y

x = 4; y = 3; x >= y返回True。

==

等于

比较对象是否相等

x = 2; y = 2; x == y返回True。x = 'str'; y = 'stR'; x == y返回False。x = 'str'; y = 'str'; x == y返回True。

!=

不等于

比较两个对象是否不相等

x = 2; y = 3; x != y返回True。

not

布尔“非”

如果x为True,返回False。如果x为False,它返回True。

x = True; not x返回False。

and

布尔“与”

如果x为False,x and y返回False,否则它返回y的计算值。

x = False; y = True; x and y,由于x是False,返回False。在这里,Python不会计算y,因为它知道这个表达式的值肯定是False(因为x是False)。这个现象称为短路计算。

or

布尔“或”

如果x是True,它返回True,否则它返回y的计算值。

x = True; y = False; x or y返回True。短路计算在这里也适用。

练习

 1 >>> 1 + 1
 2 2
 3 >>> 1 + 1 * 2
 4 3
 5 >>> (1 + 1 ) * 2       
 6 4
 7 >>> 0/32
 8 0
 9 >>> 32/0
10 Traceback (most recent call last):
11   File "<stdin>", line 1, in <module>
12 ZeroDivisionError: integer division or modulo by zero
13 >>> 3*3
14 9
15 >>> 2**8
16 256
17 >>> 2**32
18 4294967296
19 >>> 2**64
20 18446744073709551616L
21 >>> 2**32/1024/1024
22 4096
23 >>> print "以上是32位系统的寻址空间"
24 以上是32位系统的寻址空间
25 >>> 2**64/1024/1024
26 17592186044416L
27 >>> print “虽然上面是64位系统的寻址空间,但是不一定就支持”
28   File "<stdin>", line 1
29     print “虽然上面是64位系统的寻址空间,但是不一定就支持”
30           ^
31 SyntaxError: invalid syntax
32 >>> 2**42 /1024/1024
33 4194304
34 >>> 10%2
35 0
36 >>> 10%3
37 1
38 >>> print "作用是看一个数能不能被整成,过滤奇偶数"
39 作用是看一个数能不能被整成,过滤奇偶数
View Code

 

相关文章:

  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2021-09-12
  • 2021-06-23
猜你喜欢
  • 2021-07-06
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
相关资源
相似解决方案