Python之路,第二篇:Python入门与基础2

1,复合赋值运算符

               +=   、 -=  、 *=  、 /=  、 //=  、 %=  , **=

             x    +=    y     等同于  x   =   x  +  y

            x    -=    y     等同于  x   =   x  -  y

            。。。。。。

           要求:执行操作时,变量必须存在

>>> x = 3
>>> x += 1
>>> x
4
>>> x -= 1
>>> x
3
>>> x *= 2
>>> x
6
>>> x /= 3
>>> x
2.0
>>> x **= 3
>>> x
8.0
>>> x //= 3
>>> x
2.0
>>> x *= 5
>>> x
10.0
>>> x %= 3
>>> x
1.0
>>> 

 2,关系运算符

          <  、  <=   、 >  、   >= 、 == 、 != ( “<>”仅python2可用)

         说明:关系运算符返回布尔类型的值(True  or False); None 与任何对象相比都为False。

>>> x = 10
>>> x > 8
True
>>> x < 8
False
>>> x >= 8
True
>>> x <= 8
False
>>> x == 8
False
>>> x != 8
True
>>> x=9
>>> x==None
False
>>> 80 > x >60
False

 3,生成对象的函数

             float(obj) 用于字符串或数字转换成浮点数;如果不能出参数,则返回0.0 ;

             int(int [, base = 10])  用于字符串或数字转换成整数, 如果不出参数,则返回0 ;

             bool(x)  用x生成一个bool值(True or False)

             complex(r = 0.0 , i = 0.0) 用数字生成一个复数(实部为r, 虚部为i );

             str(obj=“ ”) 用对象转换成字符串

            

>>> x = "3.1"
>>> type(x)
<class 'str'>
>>> float(x)
3.1
>>> type(float(x))
<class 'float'>
>>> int()
0
>>> x = 0.0
>>> int(x)
0
>>> x = int("1101")
>>> x
1101
>>> x = int("1101",2) #2代表二进制
>>> x
13
>>> x = int("1101",8) #8代表八进制
>>> x
577
>>> x = int("1A", 16)
>>> x
26
>>> help(int) #base=10 十进制
Help on class int in module builtins:

class int(object)
 |  int(x=0) -> integer
 |  int(x, base=10) -> integer
 | 
>>> complex(1,2)
(1+2j)
>>> complex()
0j
>>>
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool(0.0001)
True
>>>
>>> bool(0j)
False

         bool()函数返回假值的情况:

               False 逻辑假值、 None 空值、  0 , 0.0 , 0j 所有零的值 、“ ” 空字符串、 【】空列表、() 空元祖、{ } 空字典 、 set( ) 空集合 ;

    4,预置的数据型函数

                abs(x) 取x的绝对值;

                round(number[, ndigits]) 对数值进行四舍五入, ndigits 是小数向右取整的位数; 负数是向左取整 ;

                pow(x , y ,  z=None )     相当于 x ** y  或   x **  y % z

>>>abs(5)
5
>>>abs(-5)
5
>>>round(123.456)
123
>>>round(1234.567)
1235
>>>int(1234.567)
1234
>>>help(round)
Help on built-in function round in module builtins:
round(...)
    round(number[, ndigits]) -> number
    
    Round a number to a given precision in decimal digits (default 0 digits).
    This returns an int when called with one argument, otherwise the
    same type as the number. ndigits may be negative.
>>>x = 123.456789
>>>round(x,4)
123.4568
>>>round(x,2)
123.46

>>> x = 123.456789
>>> round(x, -1)
120.0
>>> round(x, -2)
100.0
>>> round(x, -3)
0.0
>>>pow(3, 2)    # 相当于3 ** 2

9
>>> pow(3, 2, 4) # 相当于 3 ** 2 % 4
1

 

      帮助函数:

                    help(x)函数

                    作用:查看帮助文档

5, python 语句

              语句由一些表达式组成,通常一条语句可以独立来完成一部分事情并返回结果;

              多条语句写在一行用 " ; " 分号分隔开;

              显示折行:

                      折行符   \ (反斜线)  放在一行的末尾,来示意解释器下一行也是本行的语句;

              隐式折行:

                     所有括号的内容换行,称为隐式折行;

 

6,基本输入输出函数

              (1)基本输入函数    

                       作用:从标准输入设备上读取一个字符串,末尾的换行字符会被删除;

                      格式:  input(“提示字符串”)

                      说明: 返回的字符串(仅python3)

>>> help(input)
Help on built-in function input in module builtins:

input(...)
input([prompt]) -> string
#!/usr/bin/python

pi = 3.1415
text = input("请输入圆的半径:")
r = int(text)   #用字符串生成一个整数
print("周长:", pi * r * 2)
print("面积:", pi * r ** 2 )

>>> ================================ RESTART ================================
>>>
请输入圆的半径:5
周长: 31.415000000000003
面积: 78.53750000000001

  练习:

                     1, 分3次输入小时,分钟,秒; 2 ,此时距离0:0:0 过了多少秒?

#!/usr/bin/python
a = int(input("请输入小时:"))
b = int(input("请输入分钟:"))
c = int(input("请输入秒:"))
m = a * 60 * 60 + b * 60 + c
print(type(m))
print("此时距离0:0:0已经过了 %d"%m )

 

>> ================================ RESTART ================================
>>> 
请输入小时:21
请输入分钟:6
请输入秒:33
<class 'int'>
此时距离0:0:0已经过了 75993

  

         (2)基本输出函数 :print 函数  

                  作用: 将一系列的值以字符串的形式输出到标准输出设备上,默认为终端;

                  print(value, ... , sep=' ', end='\n', file=sys.stdout, flush=False)

                  关键字参数的含义:

                  sep : 两个值之间的分隔符,默认为一个空格;

                  end: 输出完成后,在流末尾自动追加一个字符串,默认为换行符;

                  flush : 是否将流立即进行输出; #系统内核缓存机制

                  file : 流对象,默认为sys.stdout

>>> print(1,2,3,4)
1 2 3 4
>>> print(1,2,3,4, sep="#")
1#2#3#4
>>> print(1,2,3,4, sep="<-|->")
1<-|->2<-|->3<-|->4
####

print("我是第一行字符串")
print("我是第二行字符串")

#结果

我是第一行字符串
我是第二行字符串

######## 

print("我是第一行字符串",end="")
print("我是第二行字符串")

>>> 

我是第一行字符串我是第二行字符串

########

print("我是第一行字符串",end="\n\n\n")
print("我是第二行字符串")

>>> ================================ RESTART ================================
>>>
我是第一行字符串


我是第二行字符串
>>>

 7,条件语句:

            if语句

           作用:让程序根据条件选择性的执行某条语句或某些语句;

           语法: 

                    if   真值表达式1;

                          语句1

                     elif  真值表达式2;

                           语句2

                      elif  真值表达式3;

                           语句3

                       。。。

                       else:

                             语句n

            说明:(1)elif 子句可以有0个、1个或多个

                       (2)else 子句可最多只能有一个,也可以没有

             if 语句嵌套:

                       (1)if语句本身是由多条语句组成一条语句

                       (2)if语句可以作为语句嵌套到另一个语句的内部

 

#!/usr/bin/python

season = input("请输入月份后显示季节: ")
season = int(season)
if season < 4:
    print("此月份为春季~")
elif season < 7:
    print("此月份为夏季~")
elif season < 10:
    print("此月份为秋季~")
elif season <= 12:
    print("此月份为冬季~")
View Code

 

        条件表达式:

               语法: 表达式1   if   真值表达式  else 表达式2

               作用:如果真值表达式的布尔值为True, 表达式1 执行并返回对象 ,否则 表达式2 执行并返回对象。

 1 score = int(input("请输入:"))
 2 score = "及格" if score > 60 else "不及格"
 3 print(score)
 4 >>> 
 5 请输入:70
 6 及格
 7 >>> ================================ RESTART ================================
 8 >>> 
 9 请输入:50
10 不及格
11 >>> 

 

       pass 语句

                    pass语句又名空语句,不做任何事情,语法上用来占位;

                     作用: 保证格式完整,语义完整,避免语法上的错误;

                      语法: pass

                     示例:

 1 if  a > b:
 2     pass
 3 else:
 4      a=b
 5 
 6 ##
 7 def   fun_null():
 8     pass
 9 
10 ##
11 Class MyCass:
12     pass
13 
14 ##
15 while True:
16     pass

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

              

 

posted on 2018-04-22 14:34  微子天明  阅读(228)  评论(0编辑  收藏  举报

导航