Python从入门到放弃

第一天

1.数据类型

内置的数据类型:
    int:整型,没有long, 1, 2,3
    float: 浮点型(小数) 1.1, 2.2, 3.3
    complex: 复数: a + bj
    str: 字符和字符串都用str来表示, 'c', "c" => 字符串c
    bool:布尔类型 => True, False
    None: 空类型, 其他语言中:null
    bytes: 字节类型,字节类型和字符串可以互相转换。b''
    tuple: 元组的意思 (1, 2, 3, 4) => 数组
    元组(tuple)是关系数据库中的基本概念,关系是一张表,表中的每行(即数据库中的每条记录)就是一个元组,每列就是一个属性。 在二维表里,元组也称为行
    list:列表[1,2,3,4]
    dict: 字典(dictionary) {1: 2, 3: 4} => key:value 就是一个元素
    set: 集合: 无序不重复的集合。
         1, 2, 3,2 =》 set => 1,2,3</span></span>

字符

指类字形单位或符号,包括字母、数字、运算符号、标点符号和其他符号,以及一些功能性符号。是数据结构中最小的数据存取单位,通常由8个二进制位(一个字节)来表示一个字符。

字符串

在存储上类似字符数组,所以它每一位的单个元素都是可以提取的,如s=“abcdefghij”,则s[1]=“b”,s[9]="j"

class: 类,所有的类class类型
           class int123: => 类似于创建了一种新的数据类型
              pass
    自定义的数据类型: class user_define</span></span>

2.变量

什么是变量?

变量:给一个value打一个标签,起一个名字

可以通过变量去访问它对应的值

变量的类型由值来决定

变量的定义:

每个变量在定义的时候,都必须要赋值。因为赋值之后就有了类型。图为cmd中运行代码。

>>>> a = 1 #定义一个变量,并赋值
>>> a #获取变量的值
1
>>> print(a, type(a)) # 打印a的值,以及a的数据类型
1 <class 'int'> #输出的结果: 1, class int</span></span>

print() : 打印,如果有多个需要打印的,用,隔开就可以

type():获取变量或者数据的类型

变量定义形式:variable_name=value

代码规范: variable_name = value: =号两边都由空格

变量名的要求:

1.变量名一定要有意义:便于自己查看,便于别人查看

2.变量名一般都是小写:如果有多个单词,用下划线连接

int_data = 1
print(int_data, type(int_data)) #,号后边也有一个空格</span></span>
E:\learningSoftware\pythonSoftware\python_interpreter\python.exe #使用的是哪个python解释器
D:/lwz_data/openlab/20210816Python/first_python.py# 运行的是哪个文件
#执行完first_python.py的结果:
Hello World
1 <class 'int'>
#程序执行完成并且退出码是0 #你的程序正常执行退出了
Process finished with exit code 0
#程序在执行过程中遇到了问题,然后在遇到问题的地方终止了
Process finished with exit code 1</span></span>

python注释:1.提高代码的可读性,2.注释中的代码不执行

单行注释:注释单行符号 #

快捷键:ctrl + /

解注释:快捷也是ctrl + /或者去除#

多行注释:注释多行 """ """, ''' '''成对存在的

python代码风格:

如何去判断代码的分隔: 根据缩进:4个空格,以及:

没有缩进和冒号:按行去执行就可以

基本数据类型变量定义:int, float,complex, bytes, None, str

print("Hello World")
int_data = 1
# 1 / 0
print(int_data, type(int_data))
float_data = 2.2
print("float_data: ", float_data, type(float_data))
complex_data = complex(1, 2)
print("complex_data:", complex_data, type(complex_data))
bool_data = True
print("bool_data:", bool_data, type(bool_data))
none_data = None
print("none_data:", none_data, type(none_data))
print(80 * "*")
#bytes
bytes_data = b'123'
print("bytes_data:", bytes_data, type(bytes_data))
#python中没有字符和字符串的区别
#"c", 'c'
str_data = "123"
str_data_2 = '123'
print("str_data:", str_data, type(str_data))
print("str_data_2:", str_data_2, type(str_data_2))
str_data3 = "I'm a student"
str_data3 = 'I\'m a student'
print(str_data3)
str_data4 = 'This word "dash" is usual'
str_data4 = "This word \"dash\" is usual"
print(str_data4)
​
str_data5 = """123"4"5'6'7"""
str_data6 = '''2'3'4'''
print("str_data5:", str_data5, type(str_data5))
print("str_data6:", str_data6, type(str_data6))
以上为验证python中没有字符和字符串的区别,即“”与‘’的区别。
#\: 转移符, 我们在计算机中,标识特殊意义的字符,使其区别于同一符号的作用。
#\n: 换行, \t, \r, \f都具有特殊含义
#n
print(80 * "-")
# print("n")
# print("\n")</span></span>

复杂一点的数据类型:tuple, list, dict, set

tuple: Built-in immutable sequence: 内建不可变序列

<span style="background-color:#dadada"><span style="color:#1f0909">TypeError: 'tuple' object does not support item assignment</span></span>
tuple_data = (1, 2, 3, 4)
print("tuple_data: ", tuple_data, type(tuple_data))
输出结果:tuple_data:  (1, 2, 3, 4) <class 'tuple'>
#下标的操作方式: 序列名[下标]
print(tuple_data[0])
print(tuple_data[1])
print(tuple_data[2])
print(tuple_data[3])
​
tuple_data = tuple((1, 2, 3, 4))
print(tuple_data)
输出结果:
1
2
3
4
(1, 2, 3, 4)
​
​
#如果我要定义一个单个元素元组(未懂)
tuple_data = (1) #() 代表优先级的意思, 代表的是整型 1
# 1 + 2 * 3 => (1+2) * 3 => 1 + (2 * 3)
tuple_data = (1,) => (1, 2, 3) => (1,)</span></span>

tuple中的方法:

 |  count(self, value, /)
 |      Return number of occurrences of value.
 #统计指定数值出现的次数
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  </span></span>

帮助: 一种:ctrl + left Mouse(鼠标左键)

二种: help(对象或类)

list: 列表,使用python过程最常用的一种数据类型

列表的定义,以及下标访问

list_data = [1, 2, 3, 4]
#以中括号,括起来,然后里面的元素以逗号分隔
print("list_data: ", list_data, type(list_data))
​
"""
        Built-in mutable sequence.
        
        If no argument is given, the constructor creates a new empty list.
        The argument must be an iterable if specified.
        # (copied from class doc)
"""
​
list_data = [1, 2, 3, 4]
#以中括号,括起来,然后里面的元素以逗号分隔
print("list_data: ", list_data, type(list_data))
​
print(list_data[0])
print(list_data[1])
print(list_data[2])
print(list_data[3])
​
list_data[0] = 10   赋值0变成10
print(list_data)
输出结果:
list_data:  [1, 2, 3, 4] <class 'list'>
1
2
3
4
[10, 2, 3, 4]可见组成了新的列表
​
​
​
#列表以及我们的元组是可迭代的对象
每一次对过程的重复称为一次“迭代”,而每一次迭代得到的结果会作为下一次迭代的初始值。
list_data = list([1, 2, 3, 4])列表
list_data = list((1, 2, 3, 4))元组
print("list_data: ", list_data, type(list_data))
tuple_data = tuple([1, 2, 3, 4])元组赋值列表
print("tuple_data: ", tuple_data, type(tuple_data))
​
#[1, 2, 3, 4] => tuple([1, 2, 3, 4]) => (1, 2, 3, 4)
#list(), tuple() => 类型转换常用的,将元组与列表进行转换。</span></span>

列表的使用:

操作列表的方法:

<span style="background-color:#dadada"><span style="color:#1f0909">
元组和列表中,都可以放不同类型的元素:以下为帮助列表
append(self, object, /)
 |      Append object to the end of the list. #把对象增加到列表末尾
 |                                            #指的是任意类型的数据
 |  clear(self, /)
 |      Remove all items from list.
 |  
 |  copy(self, /)
 |      Return a shallow copy of the list. #shallow: 浅
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  insert(self, index, object, /)
 |      Insert object before index.
 |  
 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).
 |      
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(self, value, /)
 |      Remove first occurrence of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(self, /)
 |      Reverse *IN PLACE*.
 |  
 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.
 |      
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |      
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |      
 |      The reverse flag can be set to sort in descending order.</span></span>
<span style="background-color:#dadada"><span style="color:#1f0909">id(object): Return the identity of an object. => 返回一个对象的标识</span></span>

Python 直接赋值、浅拷贝和深度拷贝解析 | 菜鸟教程 (runoob.com)为方便理解附上网页

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值