输出语句及变量定义

一、输出语句

python 复制代码
print("内容")

1、引号的使用

python 复制代码
print("hello world!!!!")
print("python自动化运维")

# 引号的使用, 单引号,双引号,三引号
print('shell运维')
print("python运维")
print("""Linux
Windows
Unix
Macos""")
print("我的姓名是'Martin'")

2、输出变量的值

python 复制代码
# 输出变量的值
user = "Martin"
password = "redhat"
print(user)
# 你好, Martin
print("你好", user)
print("用户名:", user, "密码:", password)

3、占位符的使用

python 复制代码
'''
占位符
%s      字符串占位
%d      整数
%f      浮点数
%%      %本身
'''

user = "Martin"
password = "redhat"

print("姓名: '%s'" % user)
print("姓名: '%s', 密码: '%s'" % (user, password))

print("数字: %d" % 100)
print("数字: %d" % 3.56)

print("小数: %f" % 3.14)
print("小数: %f" % 20)
print("小数: %.3f" % 3.1415926)


# 50%
number = 50
print("百分比: %d%%" % number)

二、变量的定义、调用

1、变量的定义、调用

变量的定义

python 复制代码
变量名称 = 值

变量名称规范:

  • 只能包含字母、数字、下划线
  • 只能以下划线、字母开头
  • 不能与python关键字冲突
  • 见名知义
python 复制代码
user = "Martin"
print(user)


hostname, ip_address = "node01.linux.com", "10.1.1.1"
print("主机名: %s, 服务器地址: %s" % (hostname, ip_address))

2、交互式定义变量

python 复制代码
变量名称 = input("提示信息")

username = input("用户名: ")
print("用户名: %s" % username)

三、数据类型-----数字

1、数据类型

数字、字符串、列表、元组、字典、集合、Bytes

2、数字的定义

整数、浮点数、复数 10+20i

python 复制代码
data_01 = 10
data_02 = 3.17
data_03 = 10 + 20j

print(type(data_01), type(data_02), type(data_03))

3、数字操作符

1) 数学运算符

+, -, *, /, %, //(地板除), **(次幂)

python 复制代码
print(10 + 4)
print(10 - 4)
print(10 * 4)
print(10 / 4)
print(10 // 4) //取整
print(10 % 4)
print(2 ** 3)

data_01 = 10
data_01 = data_01 + 1
print(data_01)

data_01 += 1
print(data_01)

2) 比较运算符

>, =,==,!=,>=,<=

python 复制代码
>>> 10 > 8
True
>>> 10 < 8
False
>>> 10 == 10
True
>>> 10 != 20
True

3) 逻辑运算符

and, or, not

python 复制代码
>>> 10 > 8 and 9 > 7
True

>>> 10 < 20 or 8 > 34
True

>>> 10 < 20
True

>>> not 10 < 20
False

4、random模块

生成随机数

python 复制代码
import random
# 生成1以内的随机数
data_01 = random.random()
print(data_01)

# 生成指定范围内的随机数
data_02 = random.randint(1, 100)
print(data_02)

示例:

python 复制代码
data_01 = int(input("第一个数字: "))
data_02 = int(input("第二个数字: "))

print("%s + %s = %s" % (data_01, data_02, data_01 + data_02))
print("%s - %s = %s" % (data_01, data_02, data_01 - data_02))
print("%s * %s = %s" % (data_01, data_02, data_01 * data_02))
print("%s / %s = %s" % (data_01, data_02, data_01 / data_02))
print("%s %% %s = %s" % (data_01, data_02, data_01 % data_02))
相关推荐
крон3 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan3 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊4 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1184 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之4 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?5 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头5 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
lyaihao5 小时前
使用python实现奔跑的线条效果
python·绘图
liuyang-neu5 小时前
java内存模型JMM
java·开发语言
ai大师6 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4