Python快速入门

Python是一门解释型语言.

Python的特点:
1.语法简单,表达能力强.
2.解释运行:Python解释器一行一行的读取源代码,每读取一行就执行一行.实现过程:Python按行读取源代码,然后将源代码转为”字节码”供Python解释器执行,此时就会生成一个.pyc文件.所以当已经存在了.pyc文件后,如果删掉了.py文件同样可以执行程序.
3.跨平台:Python是基于其解释器来进行执行,只要平台支持Python解释器就可以运行Python的源代码.
4.可扩展性强:可以用C/C++来调整某个模块.
5.可嵌入:C/C++代码中也可以嵌入Python代码.
缺点:
执行效率差.


基础知识:
python可以充当计算器:
这里写图片描述
这里写图片描述


变量和赋值
Python中的变量不需要声明,直接使用即可,会在初始化的时候决定变量的类型.
如下图所示:
这里写图片描述
注意:在Python中不支持++操作.可以用+=.


Python的变量命名规则
同C/C++一样,可以用字母,数字和下划线命名,但是数字不可以开头.
Python中的数字的取值范围没有限制!!!没有限制!!!

>>> a = 1000*1000*1000*1000
>>> print a
1000000000000

>>> b = 1.12321324354365462342
>>> print b
1.12321324354     //在Python中不区分float和double,这是在Linux2.7.5的版本上实现的,可以有11位小数

>>> type(a)
<type 'int'>    //type()是一个查看变量类型的内建函数

>>> type(b)
<type 'float'>

复数:由实部和虚部组成
例如:

>>> p = 2 + 3j
>>> print p
(2+3j)

字符串:可以用单引号(”),也可以用双引号(“”),如果字符串中包含单引号或双引号,也可以用三引号(”””)
例如:

>>> str = 'hello'
>>> print str
hello
>>> str = "hello"
>>> print str
hello
>>> str = 'hello "Alice" '
>>> print str
hello "Alice" 
(这里的同一个变量名可以重复使用,是由于其)

还有一些不可见字符,比如转义字符:
例如:

>>> str = "you are beautiful \n yes"
>>> print str
you are beautiful 
 yes

求字符串长度:
Python中有一个内建函数可以求字符串的长度:
比如:

>>> str = "hello"
>>> print len(str)
5

在Python中没有字符的概念,只有字符串的概念
例如:

>>> print len(str[0])
1

字符串的索引
例如:

>>> str = "You are very good"
>>> print str
You are very good
>>> str[0]
'Y'
>>> str[3]
' '
>>> str[19]    //访问越界
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> str[-1]    //就相当于字符串的长度减1
'd'
>>> str[-3]
'o'

切片操作符获取子串

>>> str = "You are very good"
>>> str[1:3]
'ou'
>>> str[1:-1]
'ou are very goo'
>>> str[1:]
'ou are very good'
>>> str[:]
'You are very good'
>>> str[:9]
'You are v'

字符串连接+与字符串重复*
例如:

>>> a = '123'
>>> b = '321'
>>> print a+b
123321

>>> a = a*4
>>> print a
123123123123

>>> print a+b
123123123123321

布尔类型
Python中的True和False第一个字符必须大写.
例如:

>>> t = True
>>> print t
True
>>> print type(t)
<type 'bool'>

输入输出
输出函数:print——>将结果打印到标准输出
输入函数:raw_input—–>从标准输入输入 注意:返回值是一个字符串
例如:

>>> str = raw_input("input:")
input:qwe
>>> print str
qwe

因为raw_input返回值是一个字符串,所以如果要输入一个整数到标准输入时就要用int函数将字符串转换为数字
例如:

>>> num = raw_input("input:")
input:1231
>>> print type(num)
<type 'str'>
>>> print int(num)
1231

同时输入两个数并求和:

>>> a,b = raw_input('input:'),raw_input('input:')
input:23
input:34
>>> print int(a) + int(b)
57

注释
Python中的注释,使用#来注释的.#是单行注释
例如:

# coding=utf-8   //如果是汉语注释,就要加上这个说明
str = "you are great" #str是一个字符串 
print str

因为Python的源代码默认只支持ASCLL.


操作符
Python中同样支持+-*/%运算符,用法也相同.
在Python中:/就是表示常见的除法:两个数都是整数,那么结果也是整数,但是如果有一个是浮点数,那么结果也是浮点数.
//:在Python中表示”地板除”,即向下取整
例如:

# coding=utf-8
a = 10
b = 3
print a/b

c = 2.0
print a/c

d = 3.0
print a/d

e = 6.0
print a/e
print a//e  #地板除

执行结果:
这里写图片描述
精确除法: from _future_ import division

>>> from __future__ import division
>>> a = 3
>>> b = 2
>>> print a/b
1.5

乘方运算**
乘方运算符的符号是:**
例如:

>>> a = 10
>>> b = 3
>>> print a**b
1000

>>> a = 100
>>> c = a**10
>>> print c
100000000000000000000   //数据无上限

Python中的比较运算符
支持这些>,<,>=,<=,==,!=比较运算符,并且返回值是一个布尔值
例如:

>>> a = 2
>>> b = 9
>>> print a < b
True
>>> print a > b
False
>>> print a == b
False
>>> print a != b
True
>>> print a >= b
False
>>> print a <= b
True

在Python中,逻辑运算符用 and,or,not

>>> a = 2
>>> b = 3
>>> c = 4
>>> a < b and a < c
True
>>> a > b or a < c
True
>>> a > b or a > c
False
>>> not a < b
False

而字符串也可以使用这些运算符
比如:

>>> str1 = 'aa'
>>> str2 = 'bb'
>>> str3 = 'cc'
>>> str1 + str2
'aabb'
>>> str1 + str2 + str3
'aabbcc'
>>> str1 != str2
True
>>> str1 == str2
False
>>> not str1 == str2
True
>>> str1 < str2
True
>>> str1 < str2 and str1 > str3
False

列表-元组-字典
元组和列表相当于C语言中的数组,但是在同一个元组和列表可以保存任意类型的元素.
[]表示列表
()表示元组

>>> l1 = [1,2,3,4]
>>> l1
[1, 2, 3, 4] #列表
>>> print l1
[1, 2, 3, 4] 
>>> a = (1,2,3,4)
>>> a
(1, 2, 3, 4)   #元组

>>> list = [1,1.23,'hiahia',True]
>>> list
[1, 1.23, 'hiahia', True]

>>> tuple = (2,'da',32.2,False)
>>> tuple
(2, 'da', 32.2, False)

区别:列表中的元素可以改变,而元组中的之不可以改变.
例如:

>>> list = [1,1.23,'hiahia',True]
>>> list
[1, 1.23, 'hiahia', True]
>>> tuple = (2,'da',32.2,False)
>>> tuple
(2, 'da', 32.2, False)
>>> list[0] = 100
>>> list
[100, 1.23, 'hiahia', True]
>>> tuple[0] = 200    #执行时报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

字典:用来存放键值对
符号:{}
例如:

>>> a = {'like':'watermelon','unlike':'ginger'}
>>> a
{'like': 'watermelon', 'unlike': 'ginger'}
>>> a['like']
'watermelon'

if–else语句

用法:
if 条件:
    fun(1)
    nextfun()
else:
    fun(2)

例如:

n = 10
if n > 0:
    print True
else:
    print False

if—elif—-else
elif就相当于else if

用法:
if 条件:
    fun()
elif 条件:
    fun1()
else
    fun()

pass语句
有时候会用到空语句,但是Python中没有{},所以就有了一个专门的占位语句:pass
例如:

if 条件:
    pass
else 条件:
    fun()

不支持switch/case语句.
while循环语句

用法:
while 条件:
    fun()
例如:
n = 0
while n < 10:
    print 'ret : %d' % n
    n += 1

结果:这里写图片描述


for循环语句
例如:

  1 str = 'beautiful'                                         
  2 for a in str:
  3     print a

结果:
这里写图片描述

列表的遍历:

a = [1,2,3,4]
for i in a:
    print i    #如果输出时不换行就加一个逗号(,)

结果:
这里写图片描述

字典的遍历:

a = {'like':'watermellon', 'unlike':'garlic'}
for key in a:
    print key,a[key]

结果:
这里写图片描述

用for循环生成一个列表:
例如:

s = [i**2 for i in range(10)]
print s

执行结果:
[0, 1, 4, 9, 16]

例如:


break语句与continue语句
break与continue与C/C++中的使用方法相同.
例如:

for i in range(0,100):
    if(i % 3 == 0):
        print i
    continue;

range():是一个内建函数,有3个参数.前两个参数表示一个左闭右开的区间,第三个参数表示步长.可以生成一个由数字组成的列表.
例如:

for i in range(0,100,2):
    print i

结果就是输出0到100全部的偶数.

还可以将它们放在一行之中:

s = [i for i in range(0,100) if i % 3 == 0]
print s

运行结果:
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]


函数:将一些可以重复使用的代码,封装成为一个函数.
定义函数:使用def;
返回函数:return;
例如:

def Add(x,y):
    return x +y

ret = Add(1,2)
print ret

在Python中,没有函数重载的概念,如果写了同名的函数,后面的函数就会覆盖前面的函数.
例如:

def fun():
    print 'first'

def fun():
    print 'second'

fun()

结果:
这里写图片描述

在Python中,可以同时返回多个参数并且同时可以接收多个参数:
例如:

  3 def Get():
  4     return 100,200
  5 
  6 x,y = Get();
  7 print x,y   

结果:
100 200

并且可以选择性的接收参数:
例如:

  3 def Get():
  4     return 100,200
  5 
  6 _,y = Get();
  7 print y  

结果:
200

  3 def Get():
  4     return 100,200
  5 
  6 x,_ = Get();
  7 print x   

结果:
100

注意:_是占位符
敲黑板!!!
在Python中,函数也是对象,也有类型:
比如:
这里写图片描述

~~~~
看完这些基础的东西就可以写写简单的代码 了

猜你喜欢

转载自blog.csdn.net/yinghuhu333333/article/details/80816420
今日推荐