Python 运维开发 学习记录 语言基础(一)

Shell/Python   数据类型   交互控制  

一、python编程风格

  • 缩进统一
  • 变量
    • 标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘ _ ’)
    • 标识符名称的其他部分可以由字母(大写或小写)、下划线(‘ _ ’)或数字(0-9)组成。
    • 标识符名称是对大小写敏感的。例如,myname和myName不是一个标识符。注意前者中的小写n和后者中的大写N。
    • 有效 标识符名称的例子有i__my_namename_23a1b2_c3
    • 无效 标识符名称的例子有2things、this is spaced out和my-name。

二、python数据类型

  • 整数型
age = 10  
type(age)  
<type 'int'>  
  • 字符串
>>> name = "leo"
>>> type(name)
<type 'str'>  
  • 浮点型
>>> data = 9.99
>>> type(data)
<type 'float'>  
  • 布尔型
>>> success = False
>>> type(success)
<type 'bool'>  
  • 字符串转换为整数型
>>> age = "10"
>>> type(age)
<type 'str'>  
>>> int(age)
10  
>>> type(age)
<type 'str'>  
>>> type(int(age))
<type 'int'>  
  • 整数转字符串
>>> age = "test"
>>> age = 10
>>> type(age)
<type 'int'>  
>>> age = str(age)
>>> type(age)
<type 'str'>  

三、Python算术运算符

  • 取模 - 返回除法的余数
>>> 10 % 2 
0  
>>> 10 % 3
1  
>>> 10 % 4 
2  
>>> 10 % 5 
0  
  • 加减乘除
>>> a = 21 
>>> b = 10
>>> c = 0
>>> 
>>> c = a + b 
>>> print c 
31  
>>> c = a -b 
>>> c
11  
>>> c = a * b 
>>> c
210  
>>> c = a / b
>>> c 
2  
>>> c = a % b 
>>> c 
1  
>>> a = 3 
>>> b = 2
>>> c = a ** b 
>>> c
9  

四、注释

  • # 单行
  • ''' 多行
  • 小练习
#!/usr/bin/python

name = leo  
age = 25.120000  
print '''Welcome to learn Python

I am %s

I am %.1f years old.     #用%s %d 进行测试

wish you have a nice day here.  
''' % (name,age)  

六、用户交互、流程控制

  • 猜数字小游戏 while版
__author__ = 'leoic'

import random  
#随机数
real_num = random.randrange(10)  
real_count = 0

#while True:
while real_count < 3:  
    guess_num = raw_input("Please guess the real num:").strip()
    if len(guess_num) == 0:
        continue
    if guess_num.isdigit():
        guess_num = int(guess_num)
    else:
        print "Your need input a integer of string"
        continue

    if guess_num > real_num:
        print "Wrong!,you need try smaller!"
    elif guess_num < real_num:
        print "Wrong!,you need try bigger!"
    else:
        print "Congratulations!you got is!"
        break
    real_count += 1
else:  
    print "The real num is %s" % real_num
  • for range 版
import random  
real_num = random.randrange(10)

for real_count in range(3):  
    guess_num = raw_input("Please guess the real num:").strip()
    if len(guess_num) == 0:
        continue
    if guess_num.isdigit():
        guess_num = int(guess_num)
    else:
        print "Your need input a integer of string"
        continue

    if guess_num > real_num:
        print "Wrong!,you need try smaller!"
    elif guess_num < real_num:
        print "Wrong!,you need try bigger!"
    else:
        print "Congratulations!you got is!"
        break
else:  
    print "The real num is %s" % real_num

七、字典 多级菜单

menu = {  
    "Beijing" :{
        "Chaoyang":{
            "CBD":["CCIC","CCTV"],
            "WangJing":["Momo","ChuiZi"]
        },
         "HaiDian": ["Baidu","YouKu"]
    },
    "Shanghai":{
        "PuDong":["Ctrip","1 shop"],
        "PuXi":["China Bank","America Bank"]
    }
}

exit_flag = False

while not exit_flag:  
    for index,key in enumerate(menu.keys()):
        print index,key
    choise_1 = raw_input("please choise your like city: ")
    if choise_1.isdigit():
        choise_1 = int(choise_1)
        key_1 = menu.keys()[choise_1]
        print key_1
        while not exit_flag:
            for index,key in enumerate(menu[key_1]):
                print '--->',index,key
            choise_2 = raw_input("Please choise your like area: ")
            if choise_2.isdigit():
                choise_2 = int(choise_2)
                key_2 =  menu[key_1].keys()[choise_2]
                print key_2
                while not exit_flag:
                    for index,key in enumerate(menu[key_1][key_2]):
                        print '-->-->',index,key