【python零基础入门学习】python基础篇之判断与for循环(二)

news/2024/7/7 21:53:04

 本站以分享各种运维经验和运维所需要的技能为主

《python》:python零基础入门学习

《shell》:shell学习

《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战

《k8》暂未更新

《docker学习》暂未更新

《ceph学习》ceph日常问题解决分享

《日志收集》ELK+各种中间件

《运维日常》持续更新中

判断语句以及for循环

判断语句:

if 条件:
    条件为真时执行的语句
else
    条件为假时执行的语句

if 条件:
    cmd1
elif 条件:
    cmd2
else:
    cmd3


练习:
if 3>0:
    print('ok')
    print('yes')

if 10 in [10,20,30]:
    print('OK')

if -0.0:
    print('yes')#任何值为0的数字都是False

if 1:
    print('yes')
    
if [1,2]:
    print('yes')#非空列表为True

if {1,2}:
    print('yes')#非空对象都是True

if (1,2):
    print('空元组为False')

if ' ':
    print('空格也是一个字符,非空为True')

if not None:
    print('None为False,取反为True')

测试结果:
ok
yes
OK
yes
yes
yes
空元组为False
空格也是一个字符,非空为True
None为False,取反为True

# a = 10
# b = 20
# if a < b :
#     smaller = a
# else :
#     smaller = b
# print(smaller)
#简化版:
a=10
b=20
smaller = a if a < b else b
print(smaller)

测试结果
10

判断学习实例:

登录账号:(密码输入的时候不显示)

import getpass #导入名为getpass的模块

uname = input('username: ')
upass = getpass.getpass('password: ')

if uname == 'bob' and upass == '123456' :
    print('登录成功')
else :
    print('登录失败')
------要在终端下用 python 进行运行py文件 才会显示效果

判断成绩:

grade = int(input("请输入你的成绩: "))
if grade > 90 :
    print("优秀")
elif grade > 80 :
    print("好")
elif grade > 70 :
    print("良")
elif grade > 60 :
    print("及格")
else :
    print("不及格,你需要更努力了")

try:-----输入不是整数的时候或者空的时候会报错
    g = int(input('g:'))
    if g > 90 :
        print('1111')
    elif g > 80 :
        print('111')
    elif g > 70:
        print('11')
    elif g > 60 :
        print('1')
    else:
        print('0')
except:
    print("输入整数")
------
score = int(input('分数: '))

if score >= 60 and score < 70:
    print('及格')
elif 70 <= score < 80:
    print('良')
elif 80 <= score < 90:
    print('好')
elif score >= 90:
    print('优秀')
else:
    print('你要努力了')

猜拳:

>>> import random
>>> random.choices(['aa','bb'])
['bb']
>>> random.choice(['aa','bb'])
'bb'
>>> random.randint(1,100)
14

import random
choices = ['拳头','剪刀','布']
#print(choices)
computer = random.choice(choices)
#print(computer)
while True:
    player = input('请出拳: ')
    print("mychoice:%s computer's choice:%s" % (player , computer))
    if player == computer:
        print('相同结果,请重新出拳: ')
        continue
    elif player == '拳头' and computer == '布' :
        print('你输了')
        break
    elif player == '拳头' and computer == '剪刀' :
        print('你赢了')
        break
    elif player == '布' and computer == '拳头' :
        print('你赢了')
        break
    elif player == '布' and computer == '剪刀' :
        print('你输了')
        break
    elif player == '剪刀' and computer == '布' :
        print('你赢了')
        break
    elif player == '剪刀' and computer == '拳头' :
        print('你输了')
        break

import random
choices = ['拳头','剪刀','布']
#print(choices)
computer = random.choice(choices)
#print(computer)
while True:
    player = input('请出拳: ')
    print("mychoice:%s computer's choice:%s" % (player , computer))
    if player == computer:
        print('相同结果,请重新出拳: ')
        continue
    elif player == '拳头' and computer == '剪刀' :
        print('你赢了')
        break
    elif player == '布' and computer == '拳头' :
        print('你赢了')
        break
    elif player == '剪刀' and computer == '布' :
        print('你赢了')
        break
    else:
        print('你输了')
        break

import random
choices = ['拳头','剪刀','布']
#print(choices)
win_list = [['拳头','剪刀'],['剪刀','布'],['布','拳头']]
inn = """(0)拳头
(1)剪刀
(2)布
请选择(0/1/2): """
while True:
    computer = random.choice(choices)
    # print(computer)
    ind = int(input(inn))
    player = choices[ind]
    print("mychoice:%s computer's choice:%s" % (player , computer))
    if player == computer:
        print('\033[032;1m相同结果,请重新出拳:\033[0m')
        continue
    elif [player,computer] in win_list:
        print('\033[31;1m你赢了\033[0m')
        break
    else:
        print('\033[31;1m你输了\033[0m')
        break

循环:

break:

  • 用于结束循环

#累加1+2+3+...+100
result = 0
counter = 1

while counter < 101:
    result += counter
    counter += 1

print(result)

#三盘两胜局:
# import random
# choices = ['拳头','剪刀','布']
# #print(choices)
# win_list = [['拳头','剪刀'],['剪刀','布'],['布','拳头']]
# inn = """(0)拳头
# (1)剪刀
# (2)布
# 请选择(0/1/2): """
# count = 0
# count2 = 0
#
# while count < 2 and count2 < 2:
#     computer = random.choice(choices)
#     # print(computer)
#     ind = int(input(inn))
#     player = choices[ind]
#     print("mychoice:%s computer's choice:%s" % (player , computer))
#     if player == computer:
#         print('\033[032;1m相同结果,请重新出拳:\033[0m')
#         continue
#     elif [player,computer] in win_list:
#         #print('\033[31;1m你赢了\033[0m')
#         count += 1
#         print('赢了%s次' % count)
#     else:
#         #print('\033[31;1m你输了\033[0m')
#         count2 += 1
#         print('赢了%s次' % count2)
#         
# if count == 2 :
#     print("你赢了")
# else:
#     print("你输了")

# while count < 2 and count2 < 2:
#     computer = random.choice(choices)
#     # print(computer)
#     ind = int(input(inn))
#     player = choices[ind]
#     print("mychoice:%s computer's choice:%s" % (player , computer))
#     if player == computer:
#         print('\033[032;1m相同结果,请重新出拳:\033[0m')
#         continue
#     elif [player,computer] in win_list:
#         #print('\033[31;1m你赢了\033[0m')
#         count += 1
#         print('赢了%s次' % count)
#     else:
#         #print('\033[31;1m你输了\033[0m')
#         count2 += 1
#         print('赢了%s次' % count2)
#         
# if count == 2 :
#     print("你赢了")
# else:
#     print("你输了")

#方法二:
# while 1:
#     computer = random.choice(choices)
#     # print(computer)
#     ind = int(input(inn))
#     player = choices[ind]
#     print("mychoice:%s computer's choice:%s" % (player , computer))
#     if player == computer:
#         print('\033[032;1m相同结果,请重新出拳:\033[0m')
#         continue
#     elif [player,computer] in win_list:
#         #print('\033[31;1m你赢了\033[0m')
#         count += 1
#         print('赢了%s次' % count)
#     else:
#         #print('\033[31;1m你输了\033[0m')
#         count2 += 1
#         print('赢了%s次' % count2)
#     if count == 2 or count2 == 2 :
#          break

continue:

# #1-100的偶数累加
# result = 0
# counter = 0
#
# while counter < 100:
#     counter += 1
#     if counter % 2 == 1:
#     if counter % 2 : 1为真 0为假
#         continue
#     else:(可有可无)
#         result += counter
#
# print(result)

else:

import random
num2 = random.choice(range(1,101))
count = 0
while count<7:
    count += 1
    num = int(input("请输入一个数字: "))
    if num > num2:
        print('大了')
    elif num < num2:
        print('小了')
    elif num == num2:
        print("中")
        break
    else:
        print('不要输入除了1-100之外的数字或者符号哦')
else:
    print("正确答案是:%s 你猜了%s次,你个辣鸡,别猜了" % (num2,count))

for: 

astr = 'hello'
alist = [10, 20, 30]
atuple = ('yyf', 'chao', 'yang')
adict = {'name':'yyf' , 'age':23}

for ch in astr:
    print(ch)

for i in alist:
    print(i)

for name in atuple:
    print(name)

for key in adict:
    print('%s:%s' % (key,adict[key]))

测试结果:
/root/nsd1907/bin/python /root/nsd1907/py02/day02/01.py
h
e
l
l
o
10
20
30
yyf
chao
yang
name:yyf
age:23

在终端用for循环的时候,也需要缩进
>>> a = 'wode'
>>> for i in a:
...     print(i)
... 
w
o
d
e

range用法:
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(6,10)
range(6, 10)
>>> list(range(6,10))
[6, 7, 8, 9]
>>> list(range(6,10,2))
[6, 8]
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
>>> list(range(10,1,-1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]
>>> list(range(10,1))
[]
>>> list(range(10,1,-1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]

# sum100 =0
#
# for i in range(1,101):
#     sum100 += i
#
# print(sum100)

列表实现斐波那契数列: 

# num = int(input('输入一个数字: '))
#
# fib = [0, 1]
#
# for i in range(num):
#     fib.append(fib[-1] + fib[-2])
#
# print(fib)

九九乘法:---for循环的嵌套使用

for i in range(1,4):#控制第几行的打印
    #每行之内再循环打印3个hello
    for j in range(1,4):  #行内重复打印3个hello
        print('hello',end=' ') #print默认在结尾打印回车,改为空格
    print() #每行结尾打印回车

for i in range(1,10):#控制第几行的打印
    #每行之内再循环打印3个hello
    for j in range(1,i+1):  #行内重复打印3个hello
        print('*',end=' ') #print默认在结尾打印回车,改为空格
    print() #每行结尾打印回车


n = int(input('number: '))

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print('%s*%s=%s' % (j, i, i * j), end=' ')
    print()

 列表解析:

 

>>> [5]
[5]
>>> [5+5] ----将表达式的计算结果放到列表中
[10]
>>> [5+5 for i in range(10)] ---通过for循环控制表达式计算的次数
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> [5+i for i in range(10)]  ----再表达式中,使用for中的变量
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> [5+i for i in range(1,11)] ------
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> [5+i for i in range(1,11) if i % 2 ==1] ---通过if判断语句实现过滤,满足判断条件时,才计算表达式
[6, 8, 10, 12, 14]
>>>
>>> ['192.168.1.' + str(i) for i in range(1,255) ]
>>> ['192.168.1.%s' % i  for i in range(1,255) ]
找出  192.168.1.1-254 的地址


http://lihuaxi.xjx100.cn/news/1503002.html

相关文章

权限提升-PostgreSQL数据库提权+第三方应用提权

权限提升基础信息 1、具体有哪些权限需要我们了解掌握的&#xff1f; 后台权限&#xff0c;网站权限&#xff0c;数据库权限&#xff0c;接口权限&#xff0c;系统权限&#xff0c;域控权限等 2、以上常见权限获取方法简要归类说明&#xff1f; 后台权限&#xff1a;SQL注入,数…

什么是架构,架构的本质是什么

不论是开发人员还是架构师&#xff0c;我们都一直在跟软件系统打交道&#xff0c;架构是在工作中出现最频繁的术语之一。那么&#xff0c;到底什么是架构&#xff1f;你可能有自己的答案&#xff0c;也有可能没有答案。对“架构”的理解需要我们不断在实践中思考、归纳、演绎&a…

深入浅出:手把手教你实现单链表

一、什么是链表 链表是一种链状数据结构。简单来说&#xff0c;要存储的数据在内存中分别独立存放&#xff0c;它们之间通过某种方式相互关联。 如果我们使用C语言来实现链表&#xff0c;需要声明一个结构体作为链表的结点&#xff0c;结点之间使用指针关联。 二、单向链表的结…

[国产MCU]-W801开发实例-WiFi连接

WiFi连接 文章目录 WiFi连接1、WiFi连接API介绍2、WiFi连接示例在前面的文章中,我们实现了WiFi热点扫描。本文将介绍如何将W801连接到WiFi网络。 1、WiFi连接API介绍 int tls_wifi_connect(u8 ssid,u8 ssid_len,u8 pwd,u8 pwd_len) **:通过SSID连接WiFi热点 ssid:WiFi的SSID…

C++二级题目3

数组逆序重放 #include<iostream> #include<string.h> #include<stdio.h> #include<iomanip> #include<cmath> #include<bits/stdc.h> int a[2000][2000]; int b[2000]; char c[2000]; long long n; using namespace std; int main() {cin…

每日一题(链表的中间节点)

每日一题&#xff08;链表的中间节点&#xff09; 876. 链表的中间结点 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a; 如下图&#xff1a;可以定义两个结构体指针均从链表的头节点开始向后遍历&#xff0c;fast指针一次走两步&#xff0c;slow指针一次走一步&a…

科技资讯|苹果发布新专利:可在车内定位苹果的智能设备

根据美国商标和专利局近期公示的清单&#xff0c;苹果公司获得了一项名为《车内定位移动设备的系统和方式》专利&#xff0c;概述了在车内狭窄空间内如何定位 iPhone 等移动设备。 Find My 服务现阶段没有使用 UWB 来追踪 iPhone 或者 iPad&#xff0c;而是依赖 GPS 等相关辅…

SoC 总线结构学习记录之系统存储总线(System Memory Bus)与私有设备总线

蜂鸟 E203 SOC总线结构&#xff1a;  蜂鸟 E203 内核 BIU 的系统存储接口 ICB 连接系统存储总线&#xff0c;通过其访问 SoC 中的若干存储组件&#xff0c;譬如 ROM&#xff0c;Flash 的只读区间等。  蜂鸟 E203 内核 BIU 的私有设备接口 ICB 连接私有设备总线&#xff0c…