python碎片化学习笔记——小马仓库

这篇博客涵盖了Python的基础知识,包括组合数据类型的使用、自定义函数(加法和减法)、类的创建(Person和Student)以及文件的写入。此外,还涉及了网络请求、图片下载、正弦函数图的绘制以及随机数的生成。文章深入浅出地介绍了Python编程中的核心概念和技术。
摘要由CSDN通过智能技术生成

'''A  test'''


1.组合数据类型
 

info = ['***', 18, 1007, 100]
print('my name is:', info[0])
print('I am :', info[1], 'old')
print('my students_is: ',info[2])
print('my score is:', info[3])

2.加法函数
 

def add(a, b):
    return a + b
print(add(1,2))
#数值自己改

3.减法函数
 

def sub(a, b):
    return a - b
print(sub(1,2))
#数值自己改

4.创建Person类
 

class Person:
    def __init__(self, name, sex, age):
        self.__name = name
        self.__sex = sex
        self.__age = age

    def show(self):
        print('my name is:', self.__name, '\ti am a: ', self.__sex, '\ti am : ', self.__age, 'years old')

5.创建Student类并继承Person
 

class Student(Person):
    def __init__(self,name,sex,age,stuID):
        Person.__init__(self,name, sex, age)
        self.__stuID = stuID

    def show_stu(self):
        Person.show(self)
        print('My students_id is: ',self.__stuID)

my=Student('马玉泉','男',23,211322)
my.show_stu()

6.创建文件并写入
 

with open('C://Users//Public//姓名.txt', mode='w') as f:
    f.write('我的姓名是***')

7.不限第三库,代码无误, 图片下载成功即可
 

import requests
url='https://img1.baidu.com/it/u=2693481522,4085270923&fm=253&fmt=auto&app=120&f=JPEG?w=650&h=434'
#里面的图片网址自个找
response=requests.get(url)
print(response.status_code)
response.encoding = 'utf-8'
response.encoding = 'GBK'
#下面的变量名自己编一个
jpg=open('get.jpg','wb')
jpg.write(response.content)

8.绘制正弦函数图
 

import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0, 10, 0.1)
y = [math.sin(each) for each in x]
plt.plot(x, y)
plt.show()


'''B test'''



1.创建一个1-50的随机数,并依次输出
 

import random
m_list = []
for i in range(0,50):
    m_list.append(random.randint(0, 50))
    a=m_list[i]
    print(a,end='/')

2.乘法函数
 

def mutl(a,b):
    return a*b
print(mutl(2,3))

3.除法函数
 

def divd(a,b):
    return a/b
print(divd(4,2))

4.创建Person类
 

class Person:
    def __init__(self, name, sex, age):
        self.__name = name
        self.__sex = sex
        self.__age = age

    def show(self):
        print('my name is:', self.__name, '\ti am a: ', self.__sex, '\ti am : ', self.__age, 'years old')

5.创建Student类并继承Person
 

class Student(Person):
    def __init__(self,name,sex,age,stuID):
        Person.__init__(self,name, sex, age)
        self.__stuID = stuID

    def show_stu(self):
        Person.show(self)
        print('My students_id is: ',self.__stuID)

my=Student('马玉泉','男',23,211322)
my.show_stu()

6.输出某路径下的所有文件和文件夹的名称
 

import os
m_dir='D://aDdownload'
#路径自己找一个
for filename in os.listdir(m_dir):
        print(filename)

7.获取网页的状态码和内容
 

import requests
url='*****'
response=requests.get(url)
print(response.status_code)
response.encoding=response.apparent_encoding
print(response.text)

8.绘制地图,筛选出以'r'开头的国家

from pygal_maps_world import maps
from pygal_maps_world.i18n import COUNTRIES
worldmap_chart=maps.World()
worldmap_chart.title='R Counties'
rcodes=[]
for each in COUNTRIES:
    if each.startswith('r'):
        rcodes.append(each)
worldmap_chart.add('R counties',rcodes)
worldmap_chart.render_to_file('map.svg')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值