python基础教程学习笔记 第一章 基础知识

内建函数:abs 绝对值,pow幂函数,round 四舍五入为最接近的整数
int long float 类型对象

math模块下的floor 函数向下取整 ceil 将给定的数值转换为大于或者等于它的最小整数

导入模块:
import math
from math import sqrt 可以每次调用不写上模块名字(保证不导入多个同名函数的前提下)
也可以使用foo=math.sqrt进行赋值,--》foo(4) ---2

复数:
sqrt(-1)--->math domian error 
不能求负数的平方根---》负数的平方根是虚数
sqrt 只能处理浮点数,而虚数(以及复数,即实数和虚数之和)
cmath(complex math 复数)


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

(1+3j)*(9+4j)--》-3+31j


python语言本身就提供了对复数的支持

交互式解释器是python的强项之一:
通过idle --file--new file--输入脚本--ctrl+F5 执行


name=raw_input("what is your name?")
print 'hello ,'+name+'!'


通过命令行提示符运行python脚本:
切换到要执行的python文件目录
python he.py (设置环境变量的前提下)

如果没有设置环境变量:c:\python27\python he.py

字符串拼接:

x="hello,"
y="world"
x+y
'hello,world'


字符串表示:

print 1000L
1000
>>> print 'hello,world'
hello,world

print值被转成字符串

str函数 --》将值转换为合理形式的字符串,以便用户可以理解。
repr函数--》会创建一个字符串,他以合法的python表达式的形式来表示值

>>> print repr("hello, world")
'hello, world'
>>> print repr(1000L)
1000L
>>> print str('hello ,world')
hello ,world
>>> print str(1000L)
1000

repr(x)的功能也可以用`x`实现

>>> print 'the temperature is '+`temp`
the temperature is 42



注:str和int,一样是类型对象,而repr仅仅是函数。

input 和raw_input比较:

name=input('what is your name')
print 'hello,'+name+"!"


执行--

======================= RESTART: E:/pythonlearn/he.py =======================
what is your namefa

Traceback (most recent call last):
  File "E:/pythonlearn/he.py", line 6, in <module>
  name=input('what is your name')
  File "<string>", line 1, in <module>
NameError: name 'fa' is not defined

input 会假设用户输入的是合法的python表达式
以字符串作为输入的名字,程序就没有问题

======================= RESTART: E:/pythonlearn/he.py =======================
what is your name"aa"
hello,aa!

raw_input 函数会把所有的输入当成原始数据(raw data)

>>> input('enter a number:')
enter a number:3
3
>>> raw_input('enter a number:')
enter a number:3
'3'

除非对inputyou特别的需要,否则应该尽可能使用raw_input函数

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

1.长字符串 三个引号代替普通的引号,可以在字符串之中同时使用单引号和双引号,不需要使用反斜线进行转义

>>> print '''this is a very long string.
it continues here.
and it's not over yet.
"hello,world!"
still here.'''
this is a very long string.
it continues here.
and it's not over yet.
"hello,world!"
still here.

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

>>> print 'hello\
world'
helloworld

2.原始字符串
普通字符串中,反斜线具有转义的作用 \n换行

>>> print 'helo,\nworld'
helo,
world
原始字符串不会把反斜线当做特殊字符

>>> print r'c:\nowher'
c:\nowher
原始字符串最后一个字符不能是反斜线,除非你对反斜线进行转义

>>> print r'this is illegal\'
SyntaxError: EOL while scanning string literal

>>> print r'this is legal\\'
this is legal\\

>>> print r'c:\program files\foo\bar' '\\'
c:\program files\foo\bar\

3.unicode字符串
字符串常量的最后一种类型就是Unicode字符串或者称为Unicode对象
python普通字符串在内部是以8为的ascii码形成存储的,而Unicode字符串则是存储为16位Unicode字符。

>>> u'hello world'
u'hello world'

python 3.中,所有字符串都是Unicode字符串

算法是描述如何完成一项任务的方法。

表达式是计算机程序的组成部分,他用于表示值

标量是一个名字,他表示某个值

语句是告诉计算机做某些事情的指令。

python中的函数就像数学中的函数:可以带参数,并且返回值

模块是扩展,他可以导入到python中,从而扩展python的功能

程序 字符串

新函数
abs(number) 返回数字的绝对值
cmath.sqrt(number) 返回平方根,也可以应用于负数
float(object) 将字符串和数据转换为浮点数
help() 提供交互式帮助
input(prompt) 获取用户输入
int(object) 将字符串和数组转换成整数
long(object) 将字符串和数据转换为长整型数
math.ceil(number) 返回数的上入整数,返回值的类型为浮点数 大于等于
math.floor(number) 返回数的下入整数,返回值的类型为浮点数 小于
math.sqrt(number) 返回平方根,不适用与负数
pow(x,y[,z]) 返回x的y次幂(所得结果去z取模)

>>> pow(2,3,5) 2**3%5
3

raw_input(prompt) 获取用户输入,返回的类型为字符串
repr(object) 返回值的字符串表示形式
round(number[,ndigits]) 根据给定的精度对数字进行四舍五入
str(object) 将值转换为字符串





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值