python笔记01:基础知识

1.4 数字和表达式

# -*- coding:utf-8 -*-
#1.4

#除法
print 1 / 2
print 1.0 / 2
print 10 / 3
print 10.0 / 3.0
print int(1.0/2)
print float(1/2)
#如果使用“//”,那么就算是浮点数,双斜线也会执行整除
print 1 // 2
print 1.0 // 2.0

#取余
print 10 % 3
print 2.75 % 0.5
print int(2.75 % 0.5)

#乘方运算
print 2 ** 3
print -3 ** 2
print (-3) ** 2
#幂运算符比取反的优先级要高

#长整数
print 1000000000000000000
print 11234567890123456L * 123456789345673456L + 100

#十六进制与八进制数
print 0xAF
print 010

输出如下:

0
0.5
3
3.33333333333
0
0.0
0
0.0
1
0.25
0
8
-9
9
1000000000000000000
1386983681400638600588387302184036
175
8

除法运算:单斜线与双斜线

就算是浮点数,双斜线也会整除

1.5 变量

1.6 语句

语句也即是指令
有两类语句:print语句与赋值语句

1.7 获取用户输入

input()函数与raw_input()函数

input()函数:

#
x = input("x= ")
y = input("y= ")
print x * y

# 输出如下:
x= 12
y= 23
276

raw_input()函数:

x1 = raw_input("x1= ")
y1 = raw_input("y1= ")
print x1 * y1
输出会报错:
  File "ex1-7.py", line 8
    y2 = int(raw_input("y2= ")
     ^
SyntaxError: invalid syntax
更改后:
x2 = int(raw_input("x2= ")
y2 = int(raw_input("y2= ")
print x2 * y2

x3 = int(raw_input("x3= ")
y3 = raw_input("y3= ")
print x3 * y3

输出如下:

x2= 100
y2= 23
2300
x3= 10
y3= 123
123123123123123123123123123123

raw_input()函数对于所有的输入都当作字符串来处理

1.8 函数

1.幂运算:pow(a,b)
2.绝对值:abs(x)
3.四舍五入:round(x)
5.取整函数:floor(x),这个函数不能直接使用,需要调用math模块才能使用
6.除法,整除及余数,divmod
print pow(2,10)
print abs(-10)
print 1/2
print round(1.0/2.0)

# 结果如下:
1024
10
0
1.0

>>> divmod(5,2)
(2, 1)

1.9 模块

可以把模块想象成导入到python以增强其功能的扩展
import math
print math.floor(32.9)
print int(math.floor(32.9))

x = math.ceil(12.1001)
print x
print int(x)
# 输出如下:
32.0
32
13.0
13
1.floor函数与ceil函数是相对的,floor函数是取整,不论后面有什么都给舍去,而ceil函数是只要后面有值就进一个输出
2.在确定自己不会导入多个同名函数的情况下,你可能希望不要再每次调用函数的时候都写上模块的名字,那么就可以使用import命令的另一种形式,如下:
from math import floor
from math import ceil
from math import sqrt

x = floor(23.89)
y = ceil(12.001)
z = sqrt(100.0)

print "x is %s, y is %s, z is %s." % (x,y,z)

# 输出如下:
x is 23.0, y is 13.0, z is 10.0.
sqrt函数用于计算一个数的平方根

1.9 cmath与复数

sqrt函数用于计算一个数的平方根,但是当给他一个复数作为参数时就会报错,如下:
>>> from math import sqrt
>>> sqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
这时就需要用到cmath函数了:
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> (1+3j)*(9+4j)
(-3+31j)

1.10 保存并执行

1.将命令语句保存为文件
2.通过命令提示符运行脚本:绝对路径与相对路径
3.让脚本向普通程序一样运行
只要在脚本行首添加python程序的绝对路径即可,如下:
#!/usr/bin/python
 
在实际运行脚本之前,必须让脚本具有可执行的属性:
chomod a+x hello.py
 
现在就能直接运行了

1.11 字符串

字符串是值,就像数字一样。

单引号与双引号:

在输出字符串时,使用单引号和双引号没有什么区别,但是在句子中有单引号或双引号时,就要使用另一种符号了。符号较多的时候还要用反斜线(\)进行转义
#!/usr/bin/python2.6
print "Hello,World!"
print "This's a test."
print "let\'s go!"
print 'let\'s go!'
print "\"Hello,World!\",she said."
输出如下:
Hello,World!
This's a test.
let's go!
let's go!
"Hello,World!",she said.

字符串拼接:

>>> "let's say " 'Hello,world'
"let's say Hello,world"
>>> x = "Hello, "
>>> y = "World!"
>>> x y
  File "<stdin>", line 1
    x y
      ^
SyntaxError: invalid syntax
>>> x + y
'Hello, World!'
>>> x,y
('Hello, ', 'World!')
如何正确的拼接字符串?就像进行加法运算一样
字符串表示:str与repr
>>> print repr("Hello,world!")
'Hello,world!'
>>> print str("Hello,world!")
Hello,world!
>>> print str(100000L)
100000
>>> temp = 42
>>> print "The temperature is :",temp
The temperature is : 42
>>> print "The temperature is :" + temp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is :" + `temp`
The temperature is :42

str函数可以把值转换为合理性似的字符串,以便用户理解

repr函数可以创建一个字符串,以合法的python表达式的形式来表示值
repr(x)也可以写作`x`实现(注意是反引号)。如果希望打印一个办函数值的句子,就可以用到反引号了
注:在python3中已经不再使用反引号了
简而言之:str,repr和反引号是将python的值转化为字符串的3种方法。函数str让字符串更易读,而repr则把结果字符串转换为合法的python表达式。

input与raw_input的对比?

input会假设用户输入的是合法的python表达式。除非对input有特别的必要,否则应该尽可能的使用raw_input()函数。

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

1.对于长字符串的输出,需要使用3个双(单)引号,换行输出
2.原始字符串:
原始字符串对于反斜线并不会特殊对待。在某些场景下比较有用。
>>> print "C:\\Users\\鹏飞\\Desktop\\新建文件夹"
C:\Users\鹏飞\Desktop\新建文件夹
>>> print "C:\Users\鹏飞\Desktop\新建文件夹"
C:\Users\鹏飞\Desktop\新建文件夹
>>> print "C:\nUsers\鹏飞\Desktop\新建文件夹"
C:
Users\鹏飞\Desktop\新建文件夹
>>> print "C:\\nUsers\鹏飞\Desktop\新建文件夹"
C:\nUsers\鹏飞\Desktop\新建文件夹

对于一些特殊的符号,我们在输出时需要用到反斜线进行转义,但是当特殊符号比较多的时候就非常麻烦了,这时候原始字符串就派上用场了。在原始字符串中输入的每个字符串都会与书写的方式保持一致:

>>> print r'C:\nowwhere'
C:\nowwhere
>>> print "C:\nowwhere\zbc\xyz\001"
ValueError: invalid \x escape
>>> print r"C:\nowwhere\zbc\xyz\001"
C:\nowwhere\zbc\xyz\001
当然,我们也要像平常一样对引号进行转移,但是最后输出的字符串包含了转移用的反斜杠:
>>> print r'let's go'
  File "<stdin>", line 1
    print r'let's go'
                ^
SyntaxError: invalid syntax
>>> print 'let\'s go'
let's go
>>> print r'let\'s go'
let\'s go

但是,不能再原始字符串结尾输入反斜杠,除非你对反斜杠进行了转义:

>>> print r"This is illegal\"
  File "<stdin>", line 1
    print r"This is illegal\"
                            ^
SyntaxError: EOL while scanning string literal
>>> print r"This is illegal \\"
This is illegal \\
>>> print "This is illegal \\"
This is illegal \

Unicode字符串:

>>> u"hello,world"
u'hello,world'
>>> u"hello,world 测试"
u'hello,world \u6d4b\u8bd5'
>>> r"hello,world 测试"
'hello,world \xe6\xb5\x8b\xe8\xaf\x95'

1.12 小结

本章主要内容:
1.python安装与启动IDE
2.算法:
3.表达式
4.变量
5.语句
6.函数
7.模块
8.程序
9.字符串
本章涉及的新函数:
abs(num):返回数字的绝对值
camth.sqrt(num):返回平方根,也可以用于负数
float(object):将字符串和数字转换为浮点数
help():提供交互式帮助
input(prompt):获取用户的输入
raw_input(prompt):获取用户的输入
int(object):将字符串和数字转换为整数
long(object):将字符串和数字转换为长整型数
math.ceil(num):返回数的上入整数,返回值的类型为浮点数
math.floor(num):返回数的下舍整数,返回值为浮点数
math.sqrt(num):返回平方根,不适用于负数
pow(x,y[, z]):返回x的y次幂(所得结果对z取模)
repr(object):返回值的字符串表示形式
round(num[, ndigits]):根据给定的精度对数字进行四舍五入
str(object):将值转化为字符串
 
练习:
#!/usr/bin/python
# -*- coding:utf-8 -*-
#
import math
import cmath
x1 = int(raw_input("Please input the num of x1 :"))
y1 = input("Please input the num of y1 :")
z1 = raw_input("Please input some words: ")

print "x1 * y1 =",x1 * y1
print "x1 * z1 =",x1 * z1

print "ceil(12.001) = ",math.ceil(12.001)
print "floor(32.998) = ",math.floor(32.998)
print "sqrt(16) = ",math.sqrt(16)

print "cmath.sqrt(-15) =",cmath.sqrt(-15)
print "round(10.0/3.0) =",round(10.0/3.0)
print "abs(-101) =",abs(-101)

# 输出如下:
Please input the num of x1 :6
Please input the num of y1 :10
Please input some words: xyz 
x1 * y1 = 60
x1 * z1 = xyz xyz xyz xyz xyz xyz 
ceil(12.001) =  13.0
floor(32.998) =  32.0
sqrt(16) =  4.0
cmath.sqrt(-15) = 3.87298334621j
round(10.0/3.0) = 3.0
abs(-101) = 101

 

 

posted @ 2017-09-25 19:44  flyme123  阅读(253)  评论(0编辑  收藏  举报