「Python系列」Python基础语法/数据类型

一、Python基础语法

  1. 变量和数据类型:
# 变量赋值
x = 10
y = "Hello, World!"

# 打印变量
print(x)  # 输出10
print(y)  # 输出Hello, World!

# 数据类型
a = 1  # 整数
b = 3.14  # 浮点数
c = "Python"  # 字符串
d = [1, 2, 3]  # 列表
e = (4, 5, 6)  # 元组
f = {"name": "Alice", "age": 20}  # 字典
g = {1, 2, 3}  # 集合

# 打印数据类型
print(type(a))  # 输出<class 'int'>
print(type(b))  # 输出<class 'float'>
print(type(c))  # 输出<class 'str'>
print(type(d))  # 输出<class 'list'>
print(type(e))  # 输出<class 'tuple'>
print(type(f))  # 输出<class 'dict'>
print(type(g))  # 输出<class 'set'>
  1. 条件语句(if-elif-else):
x = 5
if x > 0:
    print("x is positive")
elif x == 0:
    print("x is zero")
else:
    print("x is negative")
  1. 循环语句(for、while):
# for循环
for i in range(5):
    print(i)  # 输出0, 1, 2, 3, 4

# while循环
x = 0
while x < 5:
    print(x)  # 输出0, 1, 2, 3, 4
    x += 1
  1. 函数定义和调用:
# 函数定义
def greet(name):
    print("Hello,", name)

# 函数调用
greet("Alice")  # 输出Hello, Alice

二、Python数据类型

Python提供了多种内置的数据类型,下面是一些常用的数据类型及其案例代码:

  1. 整数(int):
x = 10
print(x)  # 输出10
print(type(x))  # 输出<class 'int'>
  1. 浮点数(float):
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值