Python does arithmetic.
Python 3.8.5 (default, Sep 4 2020, 02:22:02)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> 2 - 2
0
>>> 2*2
4
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3
>>> 10 % 3
1
>>> 2**5
32
Python objects know their types. So far, we have seen the integer type.
>>> type(2)
<class 'int'>
Python integers can be as big as you'd like.
>>> 2**1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361
22493198378815695858127594672917553146825187145285692314043598457757469857480393
45677748242309854210746050623711418779541821530464749835819412673987675591655439
46077062914571196477686542167660429831652624386837205668069376
I wonder what this is.....
>>> 3^4
7
We saw another type appear when we did division. Floats are IEEE double precision floating point numbers
>>> type(3.3333333333333335)
<class 'float'>
Ooh, ugly surprise!
>>> .1 + .1 + .1 == .3
False
Floating point numbers are approximations. Never check for equality! Check for "close enough."
>>> .1 +.1 + .1
0.30000000000000004
As expected.
>>> 4e5
400000.0
>>> 4E5
400000.0
Do arithmetic with an integer and a float and you will get a float.
>>> 4 + .5
4.5
>>> int(4.5)
4
>>> int(-4.5)
-4
Just casting about... A cast is a temporary request to regard an object of one type as being of another. The object is unchanged by this operation.
Casting float to int results in truncation of decimal part.
This is no surprise.
>>> float(4)
4.0
Python has complex numbers and complex arithmetic, too.
>>> z = complex(3,4)
>>> z
(3+4j)
>>> z**2
(-7+24j)
Rules for variable names Variable names must start wiht alpha character or _ and succeeding characters can be alphanumeric or _.
Python uses snake notation for variable names.
>>> number_of_snakes = 42
Deception What we get is is not the type of a variable. We are getting the type of object the variable points at.
>>> type(number_of_snakes)
<class 'int'>
>>> number_of_snakes = "caterwaul"
>>> type(number_of_snakes)
<class 'str'>
Here is < at work. It is a relational operator.
>>> 4 < 5 True >>> 5 < 4 False
We have met another type, the boolean type.
>>> type(True)
<class 'bool'>
This is the isequalto operator. It is distinct from =, which is assignment.
>>> 4 == 5
False
Note the asymmetry of the assignment operator.
>>> 5 = x
File "<stdin>", line 1
SyntaxError: cannot assign to literal
More relational operators...
>>> 4 <= 5
True
>>> 4 >= 5
False
>>> 4 != 5
True
The not
operator negates the truth-value of
a predicate.
>>> not(4 == 5)
True
This is or
>>> True or False
True
>>> True or True
True
>>> False or True
True
>>> False or False
False
This is and
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False
What is the order of operations for the boolean operations?
Now let us play with strings. This object holds a glob of text. Here we index into a string. NB: Indices live BETWEEN the string's characters.
>>> x = "abcdefghijklmnopqrstuvwxyz"
>>>
>>> x[1]
'b'
>>> x[0]
'a'
There is no separate character type in Python.
>>> type(x[1])
<class 'str'>
Here is string containing all characters of x
between indices 1 and 4.
>>> x[1:4]
'bcd'
Hey, cool
>>> x[1:4] + x[4:6]
'bcdef'
Type determines context: + adds numbers and it concatenate strings. Let's mix it up.
>>> "cows" + 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
Punishment is meted out. Python is defending the integrity of its type system. You can do this.
>>> a = "cows"
>>> b = 42
>>> a + str(42)
'cows42'
Format strings rock.
>>> f"{a}{b}"
'cows42'
Check these out.
>>> x[1:6]
'bcdef'
>>> x[4:]
'efghijklmnopqrstuvwxyz'
>>> x[:4]
'abcd'
>>> x[::2]
'acegikmoqsuwy'
>>> x[::3]
'adgjmpsvy'
>>> x[1::3]
'behknqtwz'
>>> x[:13:3]
'adgjm'
You can get an upper-cased version of a string. The original string is untouched.
>>> x.upper()
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> x
'abcdefghijklmnopqrstuvwxyz'
If we want the result, we save it to a variable.
>>> y = x.upper()
>>> x
'abcdefghijklmnopqrstuvwxyz'
>>> y
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Now lower yourself.
>>> y.lower()
'abcdefghijklmnopqrstuvwxyz'
Case changes are ignored by non-alpha characters.
>>> "#$%$EWDFSAvdrjgi[jer0fdsgerv23454332423".lower()
'#$%$ewdfsavdrjgi[jer0fdsgerv23454332423'
Somebody dared me to uppercase the first half of x
.
>>> x
'abcdefghijklmnopqrstuvwxyz'
>>> q = x[0:x.find("m")] + x[x.find("m")]]
File "<stdin>", line 1
q = x[0:x.find("m")] + x[x.find("m")]]
^
SyntaxError: unmatched ']'
>>> q = x[0:x.find("m")] + x[x.find("m")]
>>> q
'abcdefghijklm'
>>> q = x[0:x.find("m")] + x[x.find("m":)]
File "<stdin>", line 1
q = x[0:x.find("m")] + x[x.find("m":)]
^
SyntaxError: invalid syntax
>>> q = x[0:x.find("m")] + x[x.find("m"):]
>>> q
'abcdefghijklmnopqrstuvwxyz'
>>> q = x[0:x.find("m")].upper() + x[x.find("m"):]
>>> q
'ABCDEFGHIJKLmnopqrstuvwxyz'
>>>
after some fumbling we made it work.