Python编程入门到实践 - 笔记( 5 章)

简介:

第 5 章练习了以下内容

简单的 if 判断语句

判断字符串是否相等,还是不等

进行数字的大小比较

and,or 比较

检查列表中是否存在指定的元素

if,if-else,if-elif-else 语句写法

if 判断列表是否为空

使用多个列表进行比较判断

这一章的内容也比较简单,感觉和 shell 差不多,但还是多练习吧。

希望路过的大牛指出不足,小弟在此谢过了。



一个简单的 if 判断语句

循环打印 cars 列表中的元素,如果其中的元素等于 bmw,就全部大写打印

否则只是将元素的首字母大写

-------------------------------------------------

cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:   
     if car == 'bmw':    
          print(car.upper())    
     else:    
          print(car.title())

-----------------------------------------------------

Audi   
BMW    
Subaru    
Toyota



判断是否相等

大小写不一样,也会不等

--------------------------

car = 'Audi'   
print(car == 'Audi')

--------------------------

True

--------------------------

car = 'Audi'   
print(car == 'audi')

---------------------------

False



转换大小写进行比较

------------------------------------

car = 'Audi'   
print(car.lower() == 'audi')

------------------------------------

True



检查是否不相等

不过两个值不相等,就打印

-----------------------------------------------

requested_topping = 'mushrooms'

if requested_topping != 'anchovies':   
      print("Hold the anchovies!")

------------------------------------------------

Hold the anchovies!



数字的比较

----------------------

age = 18   
print(age == 18)

----------------------

True


进行 if 语句判断,如果两个数字不等,就打印

-----------------------------------------------------------------------------

answer = 17   
if answer != 18:    
     print("That is not the correct answer. Please try again!")

-----------------------------------------------------------------------------

That is not the correct answer. Please try again!



不光可以进行比较是否相等,还可以比较大小

直接在 python 的 IDE 下进行比较是不是看着更方便。哈哈哈

>>> age = 19   
>>> age < 21    
True    
>>> age <= 21    
True    
>>> age > 21    
False    
>>> age >= 21    
False



检查多个条件

and 当两个条件都满足的情况下,打印 True 否则打印 False

>>> age_0 = 22   
>>> age_1 = 18    
>>> age_0 >= 21 and age_1 >=21    
False    
>>> age_1 = 22    
>>> age_0 >= 21 and age_1 >=21    
True


or 至少满足一个条件,打印 True 否则打印 False

>>> age_0 = 22   
>>> age_1 = 18    
>>> age_0 >= 21 or age_1 >= 21    
True    
>>> age_0 = 18    
>>> age_0 >= 21 or age_1 >= 21    
False



检查指定的值是否包含在列表中

----------------------------------------------------------------------------

requested_toppings = ['mushrooms', 'onions', 'pineapple']   
print('mushrooms' in requested_toppings)    
print('pepperoni' in requested_toppings)

----------------------------------------------------------------------------

True   
False



给要查找的值指定一个变量并查找,如果不存就打印出来

----------------------------------------------------------------------------

banned_users = ['andrew', 'carolina', 'david']   
user = 'marie'

if user not in banned_users:   
     print(user.title() + ", you can post a response if you wish.")

----------------------------------------------------------------------------

Marie, you can post a response if you wish.



简单的 if 语句

设定年龄为19,进行 if 语句判断,如果大于18就打印

---------------------------------------------------

age = 19   
if age >= 18:    
     print("You are old enough to vote!")

----------------------------------------------------

You are old enough to vote!



if-else 语句

设定年龄为 17,与 18 进行比较,如果大于等于 18 就打印 if 下的语句,

否则打印 else 中的语句

----------------------------------------------------------------------------

age = 17   
if age >= 18:    
     print("You are old enough to vote!")    
     print("Have you registered to vote yet?")    
else:    
     print("Sorry, you are too young to vote.")    
     print("Please register to vote as soon as you turn 18!")

------------------------------------------------------------------------------

Sorry, you are too young to vote.   
Please register to vote as soon as you turn 18!



if-elif-else 语句

年龄设定为 17,分别进行判断,小于 4 多少钱,小于 18 多少钱,其他多少钱进行打印

--------------------------------------------------

age = 17   
if age < 4:    
     print("Your admission cost is $0.")    
elif age < 18:    
     print("Your admission cost is $5.")    
else:    
     print("Your admission cost is $10.")

----------------------------------------------------

Your admission cost is $5.


简化一下上面的写法,将判断的值定义一个变量,最后打印

----------------------------------------------------------------

age = 12   
if age < 4:    
     price = 0    
elif age < 18:    
      price = 5    
else:    
     price = 10

print("Your admission cost is $" + str(price) + ".")

-----------------------------------------------------------------

Your admission cost is $5.



使用多个 elif 代码块

判断条件当然不止3个,这时候就用到了多个 elif 代码块

python 并不要求必须有 else 代码块,虽然书里是这么写的,但我作为小白的我还是倔强的认为这个习惯不太好,

所以自作主张不练这个 ’省略代码块‘ 了

----------------------------------------------------------------

age = 12   
if age < 4:    
     price = 0    
elif age < 18:    
     price = 5    
elif age < 65:    
     price = 10    
else:    
     price = 5

print("Your admission cost is $" + str(price) + ".")

----------------------------------------------------------------

Your admission cost is $5.   



检查特殊元素

判断列表中的元素并指定某个元素,进行判断

-------------------------------------------------------------------------------------------

requsested_toppings = ['mushrooms', 'green peppers', 'extra cheese']   
for requsested_topping in requsested_toppings:    
     if requsested_topping == 'green peppers':    
          print("Sorry, we are out of green peppers right now.")    
     else:    
          print("Adding " + requsested_topping + ".")    
print("\nFinished making your pizza!")

-------------------------------------------------------------------------------------------

Adding mushrooms.   
Sorry, we are out of green peppers right now.    
Adding extra cheese.

Finished making your pizza!



在 循环之前先进行一个 if 判断

if requested_toppings 意识是对列表进行判断,列表中至少有一个元素时,返回 True,

现在这个列表为空,返回值为 False,打印 else 代码块中的内容

--------------------------------------------------------------

requested_toppings = []

if requested_toppings:   
     for requested_topping in requested_toppings:    
          print("Adding " + requested_topping + ".")    
     print("\nFinished making your pizza!")    
else:    
      print("Are you sure you want a plain pizza?")

----------------------------------------------------------------

Are you sure you want a plain pizza?



使用多个列表

对 requested_toppings 进行遍历,和 available_toppings 列表中的元素进行比较

-------------------------------------------------------------------------------------

available_toppings = ['mushrooms', 'olives', 'green peppers',   
                      'pepperoni', 'pineapple', 'extra cheese']    
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']

for requested_topping in requested_toppings:   
     if requested_topping in available_toppings:    
          print("Adding " + requested_topping + ".")    
     else:    
          print("Sorry, we don't have " + requested_topping + ".")    
print("\nFinished making your pizza!")

--------------------------------------------------------------------------------------

Adding mushrooms.   
Sorry, we don't have french fries.    
Adding extra cheese.

Finished making your pizza!

本文转自   mlwzby   51CTO博客,原文链接:http://blog.51cto.com/aby028/1964929

相关文章
|
3天前
|
存储 设计模式 算法
|
1天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
1天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
1天前
|
存储 索引 Python
Python从入门到精通——1.3.1练习编写简单程序
Python从入门到精通——1.3.1练习编写简单程序
|
1天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
1天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
1天前
|
Java 数据库连接 数据处理
Python从入门到精通:3.1.2多线程与多进程编程
Python从入门到精通:3.1.2多线程与多进程编程
|
1天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
机器学习/深度学习 人工智能 Python
|
14天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。