Python学習ノート(一)----Python基礎知識関連

5088 ワード

一:Python基礎
import os;

########   ##########
print("Hello,Python")
str = "this is a str"
str1 = 'this is a str1'
str2 = """this is a str2"""
print(str)
print(str1)
print(str, str1, str2)
print("-------------------------------------------------------------" * 2)

########  List##########
list = ['runoob', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']
print(list, tinylist)
print(list[0])
print(list[1:])
print(list[1:3])
print(tinylist * 5)
print("-------------------------------------------------------------" * 2)

########  ##########
tuple = ('runoob', 786, 2.23, 'john', 70.2)  #     ,       
list = ['runoob', 786, 2.23, 'john', 70.2]
print(list)
# tuple[2] = 1000    #         
list[2] = 1000  #         
print(list)
print("-------------------------------------------------------------" * 2)

########  ##########
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'jasun', 'code': 6734, 'dept': 'sales'}  #   json  
print(dict)
print(dict['one'])
print(dict[2])
print(tinydict.keys(), tinydict.values(), tinydict.get("name"))
print("-------------------------------------------------------------" * 2)

########    ##########
tag = False
name = "jasun"
if name == "ljx":
    tag = True
    print(tag, name)
else:
    print(tag, name)

var = 100
if var == 99:
    print("var is 99")
elif var != 100:
    print("var is not 100")
else:
    print("var is 100")

var1 = 5
if var1 > 5 and var1 < 2:
    print("this is not exits ")
else:
    print("var1 values is ", var1)
print("-------------------------------------------------------------" * 2)

########Python for     ##########
for letter in 'jasun':  #      
    if letter == "u":
        break  #       
    print(letter)

fruits = ['banana', 45, 3.21]
for fruit in fruits:  #      
    print(fruit)
#       
for index in range(len(fruits)):
    print(fruits[index])

print("-------------------------------------------------------------" * 2)


########Python   ##########
def printStr(st):
    print(st)


printStr(False)
printStr("jasun")
printStr(5)
printStr(5.123)


# age     , *more     
def printMoreStr(name, age=21, *more):
    print(name)
    print(age)
    for m in more:
        print(m)
    return


printMoreStr(name="jasun", age=1111)
printMoreStr(1717, 12, 12, 12, 12, )

#     
sumLa = lambda la1, la2: la1 + la2
print(sumLa(3, 4))
print("-------------------------------------------------------------" * 2)

########  I / O##########
# inp = input("   ...
") # print(inp) # file = open("D:\ip.txt") print(file.name) print(file.readlines(4)) file.close() # import os os.rmdir("d:\os") os.mkdir("d:\os") # try: fh = open("d:\ip.txt", "w") fh.write(" , !!") except IOError: print("IOError") else: print(" ") fh.close() finally: print("finally ") print("-------------------------------------------------------------" * 2)

二:Pythonオブジェクト向け
########    ##########

class Parent:
    parentAttr = 100

    def __init__(self):
        print("        ")

    def parentMethod(self):
        print("     method")

    def setAttr(self, attr):
        Parent.parentAttr = attr

    def getAttr(self):
        print("  parentAttr :", Parent.parentAttr)

    def printStr(self):
        print("   printStr")

    def testParentExtends(self):
        print("  python     testParentExtends")


class Child(Parent):
    def __init__(self):
        print("        ")

    def childMethod(self):
        print("     method")

    def printStr(self):
        print("      ,    printStr")

    def testChildExtends(self):
        print("  python     testChildExtends")


class Other(Child, Parent):
    def __init__(self):
        print("other     ")


print("-------------------------------------------------------------" * 2)

if __name__ == '__main__':
    #          
    parent = Parent()
    child = Child()
    parent.parentMethod()

    #            
    child.parentMethod()
    child.setAttr(999)
    parent.getAttr()
    child.getAttr()
    #     
    child.printStr()

    #   python     
    other = Other()
    other.testParentExtends()
    other.testChildExtends()


3:Python Gui
from tkinter import *

root = Tk()
root.title("Python")
root.geometry("400x300")


def click(event):
    Message(root, text="    ").pack()


text = Text(width=30, height=3)
btn = Button(root, text="      ")
btn.bind("", click)

text.pack()
btn.pack()
root.mainloop()