双循环案例和pass、break、continue、for循环、range函数
时间:2020-05-09
本文章向大家介绍双循环案例和pass、break、continue、for循环、range函数,主要包括双循环案例和pass、break、continue、for循环、range函数使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1、双循环案例练习
1.1 十行十列小星星
while j < 10: i = 0 while i < 10: print("*",end ="") i += 1 print() j += 1 # ********** # ********** # ********** # ********** # ********** # ********** # ********** # ********** # ********** # **********
1.2 十行十列隔列换色小星星
j = 0 while j < 10: i = 0 while i <10: if i %2 ==0: print("☆",end="") else: print("★",end="") i += 1 print() j += 1 # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★ # ☆★☆★☆★☆★☆★
1.3 十行十列隔行换色小星星
j = 0 while j < 10: i = 0 while i < 10: if j % 2 == 0: print("☆",end = "") else: print("★",end = "") i += 1 print() j += 1
1.4 99乘法表
1.4.1
i = 1 while i <=9: j = 1 while j <= i: print("%d*%d=%2d "%(i,j,i*j),end = "") j += 1 print() i += 1 # 1*1= 1 # 2*1= 2 2*2= 4 # 3*1= 3 3*2= 6 3*3= 9 # 4*1= 4 4*2= 8 4*3=12 4*4=16 # 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 # 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 # 7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 # 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 # 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
1.4.2
i = 9 while i >= 1: j = 1 while j <= i: print("%d*%d=%-2d "%(i,j,i*j),end = "") j += 1 print() i -= 1 # 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 # 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 # 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 # 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 # 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 # 4*1=4 4*2=8 4*3=12 4*4=16 # 3*1=3 3*2=6 3*3=9 # 2*1=2 2*2=4 # 1*1=1
1.4.3
i = 1 while i <= 9: k = 9-i while k > 0: print(" ",end="") k -=1 j = 1 while j <= i: print("%s*%s=%2d "%(i,j,i*j),end="") j += 1 print() i += 1 # 1*1= 1 # 2*1= 2 2*2= 4 # 3*1= 3 3*2= 6 3*3= 9 # 4*1= 4 4*2= 8 4*3=12 4*4=16 # 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 # 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 # 7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 # 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 # 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
1.4.4
i = 9 while i> 0: k = 9 - i while k > 0: print(" ",end="") k -= 1 j = 1 while j <= i: print("%s*%s=%2d "%(i,j,i*j),end="") j += 1 print() i -= 1 # 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 # 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 # 7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 # 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 # 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 # 4*1= 4 4*2= 8 4*3=12 4*4=16 # 3*1= 3 3*2= 6 3*3= 9 # 2*1= 2 2*2= 4 # 1*1= 1
1.5 在100~999中求吉利数字
方法一:
i = 100 while i <= 999: gewei = i % 10 shiwei = i // 10 % 10 baiwei = i // 100 if gewei == shiwei and shiwei == baiwei: print(i) if gewei == shiwei - 1 and shiwei == baiwei -1: print(i) if gewei == shiwei + 1 and shiwei == baiwei + 1: print(i) i += 1
方式二:字符串方式
i = 100 while i <= 999: var = str(i) a = int(var[0]) b = int(var[1]) c = int(var[-1]) if a == b and b == c: print(int(var)) if a == b +1 and b == c +1: print(int(var)) if a == b-1 and b == c-1: print(int(var)) i += 1
1.6 公鸡1块钱1只,母鸡3块钱一只,小鸡5毛钱一只
问: 用100块钱买100只鸡,有多少种买法?
x = 0 while x <= 100: y = 0 while y <= 33: z = 0 while z <= 100: if (x+y+z) == 100 and (x+3*y+0.5*z) == 100: print(x,y,z) z += 1 y+= 1 x +=1
2、pass、break、continue
2.1 pass:表示占位,无代码块可写时,表示过
if 5 == 5:
pass
2.2 break,表示终止本层循环
if 5 == 5: pass i = 0 while i < 10: print(i) if i == 5: break i += 1 #单层循环 i =0 while i < 8: print(i) j = 0 while j < 5: print(j) if j == 2: break j += 1 i += 1 # 仅终止掉本层循环
2.3 continue 表示终止本次循环,进入下一次循环
i = 0 while i < 10: if i == 8: i += 1 # 手动加1.防止进入死循环 continue print("我是" + str(i)) i += 1 # 打印 1 ~ 100 所有不含有4的数字 # 方式一: i = 0 while i <= 100: if i % 10 == 4 or i // 10 == 4 or i == 4: i += 1 continue print(i) i += 1 # 方式二: i = 0 while i <= 100: if "4" in str(i): i += 1 continue print(i) i += 1
3、for循环:可用于遍历容器内的元素(可迭代的对象包括 容器类型的数据、range函数、迭代器)
str1 = "我是一个字符串" for i in str1: print(i,end="") print(type(i)) # 我是一个字符串<class 'str'> list1 = ["我是","列表"] for i1 in list1: print(i1,type(i1)) # 我是 <class 'str'> # 列表 <class 'str'> tuple1 = ("我是","元祖") for i2 in tuple1: print(i2) set1 = {"我是","集合"} for i3 in set1: print(i3) # 注意集合的无序性 dict1 = {"我是":"字典"} for i4 in dict1: print(i4) # 我是 仅打印字典的键
4、range函数:
# range函数 # range(start,end,step) 顾头不顾尾 for i in range(1,10,2): print(i) # 1 3 5 7 9 for i in range(1,10): for j in range(1,i+1): print("%d*%d=%2d "%(i,j,i*j),end="") j += 1 print() i += 1
原文地址:https://www.cnblogs.com/fdsimin/p/12836805.html
- 【Python环境】监督学习之KNN算法
- 【数据科学】什么是数据科学家与数据科学
- Android基础总结(12)——XML和JSON解析
- 【Python环境】scikit-learn的线性回归模型
- Android基础总结(8)——服务
- 你需要每天写代码吗?
- Java基础——多线程
- No.010 Regular Expression Matching
- JavaScript依赖注入的实现思路
- No.011 Container With Most Water
- No.009 Palindrome Number
- Windows DNS API RCE漏洞分析及PoC构造
- 爬虫采集去重优化浅谈
- Android基础总结(10)——手机多媒体的运用:通知、短信、相机、视频播放
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 安装ubuntu时黑屏的解决办法(3种)
- linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方法
- 分布式锁,三种实现方式,性能对比分析!
- Linux 中锁定和解锁用户帐户的三种方法
- centos8 安装 nginx的详细教程(图文)
- Ubuntu系统日志配置 /var/log/messages的方法
- 在Ubuntu 14 上安装 Nginx-RTMP 流媒体服务器的教程
- centos8自定义目录安装nginx(教程详解)
- VMware下Centos7桥接方式网络配置步骤详解
- Linux CentOS 6.5 卸载、tar安装MySQL的教程
- Linux CentOS 6.5 ifconfig查询不到ip的解决方法
- 打卡群2刷题总结1009——二叉树的中序遍历
- 解决Ubuntu 18.04安装VMwareTools错误问题
- 对linux下syslogd以及syslog.conf文件的解读说明
- Linux下安装tomcat并部署网站(推荐)