基础知识学习上

1.关于print

  • 分隔符

    • print(1, 2, 3, 4, sep='-')
      print(1, 2, 3, 4, sep='。')
      
    • image-20241112192710763

  • 结尾符

    • print(1, 2, 3, 4, end='?')
      print(1, 2, 3, 4, end='\n') #默认\n
      
    • image-20241112192825342

  • 维持句子原有的结构

    • msg = '''
      1.啊啊啊啊啊
      2.噢噢噢噢
      3.喔喔喔喔
      4.滴滴答答
      '''
      print(msg)
      
    • image-20241112204319655

  • 查看变量类型

    • print(type(msg))
      
    • image-20241112204508657

1.1 format 方法

  • 基本语法

    • # "{位置或名称}".format(值)
      # format() 使用大括号 {} 作为占位符,可以在字符串中插入指定的值。
      
      name = "Alice"
      age = 25
      print("姓名:{},年龄:{}".format(name,age))
      
    • image-20241113163852706

  • 占位符

    • # format() 方法可以使用位置参数,以 {} 中的数字来指定插入的值。
      name = "Alice"
      age = 25
      print("姓名:{1},年龄:{0}".format(name,age))
      
    • image-20241113164306165

  • 关键字参数

    • print("姓名:{name},年龄:{age}".format(name="Alice",age=25))
      
    • image-20241113164440444

  • 格式化数字

    • pi = 3.1415926
      formatted_str = "圆周率: {:.2f}".format(pi)
      print(formatted_str)
      
      num = 5
      formatted_str = "数字: {:03d}".format(num)
      print(formatted_str)  # 输出: 数字: 005
      
    • image-20241113164913518

  • f-string

    • name = "Tom"
      age = 18
      formatted_str = f"姓名: {name}, 年龄: {age}"
      print(formatted_str)  # 输出: 姓名: Tom, 年龄: 18
      
      
    • image-20241113190919092

2.运算符

2.1 除法运算

num = 10
num2 = 3
print(10 % 3) #求余
print(10 / 3) #除法
print(10 // 3) #整除

image-20241113105109336

2.2 幂运算

number = 2
result = number ** 4
print(result)

number = 9
result = number ** 0.5
print(result)

image-20241113105204968

3.条件控制语句

3.1 if语句

# if 条件:
#     代码块

x = 10
if x > 5:
    print("x 大于 5")

# if 条件:
#     代码块1
# else:
#     代码块2

x = 3
if x > 5:
    print("x 大于 5")
else:
    print("x 小于或等于 5")

# if 条件1:
#     代码块1
# elif 条件2:
#     代码块2
# else:
#     代码块3
x = 8
if x > 10:
    print("x 大于 10")
elif x > 5:
    print("x 大于 5 且小于等于 10")
else:
    print("x 小于或等于 5")

image-20241113160433877

3.2 循环语句

  • while循环

    • i = 1
      while i<=10:
          print(i)
          i+=1
      
    • image-20241113191035311

  • for循环

    • # for 变量 in 序列
      #     循环体
      #
      # 序列:比如字符串、列表、元组、集合...
      
      for i in range(1,5):
          print(i)
      
    • image-20241113192550859

  • while…else 或 for…else

    • for i in range(1,5):
          print(i)
      else:
          print('循环结束')
      
      i = 1
      while i <= 10:
          if i == 5:
              print('i==5')
              break
          i+=1
      else:
          print('猜猜我会执行吗?')
      
    • image-20241113194206408

4.复杂数据类型

4.1列表

  • 打印操作

    • # 变量名字 = [元素,元素,元素,元素,...]
      # 列表名[start:end:step] 左闭右开
      
      heros = ['吕布','赵云','张飞']
      print(type(heros))
      
      #索引访问
      print(heros[2])     #正索引: 0  1  2
      print(heros[-1])    #负索引:-3 -2 -1
      
      #切片访问 a[start:end:step]
      print(heros[1:3])
      print(heros[1:])
      print(heros[:2])
      print(heros[-3:-1])
      
      for e in heros:
          print(e)
      
      • image-20241114113104586
  • 增删改查

    • # 变量名字 = [元素,元素,元素,元素,...]
      # 列表名[start:end:step] 左闭右开
      
      heros = ['吕布','赵云','张飞']
      heros.append('刘备')
      heros.insert(0,'关羽')
      
      print(heros)
      
      heros.pop()
      print(heros)
      
      heros.append('杨玉环')
      heros.append('孙尚香')
      print(heros)
      
      heros.pop(0)
      print(heros)
      
      heros.remove('孙尚香')
      print(heros)
      
      heros[0] = '刘备'
      print(heros)
      
      n = heros.index('张飞')
      print(f"张飞在第{n}个位置")
      
    • image-20241113201252446

  • 列表生成式

    • # 列表生成式的格式:[expression for i in 序列 if...] 表达式+循环+条件
      # 运用列表生成式,可以快速通过一个list推导出另一个list,而代码却十分简洁
      
      a = [1,2,4,5,7,9]
      
      b = [i*i for i in a]
      print(b)
      
      b = [i*i for i in a if i%2 == 1]
      print(b)
      
    • image-20241114130828233

4.2字典

  • 打印操作

    • # 变量名字 = {key1:value1,key2:value2,...}
      
      hero = {'name':'孙悟空','age':20,'血量':9999}
      print(type(hero))
      print(hero)
      
      hero = {'name':'孙悟空','age':20,'血量':9999,'对手':['花木兰','元歌']}
      print(hero)
      
      print(hero['血量'])
      print(hero['对手'])
      print(hero.get('性别','未知'))
      
      
    • 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 增删改查

    • hero = {'name':'孙悟空','age':20,'血量':9999,'对手':['花木兰','元歌']}
      print(hero)
      
      hero['性别']='你猜'
      print(hero)
      
      hero['血量']= 2222
      print(hero)
      
      hero.pop('age')
      print(hero)
      
      if '性别' in hero:
          hero.pop('性别')
      print(hero)
      
      print(hero.keys())
      print(hero.values())
      
    • image-20241113202327540

4.3字符串

message = '王者荣耀真的好玩吗?'

print(message[2])
print(message[:4])

print(message.find('真的'))

print(message.startswith('王者'))
print(message.endswith('吗'))

email = '        aaaaaa@aa.com    \t\n\r\n'
print(email.strip())

image-20241113202927130

  • 以指定的字符连接生成一个新的字符串

    • s1 = 'aaaa'
      s2 = 'bbbb'
      s3 = 'cccc'
      s1 = ','.join([s1,s2,s3])
      
      print(s1)
      
    • image-20241114111203260

  • 字符串分割

    • print(s1.split(','))
      

5.函数

# def 函数名(参数列表):
#     函数体
#     return 返回值(可选)
from docutils.nodes import address


def show_person_info(name,age=11):
    print(f"名字:{name},年龄:{age}")

show_person_info('张三',30)
show_person_info('张四')
show_person_info(age=30,name='张五')


def show_person_info(name,age=11,address='桥洞',sex='非人'):
    print(f"名字:{name},年龄:{age},住址:{address},性别:{sex}")

show_person_info('李四',address='马路')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值