python入门(二)

1.使用math模块

>>> import math
>>> math.pi
3.141592653589793

查看math模块所提供的工具

>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

因此math模块可以提供正弦sin()...

另外help命令可以为我们提供每个函数的使用方法

>>> help(math.pow)

Help on built-in function pow in module math:
pow(...)
    pow(x, y)
    
    Return x**y (x to the power of y).
(END)

解释第一行表示这是math模块的内建函数,第三行表示该函数有两个参数,也表示函数的调用方式,第四行表示函数的返回值以及x**y的含义

也就是说math.pow(x,y)与x**y含义等效

>>> math.pow(4,2)
16.0
>>> 4**2
16

2.hello world

[gugu@localhost python_source]$ vim hello.py
[gugu@localhost python_source]$ python hello.py 
hello world

3.算数19+2*48/2

[gugu@localhost python_source]$ vim math1.py
[gugu@localhost python_source]$ python math1.py 
65.0

文件内容如下

#! /usr/bin/env python
#code:utf-8
# 计算19+2*46/2
a = 19+2*46/2;
print(a);

第一行是必须要写的,它能够引导程序找到python的解释器,第二行指定编码,第三行单行注释

4.字符串,在python中但隐含和双引号引起来的都是字符串,但是如果字符串中含有’或者”的时候使用,单引号包括字符串中的双引号,或正在使用双引号包含字符传中的单引号。或者使用转义字符反斜杠(\)

  1 #! /usr/bin/env python
  2 #code:utf-8
  3 
  4 a="What's your name"
  5 b='What"s your name?'
  6 c='What\'s your name?'
  7 
  8 print("a:"+a);
  9 print("b:"+b);
 10 print("c:"+c);

奇特现象:

>>> dos = "c:\news"
>>> dos
'c:\news'
>>> print(dos)
c:
ews

第一种采用转义字符,第二种使用下面的方式

>>> dos = r"c:\news"
>>> dos
'c:\\news'
>>> print(dos)
c:\news

其中字符串前面的r表示引号里面的是原始字符串,引号里面的内容不要转义了。

5.内建函数

abs(),divmod(),imput(),open(),staticmethod(),all(),enumeerate(),int(),ord(),str(),any(),eval(),isinstance(),pow(),sum(),basestring(),execfile(),isubclass(),print(),super(),bin(),file(),iter(),property(),tuple(),bool(),filter(),len(),range(),type(),bytearry(),float(),list(),raw_input(),unichr(),callable(),format(),locals(),redue(),unicode(),chr(),frozenset(),long(),reload(),vars(),classmethod(),getattr(),map(),globals(),max(),reversed(),zip(),compile(),hasattr(),memoryvierw(),round(),import(),complex(),hash(),min(),set(),apply(),delattr(),help(),next(),setattr(),buffer(),dict(),hex(),object(),slice(),coerce(),dir(),id(),oct(),sorted(),intern().

6.简单的python输入输出小程序

  1 #! /usr/bin/env python
  2 #coding=utf-8
  3 
  4 name = input("What is your name?")
  5 age = input("How old are you?")
  6 
  7 print("Your name is :" + name)
  8 print("Your are" + age + " years old.")
  9 after_ten = int(age) + 10
 10 print("You will be " + str(after_ten) + " years old after ten years")

[gugu@localhost python_source]$ python in_out.py 
What is your name?gugu
How old are you?16
Your name is :gugu
Your are16 years old.
You will be 26 years old after ten years

7.索引

>>> str1 = 'study python'
>>> str1
'study python'
>>> str1[1]
't'
>>> str1[1:6]
'tudy '

8. in判断字符串是否存在

>>> 'a' in 'a a a a'
True
>>> 'a1' in 'a a a a'
False

9. cmp(str1, str2)用于比较【python3中没有】,ord()返回字符的ASCII码,chr()根据ASCII码获取相应的字符

>>> cmpare('aa','ab')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'cmpare' is not defined
>>> import operator
>>> operator.eq('aa','ab')
False

10,字符串乘法

>>> print('abc' * 3)
abcabcabc
>>> print(3 * 'abc')
abcabcabc

11.常用方法

  1. split()
  2. S.strip()去掉左右空格,S.lstrip()去掉左空格,S.rstrip()去掉右空格
  3. S.supper()转大写。S.lower()转小写
  4. join连接字符串

12.格式化字符串,%s 和{}格式化输出,推荐第二种

>>> print('hello %s ' % 'gugu')
hello gugu 
>>> "Hello {0}".format('gugu')
'Hello gugu'
>>> "Hello {name}".format(name = 'gugu')
'Hello gugu'





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值