python的基本数据类型

1.数值类型:int1),float1.02e82e-8),tra字符串("hello"),long(1111L),bool布尔类型(True,False),complex复数类型(1+2j)

这里写图片描述
这里写图片描述

2.运算符和表达式
(1)算术运算符:+,-,*,**(次方), /, %, //(得int)  

In [1]: 5/2
Out[1]: 2

In [2]: from __future__ import division

In [3]: 5/2
Out[3]: 2.5

(2)赋值运算符(同属之类型之间):=, +=(a+=b即a=a+b), -=, /=, *=, %= 
(3)关系运算符: >, >=, <, <=, !=, ==(比较是否相等)   ##最终返回结果为bool值
(4)逻辑运算符:逻辑与and, 逻辑或or, 逻辑非not

这里写图片描述
这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

内置方法
cmp    ##比较大小,<为-1,>为1,=为0
In [1]: cmp(1,3)
Out[1]: -1

In [2]: cmp(5,3)
Out[2]: 1

In [3]: cmp(5,5)
Out[3]: 0
寻找帮助:help(cmp)
str(),type(),int(),float(),long(),complex(),bool()(不是0都是true)   ##转换
In [4]: int = 2
In [5]: float(int)
Out[5]: 2.0

divmod()    ##得商和余
In [10]: divmod(5,2)
Out[10]: (2, 1)

abs()    ##取绝对值
In [11]: abs(1.2)
Out[11]: 1.2

pow()    ##**次方
In [12]: pow(2,3)
Out[12]: 8

round()    ##四舍五入
In [13]: round(1.5)
Out[13]: 2.0

这里写图片描述这里写图片描述这里写图片描述这里写图片描述

端口输入输出
1.input接收数值类型数据

year = input("输入判断年份:")
print type(year)
print ( (year%4==0 and year%100!=0) or year%400==0 )

2.raw_input接受字符串类型

username = raw_input("用户名:")
password = raw_input("密码:")
print "username:%s   password:%s" %(username,password)

#print "用户名是:",username ##另一种打印方法
#print "密码是:",password

3.print1print "hello"   ##print hello (报错,此时hello为变量名,不带""就为变量)2print '''
    1:%s
    2:%s
    '''%(1,2)     ##'''有三种功能,%s,%d,%f,%.5d.%.2f,%e3print "1:%s 2:%s" %(1,2)

这里写图片描述

这里写图片描述

这里写图片描述

if
1.if语句的格式:
if (表达式):
    满足表达式执行的语句
else:
    不满足表达式执行的语句
例1:
age = input("age:")
if age > 18:
   print "成年"
else:
   print "未成年"2:
year = input("输入年份:")
if (year%4==0 and year%100!=0) or year%400==0:
    print "闰年"
else:
    print "不是闰年"

2.if包含多重条件表达式
(1)warn = "有人"
   if warn:
      print "有人来了!"
   else:
      print "一切顺利!"    ##warn为空时,输出一切顺利!

(2)warn = "1"
   disk_uasge = 78
   if warn or disk_uasge >80:
      print "服务器需要维护。。。"
   else:
      print "一切顺利!"
例:
#!/usr/bin/env python
#coding:utf-8
'''
录入信息
'''
print ("============服务信息录入=============")
hostname = raw_input("主机名:")
IP = raw_input("IP:")
used_year =input("使用年限:")
CPU = raw_input("CPU:")
Memory = raw_input("Memory:")
manager_name = raw_input("管理员名字:")
if used_year > 10:
   print "该服务器使用年限太久!"
else:
   print '''
       主机名:%s
       IP:%s
       使用年限:%s
       CPU:%s
       Memory:%s
'''%(hostname,IP,used_year,CPU,Memory)

这里写图片描述

这里写图片描述

这里写图片描述这里写图片描述

while
1.whlie循环的语法:
while 表达式:
      满足表达式执行的语句
else:
      不满足表达式的语句    ##其他语言所没有的
无限循环:(不需要else)
while True:
      print "hello"

例:
#!/usr/bin/python
#coding:utf-8

'''
1.用户名和密码系统给定
2.用户登录时,输入用户名和密码,判断是否登陆成功
3.用户登录有三次机会,超过三次还未成功,则报错
'''

print ("=======用户登录======")
trycount = 0
while trycount < 3:
      hostname = raw_input("用户名称:")
      password = raw_input("用户密码:")
      if hostname == "westos" and password == "redhat":
         print "登陆成功!"
         exit()
      else:
         if trycount < 2:
            print "登录失败!"
         trycount += 1
else:
      print "超过三次,不可再登陆!"

2.循环语句中的关键字
continue     ##跳出本次循环,继续回到循环语句执行下一次循环
break        ##跳出循环,不再进行循环
例:
#!/usr/bin/env python
#coding:utf-8

"""
1.cmd = 显示命令行提示符,等待用户输入
2.如果命令为空,跳出本次循环,继续接受用户命令
3.如果命令为quit,跳出所有循环,结束程序
4.如果有命令,那么打印"run %s"%(pwd)
"""
while True:
  cmd = raw_input("[root@server~]:")
  if not cmd:
     continue
  elif cmd == "quit":
     break
  else:
     print ("run %s")%(cmd)

这里写图片描述这里写图片描述

这里写图片描述这里写图片描述


for

for i in 可迭代对象:
    state1....
break     ##跳出for循环
continue  ##跳出本次循环
else:
    state2....
第一个可迭代对象:range(start,stop,step)
例:
#!/usr/bin/env python
#coding:utf-8
'''
编写九九乘法表:
1*1=1
1*2=2 2*2=4
...
1*9=9 ...       9*9=81
'''
for i in range(1,10):
 for j in range(1,1+i):
    print "%d*%d=%d"%(j,i,i*j),    ##最后的,表示不换行
 print     ##默认换行,执行完一次换行

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值