Python学习第一天20180702--基本数据类型和运算

1、while循环

while 条件:
  代码块
else:
  代码块
continue 跳出此次循环,循环语句continue以下代码不再执行
break 结束while循环

 1 #!/usr/bin/env python
 2     # -*- coding: utf-8 -*-
 3     # @Time    : 2018/7/2 11:46
 4     # @Author  : chenxiaowei
 5     # @Email   : [email protected]
 6     # @File    : continue和break.py
 7     count=0
 8     while count<10:
 9         count=count+1
10         if count==5:
11             continue #跳出此次循环,以下代码块不在执行
12         if count==8:
13             break   #结束while循环
14         print(count)    

2、基本运算
算术运算符:结果为值
+ - * /
%取余 //取商 **次方

 1 #!/usr/bin/env python
 2     # -*- coding: utf-8 -*-
 3     # @Time    : 2018/7/2 20:23
 4     # @Author  : chenxiaowei
 5     # @Email   : [email protected]
 6     # @File    : 基本运算.py
 7     c = input('请输入运算符号:')
 8     count = 0
 9     while c not in ("+,-,/,*,%,//,**"):
10         print("错误!您输入的为非法运算符。")
11         c = input('请重新输入运算符号:')
12         count += 1
13         if count == 2:
14             print("你输入错误三次,系统即将退出。")
15             exit(1)
16     a = input('请输入第一个值a:')
17     b = input('请输入第二个值b:')
18     if c == '/':
19         count1 = 0
20         while b == 0:
21             print("错误!被除数不能为0,重新输入")
22             b = input('请输入第二个值b:')
23             count += 1
24             if count1 == 2:
25                 print("你输入错误三次,系统即将退出")
26                 exit(2)
27     result = a+c+b
28     print(eval(result))    #将字符串转换为函数

赋值运算符:结果为值
= += -= *= /= %= **= //=

比较运算符: 结果为布尔值
== != <> > < >= <=

逻辑运算符:结果为布尔值
and or not

成员运算符:结果为布尔值
in not in

位运算符:结果为布尔值
& 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0
| 按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。
^ 按位异或运算符:当两对应的二进位相异时,结果为1
~ 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1 。
<< 左移动运算符:运算数的各二进位全部左移若干位,由 << 右边的数字指定了移动的位数,高位丢弃,低位补0。
>> 右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,>> 右边的数字指定了移动的位数。

身份运算符:结果为布尔值
is is not
is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。

3、基本数据类型
Number(数字)
  int
  int(str) #强制转换 默认十进制
  int(str,base=2)
  int(str,base=8)
  int(str,base=16)
  bit.length(num) # Number of bits necessary to represent self in binary(最少需要多少位二进制表示该数)
  float
  bool
  complex 复数
  type()函数用来确定数据类型

String(字符串)
  str.capitalize(self) #首字母大写
  str.count(self,_start,_end)#查找出现的次数
  str.find(self,_start,_end)#查找出现的位置
  str.upper(self)#全部替换为大写
  str.lower(self)#全部替换为小写
  str.casefold(self)#全部替换为小写
  str.center(self,width,fillchar)#居中填充
  str.format(self,args,kwargs)#将占位符替换为制定的值

 1         #!/usr/bin/env python
 2         # -*- coding: utf-8 -*-
 3         # @Time    : 2018/7/2 21:31
 4         # @Author  : chenxiaowei
 5         # @Email   : [email protected]
 6         # @File    : 基本数据类型.py
 7         a1=int('111111')
 8         a2=int('111111',base=2)
 9         a3=int('111111',base=8)
10         a4=int('111111',base=16)
11         print(a1,a2,a3,a4)
12         b1=a1.bit_length()
13         print(b1)
14         c=1+2j
15         print(type(c))
16         d = "abce{DF}bcdef"
17         d1=d.capitalize()
18         d2=d.count('a',0,9)
19         d3=d.find('a',3,9)
20         d4=d.upper()
21         d5=d.lower()
22         d6=d.casefold()
23         d7=d.center(23,'@')
24         d8=d.format(DF='WO')
25         print(d1,d2,d3,d4,d5,d6,d7,d8)

List(列表)
Tuple(元组)
Set(集合)
Dictionary(字典)

 

猜你喜欢

转载自www.cnblogs.com/chenxiaoweiworkinghard/p/9256085.html