Python选择结构与循环结构基础知识点总结

Python选择结构与循环结构基础知识点总结

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time  : 2020/4/23 9:29
# @Author: xuhui
# @File  : Structure.py

# 运算符     说明
# ==        两端相等,返回True;否则,返回False
# !=        两端不相等,返回True;否则,返回False
# >         左侧大于右侧,返回True;否则,返回False
# >=        左侧大于或相等右侧,返回True;否则,返回False
# <         左侧小于右侧,返回True;否则,返回False
# <=        左侧小于或相等右侧,返回True;否则,返回False
# is        两个对象是同一个对象(比较地址),返回True;否则,返回False
# not is    两个对象不是同一个对象(比较地址),返回True;否则,返回False
# in        是成员关系(左侧是右侧的成员),返回True;否则,返回False
# not in    不是成员关系(左侧不是右侧的成员),返回True;否则,返回False
print()
print("·········选择结构·········")
print()
# !!!选择结构语法
# if 条件语句:
#     代码块
# elif 条件语句:    # 24行与25行语句根据情况自行选择使用或不使用
#     代码块
# else:            # 26行与27行语句根据情况自行选择使用或不使用
#     代码块
# !注意:冒号和缩进
# protocol = input("Please input protocol name:")
protocol = "tcp"
protocol = protocol.lower()
if protocol == "tcp":
    print("TCP's protocol id is 6")
elif protocol == "udp":
    print("UDP's protocol id is 17")
else:
    print("I don't know")

print()
print("·········循环结构·········")
print()

# !!!循环结构语法
# 1.简单for循环
print("```(1)```")
# for 变量 in 序列:
#     代码块
# !注意:冒号和缩进
# 序列有元组、列表、字典、集合等
device = {"IP": "127.0.0.1", "hostname": "local", "port": "80", "webServer": "tomcat", "App": "IDEA"}
for x in device:
    print("This device's %s is %s" % (x, device[x]))
device_key = {"IP", "hostname", "port", "webServer", "App"}
device_value = {"127.0.0.1", "local", "80", "tomcat", "IDEA"}
for x in device_key:
    print(x)
for x in device_value:
    print(x)
# 2.for循环增强版,for...else...结构
print("```(2)```")
# for 变量 in 序列:
#     if 条件语句:
#         代码块
#         break
# else:
#     代码块
# 当for循环正常退出时,执行else内语句;当for循环break退出时,不执行else内语句。
# 补充:退出for循环的三种方式
#      (1)遍历完序列中所有元素后正常退出
#      (2)在特定条件下(根据业务情况自行选择使用)使用break语句强制退出
#      (3)在函数中,可使用return语句强制退出(不仅退出循环,还直接退出函数的执行;同样根据业务逻辑自行选择使用)
ip = ["192.1.1.1", "192.1.1.2", "192.1.1.3", "127.0.0.1"]
# myIp = input("Please input your IP:")
myIp = "127.0.0.1"
for x in ip:
    if x == myIp:
        print("Your IP has been entered")
        break
else:
    print("Your IP is not entered")
# 3.while循环
print("```(3)```")
# while 条件语句:
#     代码块
# !注意:冒号和缩进
devices = {"R1": "1.1.1.1", "R2": "1.1.1.2", "R3": "1.1.1.3", "R4": "1.1.1.4"}
while True:
    list_router = devices.keys()
    # device.keys()返回的类型为dict_keys
    # print(type(list_router))
    # print(list_router)
    # 使用list()函数将list_router变为列表
    list_router = list(list_router)
    # print(type(list_router))
    # print(list_router)
    print("Select one host to connect")
    for router_name in list_router:
        print(router_name)
    print("input e to exit")
    router_input = input("Please select one router:")
    router_input = router_input.upper()
    if router_input in list_router:
        print("I will connect to router %s %s" % (router_input, devices[router_input]))
    elif router_input == "E":
        print("exit")
        break
    else:
        print("Unknow hostname")

你可能感兴趣的:(python,编程语言,语法)