Python3_条件控制语句【IF判断语句】

尚硅谷 Vue2.0 + Vue3.0 入门到精通教程学习笔记 (一) 本文为总结尚硅谷Vue2.0+Vue3.0 的学习笔记系列的第一章——Vue的核心,包含Vue简介、模板语法、数据绑定、MVVM模型、事件处理,计算属性与监视、class与style绑定、列表渲染、过滤器、内置指定和自定义指令以及Vue生命周期等知识点 阅读详情

Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。

可以通过下图来简单了解条件语句的执行过程:

代码执行过程:

if 语句

Python中if语句的一般形式如下所示:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句
如果 "condition_1" 为False,将判断 "condition_2"
如果"condition_2" 为 True 将执行 "statement_block_2" 块语句
如果 "condition_2" 为False,将执行"statement_block_3"块语句
Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else.

注意:

1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
3、在Python中没有switch – case语句.

#!/usr/bin/python3

age = int(input("请输入你家狗狗的年龄: "))
print("")
if age <= 0:
        print("你是在逗我吧!")
elif age == 1:
        print("相当于 14 岁的人。")
elif age == 2:
        print("相当于 22 岁的人。")
elif age > 2:
        human = 22 + (age -2)*5
        print("对应人类年龄: ", human)

### 退出提示
input("点击 enter 键退出")

###以下为if中常用的操作运算符:

if 嵌套

在嵌套 if 语句中,可以把 if...elif...else 结构放在另外一个 if...elif...else 结构中。

if 表达式1:
    语句
    if 表达式2:
        语句
    elif 表达式3:
        语句
    else:
        语句
elif 表达式4:
    语句
else:
    语句

#eg.例子

# !/usr/bin/python3

num=int(input("输入一个数字:"))
if num%2==0:
    if num%3==0:
        print ("你输入的数字可以整除 2 和 3")
    else:
        print ("你输入的数字可以整除 2,但不能整除 3")
else:
    if num%3==0:
        print ("你输入的数字可以整除 3,但不能整除 2")
    else:
        print  ("你输入的数字不能整除 2 和 3")

#!/usr/bin/python
# -*- coding: utf-8 -*-
#Author: zhaosj

# ###判断语句###
# if condition:
#     do
# else:
#     do
# #注意:冒号和缩进
# if condition:
#     do
# elif condition:
#     do
# else:
#     do
++++++++++++++++++++++++++++++++++++++++++++
#eg.:密码登录判断

#定义确认登录函数
def count_login():
    password = input('password:')
    if password == 123456:
        print ('输入成功!')
    else:
        print ('输入错误!请再次输入')
        count_login()

#函数调用
count_login()

#执行结果:

[root@bigdata script_ce]# python zhaosj_if.py 
password:123
输入错误!请再次输入
password:12345
输入错误!请再次输入
password:123456
输入成功!

 

VLA仿真平台:Isaac sim、MuJoCo 在虚拟仿真领域,VLA(Virtual Learning Agent)仿真器是用于开发和训练虚拟机器人代理的环境,它们为机器人提供了一个平台来进行各种任务的模拟和学习,特别是在强化学习(RL)中。两款广泛应用的仿真器是和。这两者各有其特点和优势,适用于不同类型的机器人研究和开发。 阅读详情

相关推荐

从原理到实践:手把手教你如何校准和使用六维力传感器(附常见问题排查)

本文详细解析六维力传感器的工作原理与校准方法,提供从选型到故障排查的完整指南。重点介绍电阻式与电容式技术对比、六步校准法实战以及温度补偿技巧,帮助工程师精准测量空间力场。适用于机器人触觉反馈、航空航天测试等高精度场景。

weixin_29309475的博客 386

Python--条件控制

Python--条件控制

LAO__LIN的博客 850

QuickFix Java 讲解(一)概述、下载方法,和协议内容

本系列力求手把手教你怎样利用QuickFix Java 搭建自己的 FIX 协议收法平台,以及其中的注意事项。 所有源码的地址(免费): https://github.com/zongzhec/QuickFixPractise 1. 什么是FIX,QuickFix,Quick Java? 1.1. 什么是FIX 官网以及其他翻译自官网的帖子都太官方了,这里我们设想一个情景: 你是一个交易员,现在客户打电话来让你下个单买点股票。你啪啪啪的在你的机器上熟练地输入了下单指令。系统显示:成...

4573

python 条件控制

条件判断

x331_的博客 384

Android音频混音器AudioMixer实战:从多路输入到设备播放的完整流程解析

本文深入解析了Android平台上音频混音器AudioMixer的完整工作流程,涵盖从多路音频输入、轨道创建与配置、AudioBufferProvider数据源接入,到核心混音处理与最终设备播放的全过程。文章重点探讨了如何高效管理音频数据流、配置音量与重采样参数,并提供了缓冲区管理与性能优化的实战技巧,帮助开发者构建稳定、低延迟的音频应用。

l3m4n的博客 342

Ubuntu本地部署CodeX编程助手:CLI配置全指南

本地AI编程助手是当前开发者关注的核心技术趋势,指在不依赖云端API、保障代码隐私前提下,于本地环境运行具备代码理解、多语言补全与知识库检索能力的智能辅助工具。其技术原理涉及模型加载、API网关编排、向量数据库集成及系统级资源调度,关键价值在于可控性、低延迟与合规性。典型应用场景包括企业内网DevOps提效、边缘设备嵌入式开发支持,以及敏感代码场景下的离线编程增强。本文聚焦Ubuntu系统下基于CLI的手动配置路径,深入解析Ollama服务注册、Node.js版本锁定、API Key环境隔离及Qwen2.5

weixin_26804345的博客 594

Python条件控制

Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。

qq_69473462的博客 210

Eplan2022 复制已有的宏文件生成新的原理图宏文件

Eplan2022 新建原理图宏文件

haier123888的博客 1万+

【Python基础】- if/else控制语句

本文主要介绍Python中的控制语句if-else,包括单分支和多分支语句。

m0_64336780的博客 2万+

dex2oat编译优化调试

dex2oat编译调试的一些技巧和方法

jidapenglin28的专栏 5045

DDD系列 - 第12讲 如何在DDD中实现Query服务(轻量化CQRS)

轻量化CQRS、整合Repository和DAO的优势。

罗小爬的技术宝书 266

Python 的条件控制语句

程序并非是一成不变的向下执行,有的时候也要根据条件的不同选择不一样的代码,这个时候便用到了分支结构。那么到底如何让程序根据条件的不同选择不一样的代码呢?

win19970108018的博客 919

PCIe协议之 Equalization篇 之 理论篇 之 FFE

作为最后一道“精加工”工序,DFE利用其不放大噪声的优势,精准地消除残余的、强烈的后光标ISI,最终得到一个非常干净、张得很开的眼图,供判决器使用。信号经过信道衰减后变得模糊,首先由CTLE进行“粗调”,提供一个宽频带的增益,将眼图初步打开,为后面的DFE创造一个可以正常工作的信号环境。,它通过将输入信号在不同时间点上的多个副本(称为“抽头”)进行加权求和,来直接塑造或“整形”输出信号的波形。:这是最主要的信号路径,通常系数为正值,决定了信号的幅度。:将所有加权后的信号相加,产生最终的均衡输出。

那么菜的博客 753

3.1 Python3 条件控制

本文介绍了Python中的条件控制结构,主要包括if语句、if嵌套和match-case语句。if语句是最基本的条件判断,包含if-else和if-elif-else结构;if嵌套可以实现更复杂的条件逻辑;Python 3.10引入的match-case语句则提供了更简洁的多条件分支处理方式。文章通过成绩评级、闰年判断、计算器和用户登录验证等4个例程,展示了这些条件结构的实际应用。这些结构是构建程序逻辑的基础工具,合理使用可以编写出清晰高效的代码。

weixin_40978412的博客 1567

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Python3 条件控制

Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。

qq_25502783的博客 821

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Python3(12) 条件控制

Python3 的条件控制语句(if 语句和 match...case 语句)是编程中实现逻辑控制的重要手段。通过灵活运用这些语句,结合各种条件操作符,能让程序根据不同的情况执行不同的代码,实现多样化的功能。在实际编程中,要根据具体需求选择合适的条件控制结构,合理嵌套,以优化代码逻辑和提高程序的可读性。希望这篇博客能帮助你更好地复习和巩固 Python3 条件控制的知识,在编程实践中灵活运用。

Theodore_1022的博客 944

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Python基础_03_if语句

Python3 条件控制 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。 可以通过下图来简单了解条件语句的执行过程: if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_block_2 else: ...

Pythonkilla的博客 303

Python3 条件控制(13)

Python3 条件控制 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。 可以通过下图来简单了解条件语句的执行过程: if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_block_2 else: state...

weixin_42180610的博客 348

python条件语句-Python3 条件控制

Python3 条件控制Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。可以通过下图来简单了解条件语句的执行过程:代码执行过程:if 语句Python中if语句的一般形式如下所示:ifcondition_1:statement_block_1elifcondition_2:statement_block_2else:statement_bloc...

weixin_37988176的博客 418

14.从入门到精通:Python条件控制 if 语句 if 嵌套

其中,condition1和condition2是要检测的条件,statement(s)是要执行的语句块。如果condition1为真,则检测condition2,如果condition2为真,则执行if语句块中的代码,否则执行else语句块中的代码。如果与某个匹配,则执行相应的代码块;总之,if语句是Python中最常用的条件语句之一,可以根据条件执行不同的代码块,使程序具有更好的灵活性和适应性。需要注意的是,if语句嵌套可以无限嵌套,但是过多的嵌套会使代码难以理解和维护,因此应该尽量避免过多的嵌套。

weixin_63740705的博客 5725

python3笔记4--条件控制

python3笔记–if条件控制 条件语句是通过一条或者多条语句的执行结果(条件为真或假)来决定执行的代码块。在python中,使用缩进来区分语句块,相同缩进数的语句在一起组成一个代码块。 执行过程: 1、if用法之一----一般格式 if语句的一般格式如下: if <条件>: <代码块> elif <条件>: <代码块> else: <代码块> <条件控制之外的代码> 例:注意每个条件后都要使用冒号。 i = 5

qq_45285376的博客 197

【python基础教程】(2)Python3 条件控制

Python3 条件控制 Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。 可以通过下图来简单了解条件语句的执行过程: 代码执行过程: 1、if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_...

weixin_42892238的博客 99

【Python基础】条件控制语句

在编程语言中,条件控制语句是实现逻辑判断和程序流程控制的基础工具。Python作为一种高级编程语言,提供了简洁而强大的条件控制语句,使得程序可以根据不同的条件执行不同的代码块。本文将详细介绍Python中的条件控制语句,包括if语句、if-else语句、if-elif-else语句以及相关的嵌套结构和高级特性。什么是Python?

分享是一种境界,勤于分享,乐于分享! 1337

python入门基础知识(条件控制)

Python3 条件控制Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。

qq_52817786的博客 1006

python013 Python3 条件控制

Python3 条件控制 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。 可以通过下图来简单了解条件语句的执行过程: if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_blo

阿木的专栏 304
上一篇: Python3_函数
下一篇: Python3_for循环语句
MR.ILen
博客等级 码龄10年 6粉丝 106原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值