输出语句及变量定义

一、输出语句

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))
相关推荐
像是套了虚弱散27 分钟前
DevEco Studio与Web联合开发:打造鸿蒙混合应用的全景指南
开发语言·前端·华为·harmonyos·鸿蒙
旭意29 分钟前
C++蓝桥杯之结构体10.15
开发语言·c++
飞翔的佩奇1 小时前
【完整源码+数据集+部署教程】【天线&水】舰船战舰检测与分类图像分割系统源码&数据集全套:改进yolo11-repvit
前端·python·yolo·计算机视觉·数据集·yolo11·舰船战舰检测与分类图像分割系统
麦麦鸡腿堡2 小时前
Java的单例设计模式-饿汉式
java·开发语言·设计模式
简单点了2 小时前
go前后端项目的启动 、打包和部署
开发语言·后端·golang
木头左2 小时前
最大回撤约束下ETF多因子动态止盈参数校准方案
python
爱吃山竹的大肚肚2 小时前
@Valid校验 -(Spring 默认不支持直接校验 List<@Valid Entity>,需用包装类或手动校验。)
java·开发语言
汤姆yu2 小时前
2026版基于python的协同过滤音乐推荐系统
开发语言·python
汤姆yu2 小时前
基于python的电子商务管理系统
开发语言·python
我是大咖3 小时前
C语言-贪吃蛇项目开发工具篇---ncursee库安装
c语言·开发语言