codecademy python study

版权声明:欢迎交流、沟通 QQ:983433479 微信:wj983433479 ;努力学习~赚钱养家不是梦。 https://blog.csdn.net/u012881904/article/details/83450795

https://www.codecademy.com/learn 好像是之前,了解bash脚本的时候,感觉这个网站还不错哦!将Python学习了一下,主要是了解一下语法!其实我平时也是不用的!毕竟是Java开发的程序,虽然说,大学的时候有些课程了解过一些Python的语法,不过时间这个东西很难说,很久不适用慢慢的就忘记了…所以还是得没事的时候学一下,免得别人都说我在debug哈哈哈…,或者来一句头发都要掉光了…这些鸽子就是喜欢开玩笑。其实py也是一种脚本语言,在很多的途径都是有使用的!可能是自己接触的范围比较少吧,学历这个东西不足,了解的范围还是比较少。

如下是我学习的记录:

image.png | left | 747x227

学习Python

  1. https://www.codecademy.com/learn 这个网站跟着敲确实不错!能够记住一些东西,反复的提醒你!多多少少的还是学到一些东西。
  2. http://www.runoob.com/python/python-tutorial.html 菜鸟教程不可少,python2,python3都有,对于我们这种随便玩玩的同志,无所谓啦!
  3. IDEA工具准备好~PyCharm 提示功能鉴权,对于一个习惯了IDEA开发工具的程序员来说,这个工具对于学习Python简单了不少。

image.png | center | 145x119

  1. github 是个好东西!没事自己瞎玩玩!

Note:Java程序估计是忍受不了没有;,没有{}…,没有类型的区别,有点类似Js的感觉,不过这个看起来确实非常的简洁,很多功能使用起来还是十分的方便的!

codecademy 的一些记录

  1. 输入参数
name = raw_input("What is your name")
  1. 读取时间
from datetime import datetime
 print datetime.now()
  1. 打印查看当前导入的类的一些信息
import math
print dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asi
nh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
  1. 定义了函数,定了类没有实现 pass
def print_dic(dic):
    pass


class NewClass(object):
    pass
  1. List 添加删除、遍历
n = [1, 3, 5]

#def remove(self, value): # real signature unknown; restored from __doc__
#       """
#        L.remove(value) -- remove first occurrence of value.
#        Raises ValueError if the value is not present.
#       """
# 删除数据中存在的元素
print n.remove(1)
#打印日志为 NONE
##删除指定的index下标的元素
print n.pop(1)
#打印日志为5
n.append(6)


def print_list(array):
    if type(array) == list:
        for item in array:
            print item
    else:
        print "type is error"

def print_list2(array):
    if type(array) == list:
        for item in range(len(array)):
            print array[item]
    else:
        print "type is error"


if __name__ == '__main__':
    print_list([1, 2, 3])
    print_list2([4, 5, 6])
1
2
3
4
5
6

  1. 类似Map 的dic ,数据字典
def print_dic(dic):
    if type(dic) == dict:
        print dic.items()
        print dic.values()
        print dic.keys()
        for dic_item in dic:
            print dic[dic_item]


if __name__ == '__main__':
    print_dic({"name": "wangji", "hight": "168cm"})

[('name', 'wangji'), ('hight', '168cm')]
['wangji', '168cm']
['name', 'hight']
wangji
168cm
  1. 数组设置默认值
array = [0] * 5
print array


#[0, 0, 0, 0, 0]
  1. join操作符,类似将一个数组然后使用使用
letters = ['a', 'b', 'c', 'd']
print "***".join(letters)

##a***b***c***d
  1. while/else, for/else
    只要循环正常退出(没有碰到break什么的),else就会被执行。
  2. for循序中使用index ,enumerate可以帮我们为被循环对象创建index
def print_list_index(array):
    if type(array) == list:
        for index, item in enumerate(array):
            print index,item
    else:
        print "type is error"

print_list_index(['a', 'b', 'c', 'd'])
0 a
1 b
2 c
3 d
  1. 逗号的使用
def douhao_example():
    a, b, c = 1, 2, "汪吉"
    print a, b, c
    stirng = "d,e,f"
    d, e, f = stirng.split(",")
    print d, e, f

    # print a + "" + b + "" + c
    # error   print a + "" + b + "" + c
    # TypeError: unsupported operand type(s) for +: 'int' and 'str'

1 2 汪吉
d e f
print a, b 优于 print a + " " + b,因为后者使用了字符串连接符,可能会碰到类型不匹配的问题。

  1. List 分片,反转,步长
array = [1, 2, 3, 4, 5]
print array[0:1: 2]
print array[::-2]
##反转,开始,结束,步长
print array[::-1]


[1]
[5, 3, 1]
[5, 4, 3, 2, 1]

  1. lambda 表达式,感觉和c++的差别不大,这里的filter 还有其他的一些map、reduce等等,教程中没得…
 print filter(lambda x: x % 3 == 0, range(0, 100))
#[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
0b1010
  1. 二进制的操作
print 0b111 #7, 用二进制表示10进制数
bin()函数,返回二进制数的string形式;类似的,oct(),hex()分别返回八进制和十六进制数的string。
int("111",2),返回2进制11110进制数。
  1. 类的定义,类的方法都要添加self这个!有点类似指针的感觉
    可以在子类里直接定义和父类同名的方法
    子类调用父类方法可以使用 super(子类,self).父类的方法
    class Children(Parent1,Parent)这样的语法糖
    继承连接
class Fruit(object):
    """A class that makes various tasty fruits."""

    def __init__(self, name="wangji", color="wangji", flavor="wangji", poisonous="wangji"):
        self.name = name
        self.color = color
        self.flavor = flavor
        self.poisonous = poisonous

    def description(self):
        print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor)

    def is_edible(self):
        if not self.poisonous:
            print "Yep! I'm edible."
        else:
            print "Don't eat me! I am super poisonous."


lemon = Fruit("lemon", "yellow", "sour")

lemon.description()
lemon.is_edible()
wangji = Fruit()
wangji.description()

  1. 包的引用
    http://www.runoob.com/python/python-modules.html

总结

在忙也要学习,补充一下能量,无论是什么都行,别闲着就好了…

猜你喜欢

转载自blog.csdn.net/u012881904/article/details/83450795
今日推荐