Search results

  1. magneto

    ๐Ÿ python python grimoire

    TKinter : import tkinter # window GUI for the program window = tkinter.Tk() window.title("GUI test") window.minsize(width=500, height=300) # add a label : lbl1 = tkinter.Label(text="label1", font=("Times", 24, "bold")) lbl1.pack() # stick the label view on lbl1["text"] = "txt changed" #...
  2. magneto

    ๐Ÿ python python grimoire

    c'tor overload with kwargs class Car: def __init__(self, **kw): self.make = kw.get("make") self.model = kw.get("model") my_car = Car(make="audi") print(my_car.model) # none
  3. magneto

    ๐Ÿ python python grimoire

    double click a function summon (example: turtle.write()) and see the function doc. var that have ... have default values and are optional varargs : # varargs example def add(*N): sum1 = 0 # iterate tuple varargs for num in N: sum1 += num return sum1 print(add(1, 7...
  4. magneto

    ๐Ÿ python python grimoire

    nato codes dictionary from nato csv file of columns letter, code : # nato dictionary out of csv file using list and dictionary comprehension import pandas data = pandas.read_csv("nato_phonetic_alphabet.csv") # form dataframe nato_dict = {row.letter: row.code for (index, row) in...
  5. magneto

    ๐Ÿ python python grimoire

    # list comprehension aka lambda expressions numbers = [1, 2, 3] new_list = [n + 1 for n in numbers] print(new_list) # prints 2,3,4 # example 2 string to list with word to letter list name = "magneto" letters_list = [letter for letter in name] print(letters_list) # ['m', 'a', 'g', 'n', 'e'...
  6. magneto

    ๐Ÿ python python grimoire

    screen input : screen = screen() answer = screen.textinput(title="",prompt="")
  7. magneto

    ๐Ÿ python python grimoire

    CSV files (excel) example file used : day,temp,condition Monday,12,Sunny Tuesday,14,Rain Wednesday,15,Rain Thursday,14,Cloudy Friday,21,Sunny Saturday,22,Sunny Sunday,24,Sunny read like a csv txt file : with open("weather_data.csv") as file: contents = file.readlines() for line in...
  8. magneto

    ๐Ÿ python python grimoire

    save load with text files create a txt file in the pycharm project (my_file.txt in this example) you can also use the txt files absolute file path (/Users/cake/Desktop/file.txt) or the relative path ../../Desktop/file.txt ./ goes to projects starting folder load : file = open("my_file.txt")...
  9. magneto

    ๐Ÿ python python grimoire

    3rd method to overrite c'tor : class Book: def __init__(self, title: str, author: str, pages: int): self.title = title self.author = author self.pages = pages @classmethod def from_json(cls, book_as_json: str) -> 'Book': book =...
  10. magneto

    ๐Ÿ python python grimoire

    inheritance example class Animal: def __init__(self): print("animal created") def cry(self): print("cry") class Bird(Animal): def __init__(self, eyes): super().__init__() self.num = eyes print(f"bird created with {self.num} eyes") #...
  11. magneto

    ๐Ÿ python python grimoire

    higher order functions : functions that have other functions as parameters from turtle import Turtle, Screen tim = Turtle() screen = Screen() def moveForward(): tim.forward(10); screen.listen() # use function as a param : screen.onkey(key="space", fun=moveForward) # trigger function...
  12. magneto

    ๐Ÿ python python grimoire

    Tuple : the values inside tuple are constant mytuple = ("apple", "banana", "cherry") Print the number of items in the tuple: thistuple = ("apple", "banana", "cherry") print(len(thistuple)) One item tuple, remember the comma: thistuple = ("apple",) print(type(thistuple)) A tuple with...
  13. magneto

    ๐Ÿ python python grimoire

    auto download packages : import pkgName # click on the pkgName error to get an install function example : import heroes print(heroes.gen())
  14. magneto

    ๐Ÿ python python grimoire

    # refer to a module using an alias import ClockUtil as cu print(cu.getTimeStamp())
  15. magneto

    ๐Ÿ python python grimoire

    You can delete properties on objects by using the del keyword: del p1.age or delete an object : del p1 or add attributes externally : p1.level = 9 protip : to clean pasted long lines of code : code, reformat code
  16. magneto

    ๐Ÿ python python grimoire

    All classes have a function called __init__(), which is always executed when the class is being initiated. Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created: class Person: def __init__(self, name...
  17. magneto

    ๐Ÿ python python grimoire

    python package index : UL/DL packages : https://pypi.org/ to DL : file, settings to DL on mac : PyCharm, pref click on your project, project interpreter, +, type package name to search, install package, click ok code example (imported prettytable, a pkg to display ascii tables) #right click...
  18. magneto

    ๐Ÿ python python grimoire

    # summonig objects with turtle cls as example from turtle import Turtle, Screen batch = Turtle() print(batch) batch.shape("turtle") batch.color("blue") batch.forward(100) # moves the object myScreen = Screen() print(myScreen.canvheight) myScreen.exitonclick() # makes the prog exit on screen...
  19. magneto

    ๐Ÿ python python grimoire

    python IDE : https://www.python.org/downloads/ on the install wizard, check Add Python to PATH if you see a msg saying : Disable path length limit ^click on it at this point you can delete the installer. step 2 : DL PyCharm https://www.jetbrains.com/pycharm/download/#section=windows ^select...
  20. magneto

    ๐Ÿ python python grimoire

    multiple import elements : # importing variables from a file named art.py from art import logo,vs switch case #select case def week(i): switcher={ 0:'Sunday', 1:'Monday', 2:'Tuesday', 3:'Wednesday', 4:'Thursday', 5:'Friday'...
Top