【莫烦Python】Python 基础教程——学习笔记

文章目录

本笔记基于p1-p29【莫烦Python】Python 基础教程

大家可以根据代码内容和注释进行学习。

  1. 安装
    我的:python3.8+anaconda+VS code
  2. print()
print(1) # 直接打印数字
print("hello") # 单双引号打印字符串
print('I\'m happy!') # \使符号直接输出
print('apple'+'pen') # 连接字符串
print('chat'+str(4)) # 连接字符串和数字
print(1+2) # 自动计算
print(int('1')+2) # 字符串转整数
print(float('1.2')+2) # 字符串转浮点数
  1. 数学
1+1
2**2 # 2的平方
2**3 # 2的三次方
8%3 # 取余数
9//4 # 取整(向下)
  1. 自变量variable
apple_fruit = 1 # 定义和初始化自变量
print(apple_fruit)
apple_egg = 15+3 # 初始化为运算结果
print(apple_egg)
a,b = 1,2 # 一次性定义
c = a*2
print(a,b,c) # 一次性输出
  1. while循环
conndition = 1
while conndition < 10: # 当while等于true时往下执行,否则结束
    print(conndition)
    conndition = conndition + 1
  1. for循环
example_list = [1,2,3,4,5,996,345,67,0] # 定义和初始化自变量
for i in example_list:
    print(i) # 循环输出list
    print(("inner of for")) # for内部
# 多行Tab:Ctrl+[
print("outer of for") # for外部,说明python中语言结构非常重要

for i in range(2,10): # range(起始数, 终点数+1)
    print(i) # 输出2到9

for i in range(2,10,2): # range(起始数, 终点数+1, 步长)
    print(i) # 输出2 4 6 8
  1. if条件
x = 1
y = 2
z = 0
if x < y > z: # 当if为true时执行语句,否则结束
    print("x is less than y, and y is greater than z")
if x <= y:
    print("x is less or equal to y")
if x == y:
    print("x is equal to y") # 无输出
if x != y:
    print("x is not equal to y")
  1. if else条件
x = 1
y = 2
z = 3
if x > y:
    print("x is greater than y") # 无输出
else:
    print("x is less or equal to y") # 有输出
  1. if elif else
x = -4
if x > 1:
    print('x > 1')
elif x < -1: # else if
    print('x < -1') # 打印后跳出if
elif x < 1:
    print('x < 1') # 不打印
else:
    print('x = 1')
print('finish')
  1. 函数def
def func(): # 定义函数
    print("This is a function!")
    a=1+1
    print(a)

func() # 调用函数
  1. 函数参数
def fun(a,b): # 定义函数
    c = a*b
    print('the c is',c)

fun(2,4) # 调用函数
  1. 函数默认参数
def sale_car(price,colour,brand,is_second_hand = True):
    print('price:',price,
    'colour:',colour,
    'brand:',brand,
    'is_second_hand:',is_second_hand,)

sale_car(1000, 'red', 'BYD')
sale_car(1234, 'blue', 'BMW', False)
  1. 全局和全局变量
APPLE = 100 # 全局变量
a = None
def fun():
    global a # 改为全局变量,使函数内外值同步改变
    a = APPLE
    return a+100 # 函数返回
print(APPLE)
print('a past=',a)
print(fun())
print('a now=',a)
  1. 模块安装(在环境里)
pip install 包名 # 下载
pip uninstall 包名 # 删除
pip -u 包名 # 升级
  1. 读写文件1
text = 'This is my first test.\nThis is next line.'

my_file = open('my_file.txt','w') # open打开同路径下文件(没有则自动创建),w以覆盖写的形式
my_file.write(text) # 写入文件
my_file.close() # 记得关闭文件
  1. 读写文件2
append_text = '\nThis is appended file.'

my_file = open('my_file.txt','a') # a以追加写的形式
my_file.write(append_text)
my_file.close()
  1. 读写文件3
my_file = open('my_file.txt','r') # r以读的形式打开

# content = my_file.read() # 读取文件内容
# print(content)

# content_list = my_file.readlines() # 逐行读取文件内容,并放入list中
# print(content_list)

first = my_file.readline() # 读取第一行内容
print(first)
second = my_file.readline() # 读取第二行内容
print(second)

my_file.close()
  1. 类class
class Calculator:
    name = 'Good calculator' # 定义类的属性
    price = 18
    def add(self,x,y): # 定义类的功能
        print(self.name)
        print(x+y)
    def minus(self,x,y):
        print(x-y)
    def times(self,x,y):
        print(x*y)
    def divide(self,x,y):
        print(x/y)
    
calcul = Calculator()
print(calcul.price)
print(calcul.add(10, 11))
print(calcul.minus(10, 11))
  1. 类init
class Calculator:
    name = 'Good calculator' # 固有属性
    price = 18

    def __init__(self,name,price,hight,width,weight = 0): # 初始化时必须输入的属性
        self.name = name
        self.price = price
        self.h = hight
        self.wi = width
        self.we = weight
        self.add(1,2) # 初始化时直接运行

    def add(self,x,y): # 定义类的功能
        print(x+y)
    def minus(self,x,y):
        print(x-y)
    def times(self,x,y):
        print(x*y)
    def divide(self,x,y):
        print(x/y)

c = Calculator('Good', 12, 30, 15, 19)
print(c.name)
print(c.add(1,2))
d = Calculator('Good', 12, 30, 15)
print(d.we)
  1. 输入input
a_input = input('Please give a number:') # 接收屏幕输入,字符串类型
if a_input == '1':
    print('This is a good one')
elif a_input == '2':
    print('This is a bad one')
else:
    print('Good luck!')
  1. 元组 列表
a_tuple = (1,3,5,7,23) # tuple元组
another_tuple = 2,4,6,8,10

a_list = [99,3,0,7,23] # list列表

for i in a_tuple:
    print(i)
for i in a_list:
    print(i)

for index in range(len(a_list)): # len求长度,range自动生成数字序列
    print('index=',index,'number in list=',a_list[index])
  1. 列表list
a = [1,2,3,4,1,9]

a.insert(1, 0) # 插入.insert(位置,值)
print(a)

a.remove(1) # 移除第一次出现.remove(值)
print(a)

print(a.index(2)) # 输出值为2的下标

print(a[1]) # 输出下标为1的值
print(a[-1]) # 输出倒数第一的值
print(a[2:5]) # 输出下标2到5-1
print(a[:5]) # 输出前五位
print(a[4:]) # 输出第四位之后

a.sort() # 从小到大排序
print(a)
a.sort(reverse=True) # 从大到小排序
print(a)
  1. 多维列表
a = [1,2,3,4,5]
multi_dim_a = [[1,2,3],[2,3,4],[3,4,5]] # 二维,三行三列

print(a[1])
print(multi_dim_a[0][1])
  1. 字典dictionary
a_list = [1,2,3,4,5]
d = {'apple':1,'pear':2,'orange':[1,2,3]} # 字典dictionary:键+值(值可以为列表、字典、函数等)
print(d['apple']) # 按键查找输出值
print(d['orange'][2])
print(a_list[0])
del d['pear'] # 按键删除
print(d)
d['b'] = 20 # 插入键值对
print(d)
  1. 载入模块import
# import time as t # 引入库并缩写
# print(t.time()) # 获取当前时刻

# from time import localtime # 只引入库中的某个函数
# print(localtime())

from time import * # 引入库中的所有函数,不用time.
print(localtime())
  1. 自己的模块(同一目录下)

m1.py(可放入环境库中)

def printdata(data):
    print('I am m1')
    print(data)

python1.py

import m1
m1.printdata('I am python1')
  1. continue和break
while True:
    b = input('type something')
    if b == '1':
        # break # 跳出while循环
        continue # 跳出本次循环
    print('still')

print('finish')
  1. 错误处理try
try:
    file = open('eeee','r+') # 只读+写入
except Exception as e: # 保存错误信息到e中
    print(e)
    response = input('Do you want to create a new file')
    if response == 'y':
        file = open('eeee','w')
    else:
        pass
else:
    file.write('buffer')
file.close()
  1. zip lambda map
a = [1,2,3]
b = [4,5,6]
print(list(zip(a,b))) # zip将两个列表按列合成
for i,j in zip(a,b):
    print(i/2,j*2)

def fun1(x,y):
    return(x+y)
print(fun1(2,3))

fun2 = lambda x,y:x+y # lambda定义简单函数
print(fun2(2,3))

print(list(map(fun2,[1,3],[2,5]))) # map() 会根据提供的函数对指定序列做映射
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值