Data Types Continued

Today we start off by continuing to learn about Data Types!

Most of the rest of this actually isn’t helpful, but I got to play with some built-in functions like abs() and divmod() which was kinda fun.

Variables in Python

Variable Assignment

Python uses the same basic assignment operations that most programming languages.

1
2
3
n = 300
print(n)
# 300

Types are fluid though, so you can re-assign n to n = "hello" and life goes on.

Chained Assignment also works.

1
2
3
n = m = x = y = 400
print(n,m,x,y)
# 400 400 400 400

Multiple Assignment

1
2
3
a, b = 300, 400
print(a,b)
# 300 400

The variable reference, e.g. n, can change based on the object it is referencing.

1
2
3
4
5
6
n = 300
type(n)
# int
n = "hello"
type(n)
# str

Object References

!! Everything is an Object !!

All of it. Every bit.

A variable points to an object, if nothing points to an object it is considered “orphaned”, and the Garbage Collector will eventually free the memory when it figures it out.

Object Identity

Integer objects have some caching, but seems like there’s even more caching that isn’t documented going on.

The numbers -5 - 256 are cached; however, the Python version on repl is also caching large integers, as can be witnessed with the id() function.

Variable Names

Constants should be uppercase. You can use underscores (snake_case) to breakup words in a variable name. You cannot star the name of your variable with a digit, you’ll get an “invalid token” SyntaxError. You can also use unicode characters in names for Python 3.

PEP 8 is the style guide for the language.

Reserved Keywords

These are words you just can’t use: help("keywords")

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
> help("keywords")

Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not

help lets you look at other stuff too:

1
2
3
4
5
> help("False")
Help on bool object:

class bool(int)
 |  bool(x) -> bool

Of course, you can programmatically access the keywords list:

1
2
3
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Operators and Expressions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# and & or work on numbers in wild ways
a = 100
b = 200
print("bool(a):", bool(a))
print("a and b:", a and b)
print("a or b:", a or b)
print("b and a:", b and a)
print("b or a:", b or a)

a = 0
print("bool(a):", bool(a))
print("a and b:", a and b)
print("a or b:", a or b)
print("b and a:", b and a)
print("b or a:", b or a)

a = -1
print("bool(a):", bool(a))
print("a and b:", a and b)
print("a or b:", a or b)
print("b and a:", b and a)
print("b or a:", b or a)

Strings and Character Data

In Python, you can multiply a String with an Integer to get it to return the String multiple times.

Python also supports membership (e.g. contains) with the in operator:

1
2
>>> "spam" in "i love the taste of spam"
True

Negative indexes of strings are limited by their min-max - i.e. you cannot wrap around a string more than once.

Repl of the Day