博客
关于我
python基础3
阅读量:797 次
发布时间:2023-03-07

本文共 2917 字,大约阅读时间需要 9 分钟。

Python 常量与基本运算符

作为一名编程新手,了解Python中的常量与基本运算符是非常重要的。它们是编写代码的基础,掌握了这些知识,才能更好地进行后续的学习。

一、常量

常量是指在程序运行中不再改变的值。在Python中,常量通常使用大写字母来表示,这样可以避免与其他代码产生混淆。常见的常量类型包括:

  • 数字常量:如 1032 等。
  • 字符串常量:如 'Hello, World!''Python' 等。
  • 布尔常量:在Python中,TrueFalse 是布尔类型的常量,代表真值和假值。

二、基本运算符补充

1. 算术运算符

算术运算符用于执行加、减、乘、除等基本的算术操作。常见的算术运算符包括:

  • +:加法
  • -:减法
  • *:乘法
  • /:除法
  • //:整数除法
  • %:取模运算
  • **:指数运算

例如:

print(10 / 3)  # 3.333...
print(10 // 3) # 3
print(10 * 3) # 30

2. 赋值运算符

赋值运算符用于将值赋予变量。在Python中,常见的赋值运算符包括:

  • =
  • +=:加法后赋值
  • -=:减法后赋值
  • *=:乘法后赋值
  • /=:除法后赋值
  • **=:指数后赋值

例如:

age = 18
age += 1 # age = 19
age = 18
age /= 3 # age = 6
age **= 2 # age = 36
  • 交叉赋值:可以将多个变量同时赋值。例如:
x, y, z = 10, 20, 30
  • 链式赋值:可以将多个变量通过赋值连接起来。例如:
x = 10
y = x
z = y

3. 链式赋值

链式赋值允许我们在同一行中赋值多个变量。例如:

x = 10
y = x
z = y

4. 解压赋值

解压赋值适用于从列表中提取多个值。例如:

l = [1.2, 2.2, 3.3, 4.4, 5.5]
a, b, c, d, e = l

或者:

l = [1.2, 2.2, 3.3, 4.4, 5.5]
a, b, *_, f = l

三、流程控制之 if 判断

if 判断是用于根据条件执行特定代码的一种流程控制结构。在 Python 中,if 的语法包括:

1. 基本语法

if 条件:
代码1
代码2
代码3
else:
代码1
代码2
代码3

例如:

age_of_bk = 30
print('start.....')
inp_age = input('>>> : ')
inp_age = int(inp_age)
if inp_age == age_of_bk:
print('猜对了')
print('end.....')
else:
print('阿姨好')

2. 语法扩展

  • elif:用于在 if 不满足时,检查其他条件。例如:
if condition1:
...
elif condition2:
...
elif condition3:
...
else:
...
  • 嵌套 if:可以将多个 if 结构嵌套使用。例如:
age = 18
gender = 'female'
is_beautiful = True
is_successful = True
if age >= 18 and age <= 25 and gender == 'female' and is_beautiful == True:
print('开始表白')
if is_successful:
print('在一起')
else:
print('我逗你玩呢。。。')
else:
print('阿姨好')

3. 条件判断

常见的条件判断包括:

  • >=:大于等于
  • <=:小于等于
  • >:大于
  • <:小于
  • ==:等于
  • !=:不等于

例如:

score = input('your score>>> : ')
score = int(score)
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')

四、流程控制之循环

循环是用于重复执行代码的流程控制结构。在 Python 中,常见的循环包括 while 和 for。

1. while 循环

while 条件:
代码1
代码2
代码3

例如:

name_of_bk = 'egon'
pwd_of_bk = '123'
tag = True
while tag:
inp_name = input('your name>>> : ')
inp_pwd = input('your password>>> : ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
tag = False
else:
print('username or password error')
print('other code....')

2. while + break

while True:
print(1)
break
print(2)
print(3)

3. while + continue

count = 1
while count < 6:
if count == 3:
count += 1
continue
print(count)
count += 1

4. while + else

count = 0
while True:
if count == 3:
print('输错的次数过多。。。')
break
inp_name = input('your name>>> : ')
inp_pwd = input('your password>>> : ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
break
else:
print('username or password error')
count += 1

通过以上内容,可以看出,Python 的常量与基本运算符是编程的基础,而流程控制是编写复杂程序的关键。掌握这些知识后,可以更好地开始学习 Python 编程。

转载地址:http://tlofk.baihongyu.com/

你可能感兴趣的文章
Python 下载的 11 种姿势,一种比一种高级!
查看>>
python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>
python读取word表格内容(1)
查看>>
python 中os.path.join 双斜杠的解决办法
查看>>
python 中PIL.Image和OpenCV图像格式相互转换
查看>>
Python 中Semaphore 信号量对象、Event事件、Condition
查看>>
python 中with的使用及样例
查看>>
python读取txt文件的行数
查看>>
Python 中内置的最大堆 API
查看>>
Python 中只有一个 True 和一个 False 对象吗?
查看>>
python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
查看>>
Python 中多线程与多处理之间的区别
查看>>
Python 中如何使用 lambda 函数
查看>>
Python 中如何创建多行字符串?
查看>>
Python 中如何处理异常?
查看>>
Python 中如何实现列表的切片?
查看>>
Python 中如何实现字典的排序?
查看>>