The following text contains sections of interest from using Python for just some quick issues like numerical expression evaluation or character-based transformations and the·like; (‥ not yet in an officially state for to 42be published, nypub℠–serivce·mark) simplest way to use Python is as calculator: > python Python 3.8.14 (default, Oct 4 2022, 06:27:18) [GCC 10.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 3*2**4 # 3·2⁴ 48 >>> 3*(2*2*2*2) 48 >>> a=2+3*5; a 17 >>> (a+1)/2 9.0 >>> print(1--3); 1-(-3); # see below, adding a negative number subtracts, subtracting a n.n. adds 4 4 >>> [ (a+1)>>1, 48, 48<<1, 48>>1, 48>>2, 48>>3, 48//2**4 ]; # in binary system just a bit shift [9, 48, 96, 24, 12, 6, 3] >>> 12345678//10**3 12345 >>> # write numbers to the base of 16, 8, 2 instead of 10, and convert in-between >>> # what is called the hexagesimal, octal and dual place-value system for digits >>> [ 0xA, 0o100, hex(10), oct(5*8) ]; [ 0b1010, bin(11) ]; [10, 64, '0xa', '0o50'] # A₁₆=10₁₀, 100₈=64₁₀; 40₁₀=50₈, shall_be_explained,_but_not_here [10, '0b1011'] >>> import math; math.sqrt(16) # square root, opposite of square 4²=16, □ 4.0 (en/de:) remarks/notes to the code sample (small program) that will follow: Anmerkung zu dem Code Beispiel (kleines Progrämmchen, 2tr:leo.org), das folgt: (en:) Converting Capital- into Small Letters ('ABC'↦'abc'): What you already have · is discounted/subtracted, and the new base of what you wanna have added in the step-of-there·after, for small letters the symbol table code point number of letter ‘a’, that is ord("a"), for capital letters this results in {by me: there·on·behalf = on the behalf of what has been told just there before or around here} in ord("A"), generally you would just add or subtract the difference of the before and after, a negative number being added results effectively in an subtraction. A negative constant is like c=(ord("A")-ord("a")) 'A' comes in the ascii cset. before 'a' and is smaller than 'a' cset. = (here:) character set, ascii = american standard code for information interchange, ascii only 7bit, 2**7=128, today you have at least 1Byte=8bit with 2**8=256 different values. for negative numbers 1Byte can represent ‘integer numbers‘ like this: 1Byte ~ -128…127, reason why there is one more negative number is that zero is considered positive, this/the-used number representation is called 2´s complement. integer number = non-fraction, non-‘real’/comma number, so 1, 2, 3, -5, -27, 887, and not ⅜ or 3.1415 or 1.7e5 (= 1.7*10⁵, the so-called scientific number representation) ascii is part of almost all known character sets, 1Byte character sets are iso8859-1, iso8859-15(same but with € sign), cp850 (old msdos). Today what is usually used is utf-8, ascii characters are one-Byte with a leading zero bit, there can be represented up to 2**16 and more characters using appropriate prefix-bytes. Prefix-Byte is a here specially constructed Byte that comes before the final byte, so that effectively, counted all possible combinations, much more than just 1*2**8 values can be represented. (de:) Konvertierung von Groß- in Kleinbuchstaben: Was man schon hat wird subtrahiert, was man danach haben möchte zuvor addiert, bzw. meistens oder fast immer (bis auf Pascal bei rein positiven Zahlentypen) kann auch eine negative Konstante (ord("A")-ord("a")) einfach addiert werden, die Addition einer negativen Zahl führt zu einer Subtraktion gleichen Ausmaßes heißt: „Betrag der Zahl gleich”, man schreibt hīer: abs(-12)=abs(+12)=12 >>> s=""; # initialize with an empty character string, otherwise appending cstrs. is not allowed >>> for c in "aBcdeFFDgh": ... if c>="a" and c<="z": s+=chr(ord(c)+(ord("A")-ord("a"))); ... else: s+=c; ... >>> s 'ABCDEFFDGH' >>> def sgn(x): ... return +1 if x>0 else (-1 if x<0 else 0); ... >>> sgn(-12) -1 >>> sgn(12) 1 >>> sgn(0) 0 >>> 0/abs(0) Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero >>> def idn(x): ... return abs(x)*sgn(x); ... >>> idn(-12) -12 >>> idn(+124) 124 >>> idn(0) 0 de:auch/en:also/pt:tambêm >>> def sgn(x): ... if x<0: return -1; ... elif x>0: return +1; ... else: return 0; ... >>> for x in [12,-153,460,0]: print(sgn(x)*abs(x)); ... 12 -153 460 0 de:bzw.(beziehungsweise, von Fall zu Fall) /en:respectively (paying respect to what is then not said) /pt:respectivamente:: en: using nested ›if‹ statement:: de: selbiges(the same) mit(with) verschachteltem(en:nested) ›if‹:: [( ling-de: sächlich angenommen,: “das ›if‹”; alles: “mit dem(~3.Fall/Dativ, Artikel) – verschachtelten (zweites mal kommt in -de ‘n’ statt ‘m’, 1.mal war:dem) – Ding(sbums)”, also: mit+3.Fall+3.Fall+3.Fall, Artikel wie 'dem/das' zeigt nur Fall & =/≠1 an. ling-de: bzw.: “mit verschachtelter ›if‹-Anweisung”, Artikel(die)/Eigenschaftswort(verschachtelt): { vermöge dem/alles gleich wie das} Wort ‘die Anweisung’ female/weiblich, selbiges ohne Präposition 'mit': “die verschachtelte ›if‹-Anweisung”, dann einfach alles 1.Fall. drei Wort-Eigenschaften: Geschlecht(m/w/s) Fall(1.-4.) Zahl(=1:Einzahl, >1:Mehrzahl) ⇉Endung )] Anmerkung de: Dingsbums(colloq.) = irgend-so-ein Ding en: dingbat = I don’t know, a thing and a bat (Fledermaus,pt:morcego), symbolical character ±88 explwd - de:Anmerkung = en:remark/note // explwd ~ explication of a word wordly translated: note(…-merkung) regarding what is to say – ·at· this point in time/(and/or) text,, (de:An-, wordly en:at)) explwd - :ling. = pt/es/lat: lingua = en:language explwd - :lat. = latin language, pt: antigo imperio romano, do qual vêm as linguas pt, es, fr, ‥ >>> def sgn(x): ... if x>0: return +1; ... else: ... if x<0: return -1; ... else: return 0; ... >>> for x in [12,-153,460,0]: print(sgn(x)*abs(x)); … same result Bei der letzten Version von ‘def sgn’ kann man sehr schön sehen, wie Verschachtelungen/Nestings(en:) in Python realisiert werden: innerer Level/innerer Bereich: jeweils um zwei LeerZeichen weiter nach rechts gerückt (man kann auch nur ein oder drei und mehr Zeichen einrücken,) was denselben Level hat, gehört zusammen; der übergeordnete Block (parent block) ist weniger eingerückt At the last version of the function definition of ›sgn‹ (lat:signum = en:sign, either + or -), you can see well, how nesting/boxing of statement sequences is done: The more the inner level the more spaces lead to the left. Same number of spaces in front at consecuting lines makes up a block/sequence of statements. The so called parent block is the next block surrounding the currently attended that goes more to the left/in-a-front, that is to say, has a lesser number of spaces counted in front of it. Basically we could also have written: >>> def sgn(x): ... if x>0: return +1; ... return +1; ... else: ... if x<0: return -1; ... else: return 0; … That is to say: not only contingent lines with same space count in a front belong together, but all statements of same count-of-spaces-before, interrupted here with inner/nested blocks. Any two (consecuting) nesting levels considered in a Python program, and it values: The inner levels form different code blocks. The outer level counts as one big code block, where the inner levels are to be considered part of one of the statements of the single outer·more level. Level #0 is called ‘the main program’, zu Deutsch/de: das Hauptprogramm (Haupt~caput:lat, à tino), on Level #1, there is not just a single thing but many a different some·things. Anmerkung/Note #1: ›if‹ and ›else‹ can be combined into one line in the example above, without precisely having to declare the nesting level by leading spaces at the beginning of the line; however in the general case as-a-such as this, this is ¡not possible!. Anmerkung/Note #2: The statement is called 'print(3+3) starting from python3 on, that is the programming language Python at times of version 3. Before this for long python2 was used, version 2 of this programming language, where ànd which normally it is considered a thing totally prohibited that a newer version of the ‘language standard’ of a given programming language makes programs written before incompatible with what is demanded and used since on from Python 3. Exemplum dato (:lat) for ‘example given’: Modula-3 is a totally different programming language, when you knew it´s predecessor Modula-2. If you break things, why not rehowl everything from point zero on? rehowl: the English dictionary does not know, but used in the sense of »overhowl, but start anew from the scratch using the old thing you already have« de: überholen, erneuern, von grund auf neu machen. (pelo (des)cuido não von Grund auf, seja ou fosse em contra do sentido em o qual eu uso ·von grund auf·) see at the end of this text (file): command line example with print, python2 and python3. ** from here on text not re-read for means/purposes of correction/re-formulation ** Anmerkung/Note #3: In Mathematics(/Analysis(pt:rumo,de:Teilgebiet,en:field/subject)) you often find the following definition: sgn(x) = x/abs(x). Programming a computer you would never do this, since a 'division by zero' leads to fatal error and the program stops execution. Generally it would be possible to catch that in form of a operating system signal (man:signal, SIGFPE further information needed to be taken by the so called signal handler, if installed by the programmer, in account) Maths/Analysis say: lim{±x→0}(13/x)=±∞, limes(:lat) the threshold/asymptotical value en:threshold value ~ de:Grenzwert, you come closer and close to a given value, the zero in this case, but the limes function never in deed uses/tests the expression (13/x) with this value (0=‘zero’), you come closer and closer and see where it would end if you reached the point (0=‘zero’) that would have to be tested. So the limes is the value you wont reach, but it is the closest possible threshold value to what is demanded/pt:querido/ to what you would like to use in this place. The function in the brackets (…) can produce something invalid when fed with the approximation value/ the approximated value/ the value the limes function approximates (diz: the fn. comes close to). The running base variable (de:Laufvariable) ›x‹ produces another value (pt:a partir de) starting off with the given ·x·, the value calculated from ‘x’ is called the assymptotical value. You could also write: f(x)=13/x; lim{±x→0}f(x)=±∞ most often you use to speak about an asymptotical value (pt:a partir de) when you speak about a function f(x). The clue now about using ›limes‹ is, the value of f(x) at the approximated point may or may not exist, but the limes function always can yield a valid representational value for f(x) called asymptotical, if you are interest in what the function f(x) produces near the approximation point. The only pre·condition that needs to be fullfilled by limes to yield such a result, is that the function f(x) is continuous and not f.i.(for instance, as by example; de: zum Beispiel, z.B., bspw., beispielsweise) alternating or simply not continued in the approximation region (called) ε, where a sufficiently small value range for epsilon ‘ε’ is at-to-be selected by lim. Problematic would be for instance: f(x) = x*(x%3-1) x%3 = x-x÷3*3, take the ‘remainder’ of x divided by three 3. de: Rest von x dividiert durch drei 3; das ist entweder 0, 1 oder 2: 0%3=0, 1%3=1, 2%3=2, 3%3=0 (and one times the three but the ›%‹ operator does not count for the result of the division x÷3, where for x÷3 we count only the number of times that the value 3 can be subtracted as a whole before and including reach of zero, division measures how many times the three 3 is contained in ›x‹), so continued: 4%3=1, 5%3=2, 6%3=0, 7%3=1, 8%3=2, … 0,1,2,0,1,2,0,1,… is called the modul´ sequence of three 3, Modul(o)reihe von Drei {en:say/call;de:}sprich: “5 modulo 3“, also zwei 2, geschrieben hīer als 5%3 command line sample given in addition to Anmerkung/remark #2: > python2 Python 2.7.18 (default, Oct 9 2022, 06:38:55) [GCC 10.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print 2+3; 5 >>> import sys; >>> print >>sys.stderr, 2+3; 5 >>> print(2+3); 5 >>> print(2+3,sys.stderr); (5, ', mode 'w' at 0x7fd26fe4b270>) >>> print(2+3,file=sys.stderr) File "", line 1 print(2+3,file=sys.stderr) ^ SyntaxError: invalid syntax > python3 Python 3.8.14 (default, Oct 4 2022, 06:27:18) [GCC 10.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print 2+3; File "", line 1 print 2+3; ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(2+3)? >>> print(2+3); 5 >>> import sys; >>> print(2+3,file=sys.stderr) 5 command line sample for Anmerkung/remark 3: >>> 5//3 + 5%3 3 >>> 5//3 1 >>> 5/3 1.6666666666666667 >>> 5%3 2 >>> (5%3)/3 0.6666666666666666 >>> >>> -5%3 1 >>> 5%-3 -1 >>> [ 5//-3 + 5%-3, -5//3 + -5%3, -5//-3 + -5%-3 ] [-3, -1, -1] — um inteiro falso, quem faz o estándard da lingua Python merece um vomitōō‼ se deve haver um [5, 5, 5] em o aquı̊. ‥ and here what you would call a nested sequence/list, {in general:} nested data structure, is-called what you find here, that is to say a list within a list, and not just one, three nested lists within the main list enclosing the nested ones: >>> [ [ 5//-3 , 5%-3 ], [ -5//3, -5%3 ], [ -5//-3, -5%-3 ] ] [[-2, -1], [-2, 1], [1, -2]] a mathematician would demand the following equation to be true in all cases (with positive as well as (/or simply: and) negative (à comput:)input values / (à math.:)preimage values: a//b + a%b = a ∀a∈ℤ (λ1.1) ∀ ... for all of, here: for all {of the|arbitrarily chosen values of} variable ›a‹ ℤ ... the whole numbers; …, -3, -2, -1, 0, +1, +2, +3, +4, … a∈ℤ ,,, for variable a, a value from-out-of the given set ℤ needs to be selected, that is, speak: a element ℤ, (or more verbosly:)a is an element of ℤ you could write just ‘…∀a’ (or usually/most often: ‘∀a:…’) … if the base set (de:Grundmenge) is assumed to be known from the context or valid at least for any kind of real number, imaginAry numbers not counted here. one possible solution to redefine both operators consistently, with regards to int(x); a//b ≔ abs(a)//abs(b)*sgn(a) a%b ≔ abs(a)%abs(b)*sgn(a*b) where is known: sgn(a*b) = sgn(a)*sgn(b) (you could add: ∀a,b, if you like, but generally variables not said/remarked to be constant are to be/can be freely chosen values for behaviour of the two definitions in above: the result of the division is always closest to number/point zero 0, the remainder being chosen accordingly results in the same: a//b ≔ int(a/b); a%3 ≔ a/b-int(a/b) other behaviours that would make/ result in a contradiction-less definition, being provided (de:vorausgesetzt, daß) the equationa λ1.1 is/maintains to be fulfulled. λ ... greek letter lambda, in physiscs also used for the wave length, the length measured from where a wave (pt:onda, de:Welle) is supposed to start until it returns to the more or less arbitrarily selected starting point, f = 1/λ (that is: 1m/(λ*1s)) ... frequency, measured in s⁻¹, math: 1/x = x⁻¹ ∀x You take one meter of length, the standard unit of measure in physics, see/measure how often it would go up and down until it reaches the point of start, and that is assumed that the wave is unrolled apart from the given ›movement about a mid point‹ by a velocity of 1m/s „one meter per second“, assumed velocity the wave goes forward. movement about a mid/center point (assumed to exist) ≝ oscillation eine Schwingung ist eine Bewegung um einen angenommenen Mittelpunkt :: Frequency measures the ‘speed’ or number of returning points within a second/ within a given time of measurement, for not to use µs⁻¹ (swingings per micro-second) -> de: aber eben nicht die Ausbreitungsgeschwindigkeit der Welle(!) speed of forward-penetration of the wave. Water waves, you will know the sand and the beach, there is a forward penetration speed. You could have a photo from the waves, a meter pile in the photo, photo taken from a perpendicular (only top-bottom/bottom-top direction) viewing angle, so that there is no optical distortion as would be caused by a slanting or inclined viewing angle, and measure the number of full wave length divided by the pathway of these (which streight -forward is). The speed of 1m/s assumed in the example from above would be very fast, every second it goes one whole meter, and in a minute 60m. Second point of measure is ram a pile into the beach, and you would need to measure seconds (I don´t know how to go for a fraction of a second here) until and in-between two wave-spikes occur. t = 3.5s ... time between two spikes s = v·t, space is velocity multiplied by the time you (drive a car or) go forward v = s/t; s ... we know it from the photo, we used for the first measurement example v = 2m/3.5s; v=⅐m/s “one seventh of a meter per second“ The oscillation frequency is measured apart from 1m/s that is a seven 7 times faster standard forward penetration speed. — not·yet·published, coming as soon as wíll be done, _℠. ~~~ some more notes on how to use python as a calculator/ how to use std. maths: # greek letter pi π, length of one time around a circle 2·r·π, diameter d=2·r radius # plane of a circle A = r²π = r·r·π, area is as of a quadrat □, # but multiplied with the magic number pi π. >>> import math; math.sqrt(16); # square root, 4=√16`, 4=√4*4`, cubic root: 2=∛8` 4.0 >>> 16**0.5; # quadratic root, who knows x√b**x` = b**x**(1/x) = x iff x is odd or b positive 4.0 >>> print(64**(1/3),(4*4*4)**(1/3)); 3.9999999999999996 3.9999999999999996 >>> round(64**(1/3)) 4 >>> import math; [ math.pi, round(math.pi,3), round(1.532178), round(-1.532178) ] [3.141592653589793, 3.142, 2, -2] >>> math.log(100,10); # the logarithm at the base of 10, 10ˣ=100 ⇒ x=2 2.0 >>> math.log(math.e**34); # exponential rows for ‘continuous/natural proliferation’ are done by the base of e 34.0