
如果您是 python 新手,了解基本操作、数据类型和条件逻辑至关重要。让我们回顾一下一些基本主题。我们将通过示例探讨每个主题。
第 1 章:算术运算符
python提供了多种运算符,可以轻松执行数学运算。以下是最常见运算符的快速概述:
*multiply4 * 1040+addition7 + 916-subtract23 – 419/division27 / 39**power3 ** 29%modulo7 % 43
这些运算符可帮助您处理代码中的数字。以下是一些示例:
# multiplicationresult = 4 * 10print(result) # output: 40# additiontotal = 7 + 9print(total) # output: 16# powersquared = 3 ** 2print(squared) # output: 9
您还可以使用这些运算符为变量赋值:
# define total spend amounttotal_spend = 3150.96print(total_spend) # output: 3150.96
第 2 章:数据类型和集合
在 python 中,您有多种存储数据的方法,每种方法都适合不同类型的任务。
字符串:用于文本。您可以使用单引号或双引号定义字符串。
# defining a stringcustomer_name = 'george boorman'print(customer_name)# double quotes also workcustomer_name = "george boorman"
列表:列表是可以包含多个值的有序集合。
# creating a listprices = [10, 20, 30, 15, 25, 35]# accessing the first itemprint(prices[0]) # output: 10
字典:字典存储键值对,允许您根据键查找值。
# creating a dictionaryproducts_dict = { "ag32": 10, "ht91": 20, "pl65": 30, "os31": 15, "kb07": 25, "tr48": 35}# accessing a value by keyprint(products_dict["ag32"]) # output: 10
集合和元组:
立即学习“Python免费学习笔记(深入)”;
集合:独特元素的集合。tuple:不可变列表,意味着创建后无法更改。
# creating a setprices_set = {10, 20, 30, 15, 25, 35}# creating a tupleprices_tuple = (10, 20, 30, 15, 25, 35)
第 3 章:条件关键字
python 包含几个用于评估条件的关键字,这对于代码中的决策至关重要。
andevaluate if multiple conditions are trueorevaluate if one or more conditions are trueincheck if a value exists in a data structurenotevaluate if a value is not in a data structure
让我们看一些示例来了解这些关键字的实际作用:
使用和:
age = 25 income = 50000 # check if both conditions are true if age > 20 and income > 30000: print("eligible for loan")
使用或:
day = "sunday" weather = "sunny" # check if either condition is true if day == "saturday" or weather == "sunny": print("let's go to the park!")
用于:
fruits = ["apple", "banana", "cherry"] # check if a value is in the list if "apple" in fruits: print("apple is available.")
不使用:
vegetables = ["carrot", "potato", "spinach"] # Check if a value is not in the list if "broccoli" not in vegetables: print("Broccoli is not in the list.")
总结
本概述涵盖了 python 中算术运算、各种数据类型和条件关键字的基础知识。这些基本概念将帮助您构建更复杂的程序。
以上就是Python:算术、数据类型和条件逻辑的基本概念的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1351704.html
微信扫一扫
支付宝扫一扫