Basic Python Datatypes¶
This notebook explores the basic python datatypes.
Some examples come from the python tutorial: http://docs.python.org/3/tutorial/
For IPython help, visit: http://ipython.org/ipython-doc/dev/interactive/notebook.html
Some useful short-cuts:
- shift+enter = run cell and jump to the next (creating a new cell if there is no other new one)
- ctrl+enter = run cell-in place
- alt+enter = run cell and insert a new one below
- ctrl+m h lists other commands
A "markdown cell" enables you to typeset LaTeX equations right in your notebook. Just put them in $ or $$:
$$\frac{\partial \rho}{\partial t} + \nabla \cdot (\rho U) = 0$$Integers¶
type(2)
type(2.)
2 + 2
4
Note: Integer division is one place where python 2 and python 3 differ.
In python 3.x, dividing 2 integers results in a float. In python 2.x, dividing 2 integers results in an integer. The latter is "correct" in the computational sense, since the data-type of the result is the same as the inputs, but the former is more in-line with our expectations.
1 / 2
To get an integer result, we can use the // operator, with rounding toward negative infinity.
1 // 2
-1 // 2
-1
Python is a dynamically typed language--this means that we do not need to declare the datatype of a variable before initializing it.
a = 1
b = 2
a + b
3
a * b
Variable names are case sensitive, so a and A are different
A = 2048
print(a, A)
1 2048
Python has some built-in help (and IPython has even more).
help(a)
a?
type(a)
Note in languages like Fortran and C, you specify the amount of memory an integer can take (usually 2 or 4 bytes). This puts a restriction on the largest size integer that can be represented. Python will adapt the size of the integer so you don't overflow.
a = 1234567890123456789012345678901234512345678901234567890123456789068778878887
print(a)
print(a.bit_length())
print(type(a))
1234567890123456789012345678901234512345678901234567890123456789068778878887
250
<class 'int'>
Floating Point and Complex Numbers¶
type(1.0)
When operating with both floating point and integers, the result is promoted to a float. This is true of both python 2.x and 3.x.
1. / 2
0.5
However, division of two integers (e.g. 1/2) gives different results in Python 2.x and 3.x.
To be safe,
- for floating-point division, explicitly convert one operand to float (as above), and
- for integer division, use '//'.
1. // 2
0.0
-1. // 2
-1.0
There are infinitely many real numbers between any two bounds, on a computer we have to approximate this by a finite number. This incurs rounding errors.
There is an IEEE standard for floating point that pretty much all languages and processors follow. Almost all platforms map Python floats to IEEE-754 double precision floating-point numbers.
We can ask python to report the limits on floating point.
import sys
print(sys.float_info)
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
We can format the output of floats; see https://docs.python.org/3/library/string.html#formatstrings.
a = 1.0
eps = 1.e-16
print("{:30.20} {:30.20}".format(a, eps))
b = a + eps
print("{:30.20}".format(b))
print(b == a)
1.0 9.999999999999999791e-17
1.0
True
The math module provides functions that do the basic mathematical operations as well as provide constants (note there is a separate cmath module for complex numbers).
In python, you import a module. The functions are then defined in a separate namespace -- this is a separate region that defines names and variables, etc. A variable in one namespace can have the same name as a variable in a different namespace, and they don't clash. You use the "." operator to access a member of a namespace.
By default, when you type stuff into the python interpreter, in IPython, or in a script, it is in its own default namespace, and you don't need to prefix any of the variables with a namespace indicator.
import math
print(math.pi)
3.141592653589793
pi = 3
print(pi, math.pi)
3 3.141592653589793
Floating Point Operations¶
R = 2.0
print(math.pi * R ** 2)
12.566370614359172
Operator precedence follows that of most languages. See
https://docs.python.org/3/reference/expressions.html#operator-precedence
in order of precedence:
- expressions in ()
- slicing, calls, subscripts
- power (**)
- +x, -x, ~x
- *, /, //, % (modulos)
- +, -
(after this are bitwise operations and comparisons).
Best practice: When in doubt, use parentheses.
a = 1 + 3 * 2 ** 2
b = 1 + (3 * 2) ** 2
print(a, b)
13 37
Assignment statements evaluate from right to left.
x = y = z = 1
print(x, y, z)
1 1 1
print(math.cos(math.radians(45)))
0.7071067811865476
help(math)
Python uses 'j' to denote the imaginary unit.
print(1.0+2j)
(1+2j)
a = 1j
b = 3.0+2.0j
print(a + b)
print(a * b)
(3+3j)
(-2+3j)
We can use abs() to get the magnitude and separately get the real or imaginary parts.
print(abs(b))
print(a.real)
print(a.imag)
Booleans¶
Booleans in Python are analogous to logicals in MATLAB.
type(False)
2>1
Boolean operators use explicit words, including and, or, and not. (Precedence: not > and > or.)
x = 1
y = 2
print(x > 0 and y == 0)
print(x > 0 or y != 0)
print(not y==0)
False
True
True
Both and and or are short circuited, i.e., if the first part is False or True respectively, the second operand will not be evaluated.
x = 1
print(x == 0 and something_nonsense)
print(x > 0 or something_nonsense)
The is operator compares the identity of two operands. It is not equivalent to the comparison operator ==.
print(2.0 is 2)
print(2.0 == 2)
False
True
<>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
<>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
C:\Users\WIN10HX64\AppData\Local\Temp\ipykernel_17044\2265625049.py:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
print(2.0 is 2)
The is operator can be used to check data types.
b = 1 > 0
print(type(b) is bool)
True
It can also be used in conjunction with not.
print(type(1) is not int)
print(not type(1) is int)
False
False
Booleans are especially useful for conditionals and loops, so we defer the discussions for later.
Strings¶
You can use single or double quotes for strings.
a = 'this is my " string'
b = "another \" string"
c = "abc\ndef"
print(a)
print(b)
print(c)
this is my " string
another " string
abc
def
type(a)
str
print(a + b)
this is my " stringanother " string
print(a + ". " + b)
this is my " string. another " string
print(a * 3)
this is my " stringthis is my " stringthis is my " string
You can use escape characters (e.g. \n for newline). See https://docs.python.org/2.1/ref/strings.html
a = a + "\n"
print(a)
this is my " string
\n
""" can enclose multiline strings. This is useful for docstrings at the start of functions (more on that later...)
c = """
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum."""
print(c)
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
A raw string does not replace escape sequences (like \n).
d = r"this is a raw string\n"
print(d)
this is a raw string\n
Python (like C) uses 0-based indexing.
Negative indexes start from -1 and count from the right.
Slicing a string with start:stop will end at stop-1. The same convention holds for arrays.
print(a)
print(a[0])
this is my " string
t
a = "abc"
print(a[-3])
a
a = 'this is my " string'
print('"' + a[0:4] + '"')
"this"
print(a[0:-2])
this is my " stri
print(a[1:])
his is my " string
Equality of strings can be compared using is, ==, >=, etc.
x = 'abc'
print(x is 'abc')
print(x == 'abc')
print('def' >= x)
True
True
True
<>:2: SyntaxWarning: "is" with a literal. Did you mean "=="?
<>:2: SyntaxWarning: "is" with a literal. Did you mean "=="?
C:\Users\WIN10HX64\AppData\Local\Temp\ipykernel_17044\818308584.py:2: SyntaxWarning: "is" with a literal. Did you mean "=="?
print(x is 'abc')
There are also a number of methods and functions that work with strings (and a string module).
Note that in python, strings are immutable. Operations on strings return a new string.
a = 'this is my " string'
print(a.replace("this", "that"))
print(a)
that is my " string
this is my " string
print(len(a))
19
a = ' this is my " string\n'
print('"' + a.strip() + '"') # hey! this is a comment! Also notice that strip removes the \n
"this is my " string"
print(a.strip()[-1])
g
The following statement would result in an error:
a = 'T' + a[1:]
print(a)
This is my " string
help(str)
'Python' 카테고리의 다른 글
| 6. Recursion vs. Iteration (1) | 2024.03.14 |
|---|---|
| 5. Function (0) | 2024.03.14 |
| 4. Conditionals and Loops (0) | 2024.03.14 |
| 3. Exception Handling (0) | 2024.03.14 |
| 2. List, Set, Tuple (1) | 2024.03.14 |
