python-day01 phthon概述、基本数据类型、容器类型列表

Python概述

起源

由贵铎·范·罗萨姆(Guido van Rossum)在1989年创立,1991年初python发布了第一个公开发行版

版本

Python2.x:所有系统默认安装的版本
Python3.x:2.x的升级版,于09年2月13日发布

Python特点

高级的数据结构、面向对象、可升级、可扩展、可移植性、易读、易学、内存管理器

Python的运行方式

1)交互解释器

python3  #进入交互页面
exit()或ctrl+D #退出交互页面

2)命令行执行python文件形式

python3 hello.py
或者 chomod +x hello.py &&  ./hello.py

Python语法结构

1)语句块缩进

 缩进风格4个空格最佳(范.罗萨姆风格),相同缩进的一组语句构成一个代码块(代码组)

2)注释及续行

python中一#为注释符号,一行长代码可以用\折行符号分解为几行

3)输入语句input()

输入的任意内容都会被视为字符类型(包括100这样的整数)

4)输出语句print()

print('hello','world',sep='***',end='') 同时输出多个内容用,隔开,sep指定分隔符默认为空格,end指定换行符默认\n

[root@python ~]# python3
Python 3.6.8 (default, Apr 12 2022, 06:55:39) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(123)
123
>>> print(100+200)
300
>>> print('abc'+'def')
abcdef
>>> print(100+'def')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> print('100'+'def')
100def
>>> print('100'+'200')
100200

 python文件中的注释

# 注释一行

' ' '或者" " " 注释多行

[root@python day01]# cat demo01_print.py
# print("hello world")
# print("hello world")

"""

print(100)
"""

'''
print(200)
'''
[root@python day01]# python3 demo01_print.py

逗号隔开实现一次打印多个值,注意打印多个值时类型不限

>>> print(100,200)
100 200
>>> print('abc','def')
abc def
>>> print(100,'def')
100 def
>>> 

指定多个值之间的分隔符 sep

100-200-300-400-500

>>> print(100,200,300,400,500)
100 200 300 400 500
>>> print(100,200,300,400,500,sep='-')
100-200-300-400-500
>>> 

练习:打印ip地址,将ip地址的四个值用.分割

>>> print(192,168,1,1,sep='.')
192.168.1.1
>>> 

指定结束符号,默认是\n换行结束

>>> print(100,200)
100 200
>>> print(100,200,end='\n')
100 200
>>> print(100,200,end='>>>\n')
100 200>>>
>>> print(100,200,end='>>>')
100 200>>>>>> 

Python变量

概念理解

  • 变量变量,也就是会变的量。它是一个装数据的容器,里面的数据是可以变的,因此叫变量
  • 比如 "班长" 就是一个变量,这学期可能是李雷,那下学期就可能是韩梅梅了,老师想安排同学任务,想到的是班长,而非某个具体的同学

变量定义

  • 在 Python 中,每个变量 在使用前都必须赋值,变量 赋值以后 该变量 才会被创建

  • 等号(=)用来给变量赋值

    • = 左边是一个变量名
    • = 右边是存储在变量中的值

变量名 = 值

变量定义之后,后续就可以直接使用了

[root@localhost day01]# cat demo03_var.py
#定义一个变量保存南瓜的数量,每次数量加一
#执行四次后,查看结果
count = 0
#数量加1
count = count + 1
count += 1
count += 1
count += 1
print(count)

[root@localhost day01]# python3 demo03_var.py
4

变量赋值

运算符

程序=数据+算法 

算术运算符
  • 算数运算符是 运算符的一种
  • 是完成基本的算术运算使用的符号,用来处理四则运算 

比较运算符 

[root@localhost day01]# cat demo04_op.py
#比较运算符
print(3>4) #False
print(1==2) #False
age = 23
print(18 <= age <= 60) #True
print(10 <= age >= 30) #False
[root@localhost day01]# python3 demo04_op.py
False
False
True
False
逻辑运算符

[root@localhost day01]# vim demo04_op.py
#逻辑运算符
#and
age = 23
print(age >= 18 and age <= 60) #True
#or 两个条件满足其一
print(age>=60 or age<=12) #False
#not 非 取反
print( not 1>0) # False
[root@localhost day01]# python3 demo04_op.py
True
False
False

数据类型 

数字

基本的数字类型有:

  • int:有符号整数

  • bool:布尔值

    • True:1
    • False:0
  • float:浮点数(小数)

[root@localhost day01]# cat demo05_data.py
n1 = 2 #整型
n2 = 3.14 #浮点型
n3 = True
n4 = False #布尔型
#type() 查看类型
print(n1,type(n1))
print(n2,type(n2))
print(n3,type(n3))
print(3 + True,3 + False) #True为1,False为0
[root@localhost day01]# python3 demo05_data.py 
2 <class 'int'>
3.14 <class 'float'>
True <class 'bool'>
4 3

整数数字表示方式

Python 默认以十进制数 显示

  1. 数字以 0o 或 0O 开头表示为 8 进制数
  2. 数字以 0x 或 0X 开头表示 16 进制数
  3. 数字以 0b 或 0B 开头表示 2 进制数
[root@localhost day01]# vim demo05_data.py
#进制转换
#转为8进制
print(oct(8)) #0o10
#转为16进制
print(hex(255)) #0xff
#转为2进制
print(bin(16)) #0b10000
[root@localhost day01]# python3 demo05_data.py 
0o10
0xff
0b10000
字符串
  • Python 中字符串被定义为引号之间的字符集合
  • Python 支持使用成对的单引号或双引号
  • 无论单引号,还是双引号,表示的意义相同
  • Python 还支持三引号(三个连续的单引号或者双引号),可以用来包含特殊字符
[root@localhost day01]# vim demo06_str.py
s1 = '100'
s2 = "100"
s3 = 'It is a dog'
s4 = "It's a cat"
s5 = 'it"s a ribbit'
s6 = """   hello world  """ #保持原样输出,赋给一个变量,是字
符串而不是注释
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)
print(s6)
s7 = """
          江雪
                【唐】柳元宗
        千山鸟飞绝,
"""
print(s7)
[root@localhost day01]# python3 demo06_str.py
100
100
It is a dog
It's a cat
it"s a ribbit
   hello world  

          江雪
                【唐】柳元宗
        千山鸟飞绝,
字符串索引和切片
  • 使用索引运算符 [ ] 和 切片运算符 [ : ] 可得到子字符串
  • 第一个字符的索引是0,最后一个字符的索引是 -1
  • 子字符串包含切片中的起始下标,但不包含结束下标
索引运算符[ ]

注意:超出索引范围会报错

[root@localhost ~]# python3
Python 3.8.12 (default, May 10 2022, 23:46:40) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> str = 'python'
>>> str[0]
'p'
>>> str[3]
'h'
>>> str[-2]
'o'
>>> str[100]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> str[-100]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
切片运算符 [ : ]

注意:截取时是含头去尾

>>> str = 'python'
>>> str[2:4]
'th'
>>> str[2:5]
'tho'
>>> str[-4:-1]
'tho'
>>> str[:3]
'pyt'
>>> str[-3:]
'hon'
>>> str[:1]
'p'
>>> 

间隔取字符,第三个参数为步长

>>> str = 'helloworld'
>>> str[2:-2]
'llowor'
>>> str[2:-2:1]
'llowor'
>>> str[2:-2:2]
'loo'
>>> str[::2]
'hlool'
>>> 

 字符串翻转

>>> str = 'abcd'
>>> str[::-1]
'dcba'

字符串判断:in,not in判断字符是否处于变量的范围之内

>>> len('hello')
5
>>> 'py' in 'python'
True
>>> 'pt' in 'python'
False
>>> 'pa' in 'python'
False
>>> 'pa' not in 'python'
True
列表
列表的定义
  • List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组

  • 专门用于存储 一串 信息,它能保存 任意数量,任意类型的 Python 对象

  • 列表用 [] 定义,数据 之间使用 , 分隔

  • 列表中的项目 可以改变

  • 列表的 索引0 开始

    • 索引 就是数据在 列表 中的位置编号,索引 又可以被称为 下标
  • 类似于排队,可动态变化

列表操作
  • 使用 in 或 not in 判断成员关系
  • 使用 append 方法向列表中追加元素
[root@localhost ~]# cat demo07_list.py
#定义变量,保存的是一个列表
a = ['hello','world',100,200,True,False,[1,2]]
#末尾追加
a.append(500)
#修改
a[0]='cc'
print(a)
print(a,type(a))
print(len(a))
print(a[0])
print(a[:2])
print(a[::2])
print(100 in a)
print(500 not in a)

[root@localhost ~]# python3 demo07_list.py
['cc', 'world', 100, 200, True, False, [1, 2], 500]
['cc', 'world', 100, 200, True, False, [1, 2], 500] <class 'list'>
8
cc
['cc', 'world']
['cc', 100, True, [1, 2]]
True
False
[root@localhost ~]# 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值