python基础笔记(一)

前言

一、配套视频

配套视频: https://www.bilibili.com/video/BV1ex411x7Em?from=search&seid=13312287313219257984

二、python基础初级

三、python基础中级

https://blog.csdn.net/weixin_51585624/article/details/116203832

四、python基础高级

https://blog.csdn.net/weixin_51585624/article/details/116204361
为方便大家学习,在此发布python基础入门部分配套讲义,原作者为黑马程序员, 前100节为Linux基础,所以我从100集开始记录,持续更新…

目录

python基础初级(入门)

00、Hello-word

print("Hello-word")
print("123456")
print("你好。世界")
print("323")
控制台结果

Hello-word
123456
你好。世界
323

Process finished with exit code 0

01、多行注释

"""
print("LiuZhiHui")
Guangdong University of Technology
注释结束了
"""
控制台结果



Process finished with exit code 0

02、算术运算

print(6 // 4)  # 取整
print(6 % 4)  # 取余
print(6 ** 2)  # 幂次方
print("@" * 100)  # 乘法可以拼接 字符串
控制台结果

1
2
36
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Process finished with exit code 0

03、变量

name = "小明"
print(name)
age = 18
print(age)
gender = True
print(gender)
height = 1.74
print(height)
print(type(height))  # 查看变量类型
# python3.0  中没有long型 ,只有int
控制台结果

小明
18
True
1.74
<class 'float'>

Process finished with exit code 0

04、拼接字符串

first_name = "张"
last_name = "三"
name = first_name + last_name
print(name)
控制台结果

张三

Process finished with exit code 0

05、变量的输入

a = input("请输入的数字:")
print(type(a))  # 输入的数据都是字符串类型
控制台结果

请输入的数字:123
<class 'str'>

Process finished with exit code 0

06、数据类型的转换

a='123'
b=int(a)

print(type(a))
print(type(b))
控制台结果

<class 'str'>
<class 'int'>

Process finished with exit code 0

07、格式化输出

a = input("请输入多少元:")
print(type(a))
a = int(a)
print('世界%s值%d元' % ('hello world', a))  # 字符串输出
print("数据的比例是%.2f%%" % a)  # 输出% 要使用%%进行转义  .2f保留2位小数
控制台结果

请输入多少元:100
<class 'str'>
世界hello world值100元
数据的比例是100.00%

Process finished with exit code 0

08、if语句

age = input("请输入你的年龄")
age = int(age)
if age >= 18:
    print("已满18岁,欢迎进入网吧")
else:
    print("你才%d岁,请回家写作业" % age)
控制台结果

请输入你的年龄:16
你才16岁,请回家写作业

Process finished with exit code 0

09、逻辑运算


# 与运算符用 and
# 或运算符用 or
# 非运算符用 not

# 增加缩进 Tab ,  减小缩进 shift+Tab

10、elif语句

"""                           else if的缩写
if 条件1:
    条件1满足执行的代码
elif 条件2:
    条件2满足时,执行的代码
elif 条件3:
    条件3满足时,执行的代码
elif 条件4:
    条件4满足时,执行的代码
else:
    以上条件都不满足时,执行的代码

"""
holiday_name = input("请输入节日:")
if holiday_name == "情人节":
    print("买玫瑰")
    print("看电影")
elif holiday_name == "平安夜":
    print("买苹果")
    print("吃大餐")
elif holiday_name == "生日":
    print("买蛋糕")
    print("吃大餐")

else:
    print("每天都是节日")

控制台结果

请输入节日:今天
每天都是节日

Process finished with exit code 0

11、if嵌套

has_ticket = True
knife_length = 30

if has_ticket:
    print("车票检查通过,准备开始安检")
    if knife_length > 20:
        print("您携带的刀太长了,有%d公分,不允许上车" % knife_length)
    else:
        print("祝您路途愉快")  # 同时满足才给予通过
else:
    print("大哥,请先买票")
控制台结果

车票检查通过,准备开始安检
您携带的刀太长了,有30公分,不允许上车

Process finished with exit code 0

12、随机数

import random

a = random.randint(1, 100)
print(a)
控制台结果

65

Process finished with exit code 0

13、while循环

result = 0
i = 0
while i <= 100:
    print("Hello Python")

    result += i
    i = i + 1
print("1+2+3+....100=", result)
控制台结果

Hello Python
Hello Python
      .
      .
      .
Hello Python
Hello Python
1+2+3+....100= 5050

Process finished with exit code 0

14、break

i = 0
while i < 10:
    print(i)
    i += 1
    if i == 5:
        break  # 中止循环

print("over")
控制台结果

0
1
2
3
4
over

Process finished with exit code 0

15、continue

i = 0
while i <= 10:

    if i == 5:
        i += 1  # 防止死循环
        continue  # 跳出本次循环,执行下次循环

    print(i)
    i += 1
控制台结果

0
1
2
3
4
6
7
8
9
10

Process finished with exit code 0

16、print自动换行

# python的 print会默认自动换行
# end向控制台输出内容结束后,不会换行
print("*")
print("*")
print("*", end="")
print("*")
控制台结果

*
*
**

Process finished with exit code 0

17、垂直对齐

print("1\t2\t\t3")
print("100\t20\t30")

# \n换行符
print("hello\nword")
控制台结果

1	2		3
100	20	30
hello
word

Process finished with exit code 0

18、函数

def sum_2_num(num1, num2):
    """
    求两个数字的和


    """
    result = num1 + num2
    return result


a = sum_2_num(20, 32)
print(a)

#  用lambda来实现: 简单高效
p = lambda num1, num2: num1 + num2
print(p(20, 32))
控制台结果

52
52

Process finished with exit code 0
  • 10
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值