def stick_up(*parametres, sep=' ', fin='\n'): """Function acting as print() But args must have been formated We must give print a simple str, and specify to end with nothing : print(chaine, end='')""" # Args have tuple form # So we need to convert them parametres = list(parametres) # Every value gets in str for i, parametre in enumerate(parametres): parametres[i] = str(parametre) # Creating the final str chaine = sep.join(parametres) chaine += fin print(chaine, end='')
def divide(x, y): try: result = x / y except ZeroDivisionError: print("division by zero!") else: print("result is", result) finally: print("executing finally clause")
#!/usr/bin/python3.4 from modularites.print import * print('un petit mot') ma_variable = 53 print(ma_variable) def division(nbr, multi = 2): """ Function that multiply any number by two if the 2nd arg has not been given """ p(nbr / multi) # Division(5, 4) # Division(4) def square(nbr): return nbr * nbr square(5) # Nothing happends square_5 = square(5) p(square_5) # Now 25 appears so "print" != "return" (also "return" end the function proceedings) my_lambda_function = lambda nbr: nbr * 2 p(my_lambda_function(3)) def args_unknown(arg1, arg2, *reste): nbr = arg1 * arg2 p(nbr) reste = list(reste) p(reste)
#!/usr/bin/python3.4 import sys sys.path.insert(0, "/home/etilawin/Documents/python/intro") from modularites.print import * try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) #assert 6 < 4 except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) pass except ValueError: print("Could not convert data to an integer.") pass #except AssertionError: #print("Not true so assertion error") except: print ("Unexpected error:", sys.exc_info()[0]) raise pass try: raise Exception('spam', 'eggs') except Exception as inst: print(type(inst)) # the exception instance print(inst.args) # arguments stored in .args print(inst) # __str__ allows args to be printed directly x, y = inst.args
p("Nombre numéro : {}".format(i)) i += 1 chaine = "Coucou" for letter in chaine: #letter takes successively the value of each of the letters one by one if letter in "aeiouyAEIOUY": #Another use of "in" word p(letter) else: p("*") while True: leave = input("Entrez Q pour quitter : ") if leave.lower() == "q": p("bye") break x = 1 while x < 20: if x % 3 == 0: x += 4 p("on ajoute 4 x vaut {}".format(x)) continue else: x += 1 p(x) my_list = ['coucou', 'Ma bite', 'Mes bites'] for i, elt in enumerate(my_list): print("À l'indice {} se trouve {}.".format(i, elt)) #Creates tuples: as lists but not editable
len(final) #Donne la longueur de la chaine en caractères (Attention les espaces et tout comptent!) # ------------------ Parcourir une chaîne grâce à while/for --------------- chaine2 = "Salut" i = 0 while i < len(chaine2): p(chaine2[i]) i += 1 # ------------------ Sélectionner une partie de la chaîne ------------------ mot = "coucou" try: mot[0] = "k" #Ne marche pas except: print("En effet ça ne marche pas, on obtient un TypeError") pass presentation = "hello :3" presentation[0:2] presentation[:2] presentation[2:len(presentation)] presentation[2:] mot = "k" + mot[1:] # Et la ça marche
def hibou(): print('hiboux coucou hiboux...')
def coucou(): print('Voilà je suis la fonction la plus inutile du monde : coucou')
dictionnary["pseudo"] = 'Etilawin' dictionnary["password"] = '******' dictionnary["email"] = '*****@*****.**' p(dictionnary) # del dictionnary["password"] # p(dictionnary) # old_email = dictionnary.pop('email') # Different because .pop method print the value which is deleted # p(old_email) def coucou(): print('Voilà je suis la fonction la plus inutile du monde : coucou') def hibou(): print('hiboux coucou hiboux...') functions = {} functions['c'] = coucou functions['h'] = hibou functions['c']() for cle in dictionnary: print(cle) # Will show keys in a random way but not their values for values in dictionnary.values(): print(values) # Now will show the values thx to method values for key,value in dictionnary.items(): print('Votre {} est : {}'.format(key,value)) # Shows both keys and values # About dictionnaries in functions look in fonctions.py lines 59-61