【Python | 入门】 从输出打印到面对对象(五分钟速通Python)

【Python | 入门】 从输出打印到面对对象(五分钟速通Python)

🤵‍♂️ 个人主页: @计算机魔术师
👨‍💻 作者简介:CSDN内容合伙人,全栈领域优质创作者。

Python轻松上手

🎏 该篇讲解以代码和结果结合,能够快速上手python基础用法
源码: 传送门

; 一、 编写第一个python.py


print('hello world')

魔术师 = 666
print(魔术师)

print("Majician",魔术师)

print("Majiciam", 魔术师真酷, end='!')
print("Majiciam", 魔术师真酷, sep='-', end='!')

结果:

>>>print('hello world')
hello world
>>>魔术师真酷 = 666
>>>print(魔术师真酷)
666
>>>print("Majician", 魔术师真酷)
Majician 666
>>>print("Majiciam", 魔术师真酷, end='!')
Majiciam 666!

>>> print("Majiciam", 魔术师真酷, sep='-',end='!')
Majiciam-666!

【Python | 入门】 从输出打印到面对对象(五分钟速通Python)

二、 固定数据介绍.py


import math

a = -3.5
b = abs(a)
print(b)

c = math.sin(b)
print(c)

三、 列表的性质以及增删改查.py


all_in_list = [
    1,
    'love',
    True,
    [1, 2, 3]
]

print(all_in_list)

index = all_in_list[1]
index = all_in_list[-3]

index = all_in_list[0:2]
print(index)

all_in_list.append("hello world")
all_in_list.insert(0, "pre_hello")
print(all_in_list)

all_in_list.remove("hello world")
print(all_in_list)

del all_in_list[:2]
print(all_in_list)

all_in_list[0] = 100
print(all_in_list)

x = []
for i in range(10):
    x.append(i)

x = [i for i in range(3,10)]
y = [i**2 for i in range(3,10)]
z = [i**2 for i in range(3,10) if i%2==0]
print(x)
print(y)

四、 小小的任务:求sinx曲线图形面积.py

import math

n = 10
x = []
y = []

width = 2 * math.pi / n

for i in range(n):
    x.append(i * width)
    y.append(abs(math.sin(x[i])))
sums = sum(y) * width

x = [i * width for i in range(n)]
y = [abs(math.sin(i * width)) for i in range(n)]
z = [abs(math.sin(i * width)) * width for i in range(n)]
sums = sum(z)

print(x)
print(y)
print(sums)

五、 常用操作符.py


res = 1 < 2 < 3
res = 'M' in 'Magic'
print(res)

六、 条件判断语句.py


if 1 < 2:
    print('hello')

if 1 < 0:
    print('hello')
else:
    print("world")

if 1 < 0:
    print('hello')
elif 1 < -1:
    print("world")
else:
    print("!")

x = [1, 2, 3, 4, 123, 1, 64, 1, -2]
n = len(x)
for i in range(n):
    for j in range(i):
        if x[j] > x[i]:
            x[i], x[j] = x[j], x[i]

print(n)
print(x)

七、 字符串索引&切片&增删改查


string = "字符串"

string = '''hello

嘿嘿

world'''

string = 'my ,name'

print(string)

res = string[1]
res = string[1:4]

res = string * 3

res = string + ' is '

res = string.split(sep=',')

print(1,string)
string[1] = 'y'

res = string.upper()

print(res)

字典的创建 索引&增删改查


dictionary = {
    'BIDU': 'baidu',
    'SINA': 'sina',
    'YOKU': 'youku',
}

dic = {'h': 'hello', 0.5: [0.3, 0.5], 'w': 'world'}

print(dic)

demo = dic['h']
demo = dic[0.5]
print(demo)

dic['h'] = 'hi'

dic['new'] = 'new dic'

dic.update({1: 2, 3: 4})

del dic['h']

d = {i: i ** 2 for i in range(10)}
d = {i: i ** 2 for i in range(10) if i % 2 == 0}

print(dic)
print(d)

八、 对文件处理操作


f = open('beauty_live.text', 'r')

txt = f.read()
txt = f.read(100)

f.seek(0)

txt1 = f.readlines()

f.close()

print(txt)

九、 统计小说的单词词频


import re

import os

path = os.getcwd()
print(path)

f = open('beauty_live.text', 'r')
txt = f.read()
f.close()

txt = txt.lower()

re.sub('[,.?!"\'-]', '', txt)

txt = txt.split()

word = {}
for i in txt:
    if i not in word:
        word[i] = 1
    else:
        word[i] += 1

sor = sorted(word.items(), key=lambda x: x[1], reverse=True)
print(txt)
print(word)
print(sor)

十、 用户函数自定义


y = lambda x: x ** 2
y1 = lambda x: x[1]

def Sum(x=1, y=2):
    return x + y

res = Sum()
demo = y(3)
demo = y1(['hello', 'world'])

print(res)
print(demo)

def su(x):
    z = 0
    for i in x:
        if i%2==0:
            z +=1
    return z

print(su([1,2,34,5,6,2,4,]))

十一、 python方法与函数对比


list = [2.4, 'hello', 'world']
list.append('hehe')

print(list)

string = 'myapp'

string.append('hi')
list.split()

print(string)

十二、 面对对象实例


class human:
    def __init__(self, ag=None, se=None):
        self.age = ag
        self.sex = se

    def square(self, x):
        return x ** 2

zhangfei = human(ag=28, se='M')

demo = zhangfei.square(3)
demo = zhangfei.age

print(demo)

十三、 python模块


import math
import def_math
from def_math import Sum

res = math.sin(1)
res = math.pi

from math import sin, pi

from def_math import *

res = sin(2)
res = 2 * pi

res = def_math.Sum(1, 3)
res = Sum(1, 3)

print(res)

模块def_math.py

def Sum(x, y):
    return x + y

Original: https://blog.csdn.net/weixin_66526635/article/details/124174983
Author: 计算机魔术师
Title: 【Python | 入门】 从输出打印到面对对象(五分钟速通Python)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/671618/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球