python else语句和if语句的组合,Python学习笔记——条件结构(if语句,if-else语句,if-elif-else语句、条件嵌套和专门的条件表达式)...

1、if语句

语法:

if 条件表达式:

代码块

条件表达式:比较运算符、成员运算符、逻辑运算符;条件为True时,执行代码块的内容;

2、else语句

语法:

if 条件表达式:

代码块

else:

代码块

3、elif语句

语法:

if 条件表达式:

代码块

elif 条件表达式:

代码块

。。。

else:

代码块

代码示例:

k=input('imput the index of shape:')

if k=='1':

print('corcle')

elif k=='2':

print('oval')

elif k=='3':

print('rectangle')

else:

print('you input invaild number')

4、条件嵌套

条件里面嵌套条件,同等缩进为同一条件结构。

k=input('imput the index of shape:')

if k=='1':

print('corcle')

elif k=='2':

print('oval')

elif k=='3':

sd1=int(input(‘the first side:’))

sd2=int(input(‘the second side:’))

if sd1==sd2:

print(‘the square area is’,sd1*sd2)

else:

print(‘the rectangle area is,’sd1*sd2)

print('rectangle')

else:

print('you input invaild number')

5、专门的条件表达式

x if E else y

先计算表达式E的值,E为真,则返回x,否则返回y。

使用方式:赋值给变量。

x,y,smaller=3,4,0

if x

用条件表达式:

x,y,smaller=3,4,0

smaller=x if x < y else y

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#ifdef和#if defined是C语言的两个预处理命令。它们的作用都是用于判断宏是否被定义。但是它们在使用方式上有一些区别。 #ifdef的通常用法是:#ifdef XXX .... #else .... #endif。它只能判断一个宏是否被定义。如果宏XXX被定义了,则执行其后的代码块,否则执行#else后的代码块。 而#if defined的通常用法是:#if defined xxx1 .... #elif defined xxx2 .... #elif defined xxx3 .... #endif。它可以同时判断多个宏是否被定义。根据条件的不同,选择执行相应的代码块。 区别在于,#ifdef只能在两者选择是否有定义,而#if defined可以在多个选择是否有定义。#if defined可以组成复杂的预编译条件,比如#if defined (AAA) && defined (BBB) xxxxxxxxx #endif或#if defined (AAA) || VERSION > 12 xxxxxxxxx #endif。而#ifdef就不能用这样的复杂条件。 综上所述,当我们只需要判断单个宏是否被定义时,可以使用#ifdef和#if defined,它们的效果是一样的。但是当我们需要判断复杂的条件和情况时,只能使用#if defined。 需要注意的是,无论是使用#ifdef还是#if defined,都需要搭配#endif预处理命令一起使用,以结束代码块的定义。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [#ifdef和#if defined的差别【转载】](https://blog.csdn.net/weixin_46183779/article/details/128719696)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [#ifdef vs #if defined 的差异](https://blog.csdn.net/liuzehn/article/details/127115830)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值