Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from my_dear_library \
- import askTheUserForNumber
- i1 = askTheUserForNumber(
- pTheQuestion="Escreve um n1: "
- )
- # named-params
- i2 = askTheUserForNumber(
- pTheQuestion="Escreve um n1: "
- )
- strMsg:str="Eis os valores que recebi:\n"
- strMsg = strMsg + "i1: "+str(i1)+"\n"
- strMsg = strMsg + "i2: "+str(i2)+"\n"
- strMsg = \
- "Eis os valores que recebi:\ni1: %d\ni2: %d"%\
- (i1, i2)
- strMsg = \
- "Eis os valores que recebi:\ni1: {}\ni2: {}"\
- .format(i1, i2)
- strMsg = \
- f"Eis os valores que recebi:\ni1: {i1}\ni2: {i2}"
- print (strMsg)
- """
- operadores aritméticos
- + adição
- - subtração
- * multiplicação
- / divisão real (com vírgula flutuante)
- // divisão inteira (não há parte fracionária)
- ** potência
- % resto da divisão inteira
- """
- rAdd = i1+i2
- rSub = i1-i2
- rMult = i1*i2
- rDivR:float = i1/i2
- rDivI = i1//i2
- rPot = i1**i2
- rRemainder = i1%i2
- strMsg = f"{i1}+{i2}={rAdd}\n"
- # cumulative concatenation
- strMsg += f"{i1}-{i2}={rSub}\n"
- strMsg += f"{i1}*{i2}={rMult}\n"
- strMsg += f"{i1}/{i2}={rDivR}\n"
- strMsg += f"{i1}//{i2}={rDivI}\n"
- strMsg += f"{i1}**{i2}={rPot}\n"
- strMsg += f"{i1}%{i2}={rRemainder}\n"
- print(strMsg)
- """
- operadores booleanos
- George Boole
- True
- False
- e-lógico and
- ou-lógico or
- negação-lógica not
- """
- b1 = True and False # False (False é elemento absorvente do e)
- print(b1)
- b2 = True or False # True (True é elemento absorvente do out)
- print(b2)
- b3 = not b2 # False #notar que é operador (não está obrigado à notação das functions)
- print(b3)
- *****************************************************************
- # heron.py
- # exemplo de um "guess-and-check" algorithm
- # calcula raiz quadrada de qualquer nº, com qualquer precisão
- import math
- iWantToKnowTheSquareRootOf = x = 525
- guess = -88
- precision = 1/100000000
- distance = (guess**2) - x
- bGoodEnough = distance<=precision
- iTeration = 0
- while (not bGoodEnough):
- iTeration+=1 # iTeration = iTeration+1
- guess = (guess + x/guess)/2
- distance = (guess ** 2) - x
- bGoodEnough = distance <= precision
- strMsg=f"Iteration:{iTeration}\n"
- strMsg+=f"Current guess: {guess}\n"
- strMsg+=f"Current distance: {distance}\n"
- print(strMsg)
- # while
- print("_.-^-."*10)
- strMsg = f"The Square Root of {x} is {math.fabs(guess)}\n"
- print(strMsg)
Advertisement
Add Comment
Please, Sign In to add comment