查漏补缺(一)

1.4 运算符:

取余 %  :x % y

乘方 * * :2 ** 3

1.4.1 长整数

处理长整数运算:

>>>1987163987163981639186L*198763981726391826L+23
394976626432005567613000143784791693659L

1.8 函数:

四舍五入round

向下取整 floor

1.9 模块:

“from 模块 import 模块“这种形式的import命令后,就可以直接使用函数,而不需要模块名作为点缀

1.9.1 cmath 和复数

虚数由cmath模块处理:

>>>import cmath
>>>cmath.sqrt(-1)
1j

1.10.2 让脚本像普通程序一样运行

只要把下面的内容放在脚本的首行即可:

#!/usr/bin/python

在实际运行脚本之前,必须让脚本文件具有可执行的属性:

$ chmod a+x hello.py

现在就能这样运行了(假设当前目录包含在路径中):

$ hello.py

1.11 字符串

1.11.3 字符串表示,str 和repr

值被转换为字符串的两种机制:

       一种是通过str函数,它会把值转换为合理形式的字符串,以便用户可以理解;

另一种是通过repr函数,它会创建一个字符串,以合法的Python表达式的形式来表示值。

>>>print repr ("Hello,world!")
'Hello,world!'
>>>print repr (10000L)
10000L
>>>print str ("Hello,world!")
Hello,world!
>>>print str (10000L)
10000

1.11.4 input和raw_input的比较

       input会假设用户输入的是合法的Python表达式(或多或少有些与repr函数相反的意思。)如果以字符串作为输入的名字,程序运行没问题的:

What is your name?  "Gumby"
Hello,Gumby!

       然而,要求用户带着引号输入他们的名字有点过分,因此,需要使用到raw_input函数,它会把所有的输入当作原始数据(raw data),然后将其放入字符串:

>>>input("Enter a number")
Enter a number: 3
3
>>>raw_input("Enter a number")
Enter a number: 3
'3'

1.11.5 长字符串、原始字符串和Unicode

     1 .长字符串

print '''This is a very long string.
It continues here.
And it's not over yet
"Hello,world!"
Still here.'''

PS:

   普通字符串也可以跨行。如果一行之中最后一个字符是反斜线,那么,换行符本身就“转义”了,也就是被忽略了,例如:

print "Hello,
world!"

  这句会打印Hello,world!。这个用法也适用于表达式和语句:

>>>1 + 2 + 
           4 + 5
12
>>>print 
         'Hello,world'
Hello,world

2. 原始字符串

原始字符串以r开头,基本可以在原始字符串中放入任何字符,但不能在原始字符串的结尾输入反斜线。

如果最后一个字符(位于结束引号前的那个)是反斜线,应把反斜线单独作为一个字符串来处理。

>>>print r'C:
owhere'
C:
owhere
>>>print r'C:Program Filesfonrdfooarazfrozzozz'
C:Program Filesfonrdfooarazfrozzozz

>>>print r'C:Program Filesfooar' '\'
C:Program Filesfooar
原文地址:https://www.cnblogs.com/HelloDreams/p/4970478.html