Python基础---Task01:变量、运算符与数据类型

变量、运算符与数据类型

首先非常感谢LOSGGroup举办这次Python基础刻意练习,接下来的17天督促自己采用中英文结合,以便更好深化强化学习python。


1.variable变量

在python里,无需像C++需要声明变量类型,变量被赋予什么数据结构,就是对应类型的变量。

解压序列赋值给多个变量, 同时可用_占位

a_string, an_int,_, a_float, a_bool = ['IDLE', 100,  'nut', 287.1,  True]  
#分别给a_string, an_int, a_float, a_bool 赋值, 丢弃'nut'
print('a_string:',a_string, '\n',
    'an_int:', an_int,'\n',
    'a_float:', a_float,'\n',
    'a_bool:', a_bool)

>>runfile('E:/Anaconda3/data_analysis/python/variable_operator_dataStructure.py', wdir='E:/Anaconda3/data_analysis/python')
a_string: IDLE 
an_int: 100 
a_float: 287.1 
a_bool: True

2.operator运算符

优先级 : ()> 一元运算符 > 算术运算符 > 逻辑运算符 > 关系运算符 > 赋值运算符

算数运算符及优先级:

** > */// > +-

1/3**3 + 1//3
# 0.1111111111111111

注意:python 不支持部分复合运算符如:++, *=,+=

逻辑运算符:

与and 或or 非not

s = 'python 是脚本语言'
index = 13
print(index != len(s) and  isinstance(s,str))
# 先 执行 index != len(s), 结果非0时才执行 isinstance(s,str)
# 结果:True

注意 :从左运算

关系运算符:

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

i, j, k = [0,2,1]
print(i if i>j and j>k else False)
# if 成立则返回 i 否则返回 Falase
# 结果 :False 
赋值运算符:

=

3.dataStructure数据结构

基础数据结构:int, float, bool

容器数据结构:list, tuple, set, dict

#基本类型
an_integer  = 100 # 整型
a_float = 3.14  # 浮点型
a_bool = True  # 布尔型

# 可迭代类型
a_string = 'python' # 字符
a_list = [1,2,3] #列表
a_tuple =  ('B','A','T') # 元组
a_set = {1,1,2,3,5,8} # 集合
a_dictionary = {'语言':3, '等级': 0, '学习时长(days)': 17}  #字典

检查对象类型
  • type()
print('a_list is:',type(a_list)) 
# a_list is: <class 'list'>
  • isinstance()
from collections.abc import Iterable
print('is a_set a Iterable:',isinstance(a_set,Iterable))  #检查是否为可迭代对象
# is a_set a Iterable: True
查看类型信息:dir()
print(dir(a_tuple))  # 某object对应class的所有method name

# ['__add__', '__class__', '__contains__',
# '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',
# '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
# '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__',
# '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', 
# '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', 
# '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

创建某类数据结构
lang_2 = bin(112358) # bin(integer)---把整型转换为二进制
str1 = str(lang_2)
print(str1,'is a string:',isinstance(str1,str))
# 结果:0b11011011011100110 is a string: True

# 类似还有 int(), float(), bool(), list(), tuple(), set(), dict() 
# 以上均可用 help(funcName) 查看属性使用方法

参考链接:
LOGOGroup https://mp.weixin.qq.com/s/deWdmugnAGtnYmoJiDMTNg
Python CooKbook
https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html

反思

遗忘曲线真的可怕,坚持复述才能内化 !

1 day 在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值