Python基础(2)--常用数据类型、运算符、表达式

        多说无益 ,直接上代码 对着注释敲一遍 ,再百度百度勉强入门了

# This is a sample Python script.
import datetime
import math
import turtle
from datetime import time
from fractions import Fraction


# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

# python常用内置对象
# 数字
a = 1
a: int = 1
print(a)
b = 1.0
print(b)
c = 3 + 4j
print(c)

# 字符串
s: str = "This is 'python'"
s1 = r'python'
print(s)
print(isinstance(s, str) and isinstance(s1, str))

# 字节串(字符串与字节串为编码与解码的关系)
b: bytes = b"a"
print(type(b))
print(b)

# 字符串字节串,编码解码
a = "长城好汉".encode()
print(a)
print(a.decode())

# 列表,数组
l: list = [1, 2, 3]
print(l)

# 字典,键值对
d: dict = {1: 'dog', 2: 'car', 3: "good"}
print(d)

# 元组
t: tuple = (1, 'a', 3)
print(t)

# 集合,set可变长
Set: set = {1, 2, 3, 4}
Fro: frozenset = {1, 2, 3}

# python无常量,约定大写变量名为常量并默认下文不对其进行修改
MAX = 12
print(MAX)
# 检查MAX类型,输出为int
print(type(MAX))

# 简单调包
# 打印当前时间
time = datetime.datetime.now()
print(time)

# 打印按指定格式排版的时间
time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(time)

# 调用数学模块包
print(math.sin(2))

# 调包绘制圆形
turtle.setup(50, 50, 50, 50)
turtle.circle(20)
turtle.done()
# setup设置窗体大小,四个参数中后两个参数非必选参数
# setup()是非必须的,加上done()防止图窗一闪而过

# 链式编程
x = y = 2022  # 赋值号两边需要空格
print(x)
print(y)

# python系列解包赋值,分隔符","后需要空格
x, y, z = 1, 2, 3
print(x)
print(y)
print(z)

# 基本算术运算
a, b, c = 2, -4, 8
print(a + b)
print(a - b)
print(b * c)
# 整数除法
print(a / b)
# 浮点数除法
print(a // c)
print(a % b)
# 分数类型
x = Fraction(1, 2)
y = Fraction(3.2)
print(x)
print(y)

# 逻辑运算
a, b = 0, 1
print(a or b)
print(a and b)
print(not a)

# 复合赋值运算符,+-*/%**//均可与=构成复合运算符
a = 2
a = a + 2
print(a)
a = 2
a += 2
print(a)

# 特殊运算
a = 1
b = a
print(a == b)
print(a is b)
print(a is not b)
print(isinstance(a, int))

# python支持自动类型转换和强制类型转换
k = 3.3
print(type(k))
k = int(k)
print(type(k))
print(1 + 1.0)

# python文件需要以回车空格结尾

参考文章:

(168条消息) 浮点数、类型转化以及增强赋值运算符___淡墨青衫__的博客-CSDN博客_增强赋值操作符https://blog.csdn.net/qq_38934189/article/details/105950754?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~BlogCommendFromBaidu~default-1-105950754-blog-106532893.pc_relevant_multi_platform_whitelistv2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~BlogCommendFromBaidu~default-1-105950754-blog-106532893.pc_relevant_multi_platform_whitelistv2&utm_relevant_index=1

(168条消息) Python变量类型声明_安静的世界真安静的博客-CSDN博客_python声明变量类型https://blog.csdn.net/u013255730/article/details/113096194

(168条消息) Python中字节串和字符串,不是一个概念没有区别之分_AllardZhao的博客-CSDN博客_字节串https://blog.csdn.net/qq_37189082/article/details/95198696

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七月是你的谎言..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值