|
前言:
Python, 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。
Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议。
Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。
Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。
1、打印字符串
>>> print 'hello world!'
hello world!
2、四则运算
>>> print 2+3*40/5
26
3、字符串拼接
>>> print 'hello '+'world'
hello world
>>> print 'hello '*3
hello hello hello
4、变量
>>> x = 'hello world'
>>> print x
hello world
5、获取用户输入
#代码
x = raw_input('please input your name: ')
print 'hello '+x
#输出
please input your name: world
hello world
raw_input()函数,输入的值会做字符串处理
input()函数,输入的值为原始type
6、数据类型
int:整型
str:字符串
float:浮点型
bool:布尔型
可以通过type()查看类型
>>> a = 5
>>> x = 'a'
>>> type(x)
<type 'str'>
>>> type(a)
<type 'int'>
7、转义字符‘\’
>>> print 'I\'am the world'
I'am the world
>>> print "I'am the world"
I'am the world
>>> print 'I'am the world'
File "<stdin>", line 1
print 'I'am the world'
^
SyntaxError: invalid syntax
8、字符串格式化输出
%s:字符串
%d:整型
%f:浮点型
%x:十六进制
%%:表示%本身
>>> x = 'world'
>>> age = 20
>>> print 'hello %s your name is %d'%(x,age)
hello world your name is 20
9、布尔值
True 表示‘真’
False 表示‘假’
>>> not 0
True
>>> 2>3
False
10、逻辑运算
and:与
or:或
not:非
注:‘and’的优先级高于‘or’
>>> True and False
False
>>> True and True
True
>>> True or False
True
>>> not True
False
>>> True and True or False
True
11、条件运算符
1. if语句
>>> if 2>3:
... print 'condition is True'
... else:
... print 'condition is False'
...
condition is False
1.1 多条件判断
>>> a = 3
>>> b = 3
>>> if a>b:
... print 'a>b'
... elif a<b:
... print 'a<b'
... else:
... print 'a=b'
...
a=b
1.2 if else嵌套
>>> if name=='world':
... if age>15:
... print 'you are %d years old'%age
... else:
... print 'no message'
...
you are 20 years old
2、while循环
>>> i = 0
>>> while i<5:
... print i
... i += 1 # i += 1 等于 i = i + 1
...
0
1
2
3
4
3、for循环
专门针对list,dict等结构
>>> l = ['a','b','c']
>>> for i in l:
... print i
...
a
b
c
13、break和continue
break和continue都是针对for循环和while循环的
break:跳出循环
continue:跳过本次循环
14、判断值得真假,可以使用not
>>> not 1
False
>>> not 0
True
15、list和dict
list:列表,一种有序集合,可以随时添加删除其中的元素
dict:字典,使用k/v格式存储,可以使用‘in’判断key是否存在
练习:
1、需求:让用户一直输入数字(只输入数字),如果输入为0,终止程序。打印所有输入数字的平均值
count = 0
times = 0
while True:
num = input('please input number:')
if num==0:
break
times += 1
count += num
avg = (count+0.0)/times
print avg
2、银行存款10000,年利率3.25%,多少年之后,存款能到翻一番
money = 10000
year = 0
while money<20000:
money *= 1.0325
year += 1
print '%s years'%year
3、遍历一个序列['C','js','python','css','js','html','node'],统计这个序列中’js‘出现的次数
l = ['C','js','python','css','js','html','node']
times = 0
for i in l:
if i=='js':
times += 1
print times
4、求[1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45]这个序列的最大值
l = [1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45]
max = 0
for i in l:
if i>max:
max = i
print 'The max number is %s'%max
5、用户输入数字,判断是不是闰年(闰年判断规则:1、如果是100的倍数,要被400整除 2、被4整除),如果输入的不是闰年,提示信息,并继续输入
#!/usr/bin/python
# -*- coding=utf-8 -*-
while True:
year = input('please input year: ')
if year%400==0 or (year%100!=0 and year%4==0):
print '是闰年'
break
else:
print '不是闰年'
6 ['C','js','python','js','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node'],求这个list中,每个元素出现的次数
l = ['C','js','python','js','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node']
d = {}
for i in l:
if i in d:
d += 1
else:
d = 1
print d |
|