Search results

  1. magneto

    🐍 python python grimoire

    accessing global variables locally : lives = 1 def increaseLives(): global lives lives+=1 print(f"global var modified and now equals : {lives}") print(lives) increaseLives() or : lives = 1 def increaseLives(): print(f"global var access point 1 : {lives}") return lives...
  2. magneto

    🐍 python python grimoire

    # python docstring def hellothere(): """this function print a meaningless line of text""" print("hello there") hellothere() #storing functions as vars and in dictionaries: def foo(): print('nice') def bar(): print('drink') dispatcher = {'foo': foo, 'bar': bar} # Note that the...
  3. magneto

    🐍 python python grimoire

    dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict["brand"]) str1="brand" thisdict[str1]="ok" # this can creat a new entry for non preexisting keys print(thisdict[str1]) # iterate dictionary : for key in thisdict: print(key);print(thisdict[key])...
  4. magneto

    🐍 python python grimoire

    #insert char def insertStr(word,insert,index): if(index > len(word)or index<0): return word result = word[:index] + insert + word[index:] return result #replace char def replaceChar(word,insert,index): if(index > len(word)or index<0): return word result =...
  5. magneto

    🐍 python python grimoire

    while loops : i = 1 while i < 6: print(i) i += 1 # break: i = 1 while i < 6: print(i) if i == 3: break i += 1 # continue: i = 0 while i < 6: i += 1 if i == 3: continue print(i) Print a message once the condition is false (while finalizer): i = 1 while i < 6...
  6. magneto

    🐍 python python grimoire

    return : def my_function(x): return 5 * x print(my_function(3)) function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error. def myfunction(): pass recurssion : def tri_recursion(k)...
  7. magneto

    🐍 python python grimoire

    functions : def my_function(): print("Hello from a function") my_function() def my_function(fname, lname): print(fname + " " + lname) my_function("Emil", "Refsnes") If the number of arguments is unknown, add a * before the parameter name: def my_function(*kids): print("The youngest...
  8. magneto

    🐍 python python grimoire

    loops : parts = ["feet","legs","hands"] for part in parts: print(part) # loop chars in string for x in "banana": print(x) #Exit the loop when x is "banana": fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break #Exit the loop when x is "banana"...
  9. magneto

    🐍 python python grimoire

    list : fruits = ["banana", "apple", "peach"] l1 = [l2,l3] #lists c'tor fruits[0] # get indexed item fruits[-1] # get last indexed item fruits.append("grapes") # add to list len(fruits) # list size random.choice(fruits) # selects rand from list explicit list : l1: list[str] = {'hello','hi'}...
  10. magneto

    🐍 python python grimoire

    randomisation : https://www.askpython.com/python-modules/python-random-module-generate-random-numbers-sequences import random print(random.randint(0,10)) adding a module : new file icon, name the file *.py more modules : https://www.askpython.com/python-modules in another file : import...
  11. magneto

    🐍 python python grimoire

    logic operations : draw.io # flowchart tool a = 200 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b") shorthand if if a > b: print("a is greater than b") Short Hand If ... Else a = 2 b = 330 print("A") if a != b...
  12. magneto

    🐍 python python grimoire

    DATA TYPES get data type : type(123) Strings : get char : #subscripting : print("hello"[0]) # output : h num = 5 new_num = str(num) # convert number to string #f-string f"your score is {n1}" int 1234123 #can write as 1_234_123+ exponent : print(3**2) #9 float : a= float(123) # convert to...
  13. magneto

    🐍 python python grimoire

    hello world : print("hello world") ^ online compiler https://thonny.org/ ^ offline debugger print("hello\n world") //new line print settings, code intelligence //enable/disable code corrections print("hello " + input("what is your name ")) //input prompt or select the line and cmd + / (mac)...
  14. magneto

    hello x - man

    nice HQ
Top