print函数:
print函数输出字符串型
age = '123'
print 'my age is %s' %age;
# my age is 18
输出整型和长整型
age = 18
print 'my age is %d' %age
#my age is 18
输出浮点型
age = 19.9
print '%.2f' %age
#19.90
这里的%…起到了站位的作用。
输出多位:
name = 'tang'
age = 18
print 'my name is %s my age is %d' %(name,age)
#my name is tang my age is 18
也可以这样写
age = 18
print 'my age is',age
#my age is 18//如果想在字符串的末尾打印一个变量就可以在末尾键入一个元素
#如果是其他数据类型,使用%s的方式进行格式化
#那么其实,Python是首先将这个数据转换为字符串
#再进行格式化
age = 18
print 'my age is %s' %str(age)
python2和python3中的不同
# py2
print 'hello'#相当于是语句
#py3
print ('hello')#相当于函数
###算术运算符
- ’ + ’ 号运算符
可用于字符串的拼接。
a = hello
b = world
c = a + b
print c
#hello world
- ’ * ’ 乘号运算符
c = 'hello'
a = c*3
print c*2
print a
#hello
#hellohelloehello
- '/'号运算符
a = 5
b = 2
c=a/b
print c
# 2 z如果出号两边都是整型则结果也是整型
改变浮点数
a = 5.0
b = 2
c = a/b
print c
#2.5
如果是传过来的变量
a = 5
b = 2
print float(a)/b
# 2.5
py3中如果两边都是整型则也会出现浮点型
整除 :’//’
a = 5;
b = 2;
c = a // d
print c
# 2
取余数:’%’
幂运算
a = 2
b = a**3
print b
# 8
+=
/=
*=
…
//=
else if 语句:
- 'bool’数据类型
a = True
b = False
只有这两种类型 - 比较运算符
- ‘a==b’
- ‘a!=b’
- ‘a>b’
- 条件a and b:只有条件a和b同时满足的条件下才能满足条件。
- 条件a or b:只有条件 a 或则 b 满足的条件下才能满足条件。
if True:
print 'hello'
else:
print 'world'
a = 1
b = 1
if a==b:
print 'equal'
else:
print 'not equal'
age = raw_input("您输入年龄:")
age = int(age)
if age >= 15 and age<= 24:
print '你是一个青年人'
else:
print '你不是一个亲年人'
not关键字起到了取反的作用
elif :else/if
循环:
while:在特定的条件下重复的执行某段代码
小案例:打印99乘法表
i = 1
j = 1
while i<=9:
j = 1
while j<=i:
print('%d*%d=%d'%(i,j,i*j),end=' ')#py3的输出语句在字符串的末尾会默认加入'\n'这里我们end=' ',意思就是末尾以空格结尾
j+=1
print()#换行
i+=1
break和continue
只能在两个循环中使用不能再其他语言中使用