4

python生产力工具

 1 year ago
source link: https://www.cnblogs.com/lori/p/17434596.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

python做为一个使用简单,容易上手的编程语言,在大数据,人工智能出现之后,被使用的更加广泛了,通过它来写一个数据处理,挖掘更加得心应手了。

IDE生产力工具

  • PyCharm 2022.2 (Professional Edition),属于jetbrain公司的一个产品
  • 本地安装python3.6,pip包管理工具

helloword

import hello


class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printInfo(self):
        print("name:%s,age:%d" % (self.name, self.age))


if __name__ == '__main__':  # 如果是主程序,就执行下面的代码
    hello.printName("zzl")
    data = {}
    data['name'] = 'zzl'
    data['age'] = 18
    data['gender'] = 'male'
    print(data)
    addr = []
    addr.append('beijing')
    addr.append('shanghai')
    addr.append('guangzhou')
    print(addr)
    subject = []
    subject.append(['math', 'english', 'chinese'])
    subject.append(['math', 'wuli', 'huaxue'])
    print(subject)
    for line in subject:
        print("行显示:%s" % line)
    stu = Student('zzl', 18)
    stu.printInfo();

从代码中可以看到,你不需要像.net,java那样提前定义变量的类型,它都是在解释的时候将类型推断出来的,这一点还是挺简洁的。

Hello, zzl!
{'name': 'zzl', 'age': 18, 'gender': 'male'}
['beijing', 'shanghai', 'guangzhou']
[['math', 'english', 'chinese'], ['math', 'wuli', 'huaxue']]
行显示:['math', 'english', 'chinese']
行显示:['math', 'wuli', 'huaxue']
name:zzl,age:18

来一个两数之和的算法

从数组中找出求和结果为目标值的两个元素的索引值,并进行输出

class Solution():
    def twoSum(self, nums, target):
        hashdict = {}
        for i, item in enumerate(nums):
            if (target - item) in hashdict:
                return (hashdict[target - item], i)
            hashdict[item] = i
        return (-1, -1)
s = Solution()
print(s.twoSum([2, 7, 11, 15], 9))
# 结果(0,1)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK