1. Data Types

2024. 3. 14. 01:06·Python

 

 

 
 

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¶

 
In [ ]:
type(2)
 
In [ ]:
type(2.)
 
In [1]:
2 + 2
 
Out[1]:
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.

 
In [ ]:
1 / 2
 
 

To get an integer result, we can use the // operator, with rounding toward negative infinity.

 
In [ ]:
1 // 2
 
In [2]:
-1 // 2
 
Out[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.

 
In [2]:
a = 1
 
In [3]:
b = 2
 
In [6]:
a + b
 
Out[6]:
3
 
In [ ]:
a * b
 
 

Variable names are case sensitive, so a and A are different

 
In [4]:
A = 2048
 
In [5]:
print(a, A)
 
 
1 2048
 
 

Python has some built-in help (and IPython has even more).

 
In [ ]:
help(a)
 
In [ ]:
a?
 
In [ ]:
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.

 
In [6]:
a = 1234567890123456789012345678901234512345678901234567890123456789068778878887
print(a)
print(a.bit_length())
print(type(a))
 
 
1234567890123456789012345678901234512345678901234567890123456789068778878887
250
<class 'int'>
 
 

Floating Point and Complex Numbers¶

 
In [ ]:
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.

 
In [7]:
1. / 2
 
Out[7]:
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 '//'.
 
In [8]:
1. // 2
 
Out[8]:
0.0
 
In [9]:
-1. // 2
 
Out[9]:
-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.

 
In [10]:
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.

 
In [12]:
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.

 
In [18]:
import math
 
In [19]:
print(math.pi)
 
 
3.141592653589793
 
In [20]:
pi = 3
 
In [21]:
print(pi, math.pi)
 
 
3 3.141592653589793
 
 

Floating Point Operations¶

 
In [22]:
R = 2.0
 
In [23]:
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.

 
In [24]:
a = 1 + 3 * 2 ** 2
b = 1 + (3 * 2) ** 2
print(a, b)
 
 
13 37
 
 

Assignment statements evaluate from right to left.

 
In [25]:
x = y = z = 1
print(x, y, z)
 
 
1 1 1
 
In [26]:
print(math.cos(math.radians(45)))
 
 
0.7071067811865476
 
In [ ]:
help(math)
 
 

Python uses 'j' to denote the imaginary unit.

 
In [27]:
print(1.0+2j)
 
 
(1+2j)
 
In [28]:
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.

 
In [ ]:
print(abs(b))
print(a.real)
print(a.imag)
 
 

Booleans¶

 
 

Booleans in Python are analogous to logicals in MATLAB.

 
In [ ]:
type(False)
 
In [ ]:
2>1
 
 

Boolean operators use explicit words, including and, or, and not. (Precedence: not > and > or.)

 
In [29]:
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.

 
In [ ]:
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 ==.

 
In [30]:
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.

 
In [31]:
b = 1 > 0
print(type(b) is bool)
 
 
True
 
 

It can also be used in conjunction with not.

 
In [32]:
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.

 
In [33]:
a = 'this is my " string'
b = "another \" string"
c = "abc\ndef"
 
In [34]:
print(a)
print(b)
print(c)
 
 
this is my " string
another " string
abc
def
 
In [4]:
type(a)
 
Out[4]:
str
 
In [17]:
print(a + b)
 
 
this is my " stringanother " string
 
In [35]:
print(a + ". " + b)
 
 
this is my " string. another " string
 
In [20]:
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

 
In [22]:
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...)

 
In [36]:
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."""
 
In [37]:
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).

 
In [38]:
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.

 
In [39]:
print(a)
print(a[0])
 
 
this is my " string
t
 
In [40]:
a = "abc"
print(a[-3])
 
 
a
 
In [41]:
a = 'this is my " string'
print('"' + a[0:4] + '"')
 
 
"this"
 
In [42]:
print(a[0:-2])
 
 
this is my " stri
 
In [32]:
print(a[1:])
 
 
his is my " string
 
 

Equality of strings can be compared using is, ==, >=, etc.

 
In [43]:
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.

 
In [44]:
a = 'this is my " string'
print(a.replace("this", "that"))
print(a)
 
 
that is my " string
this is my " string
 
In [45]:
print(len(a))
 
 
19
 
In [46]:
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"
 
In [42]:
print(a.strip()[-1])
 
 
g
 
 

The following statement would result in an error:

 
In [36]:
a = 'T' + a[1:]
print(a)
 
 
This is my " string
 
In [ ]:
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
'Python' 카테고리의 다른 글
  • 5. Function
  • 4. Conditionals and Loops
  • 3. Exception Handling
  • 2. List, Set, Tuple
Juson
Juson
  • Juson
    Juson의 데이터 공부
    Juson
  • 전체
    오늘
    어제
    • 분류 전체보기 (95)
      • RAG (2)
      • AI (2)
        • NLP (0)
        • Generative Model (0)
        • Deep Reinforcement Learning (2)
        • LLM (0)
      • Logistic Optimization (0)
      • Machine Learning (37)
        • Linear Regression (2)
        • Logistic Regression (2)
        • Decision Tree (5)
        • Naive Bayes (1)
        • KNN (2)
        • SVM (2)
        • Clustering (4)
        • Dimension Reduction (3)
        • Boosting (6)
        • Abnomaly Detection (2)
        • Recommendation (4)
        • Embedding & NLP (4)
      • Reinforcement Learning (5)
      • Deep Learning (10)
        • Deep learning Bacis Mathema.. (10)
      • Optimization (2)
        • OR Optimization (0)
        • Convex Optimization (0)
        • Integer Optimization (0)
      • SNA 분석 (0)
      • 포트폴리오 최적화 공부 (0)
        • 최적화 기법 (0)
        • 금융 베이스 (0)
      • Finanancial engineering (0)
      • 프로그래머스 데브코스(Boot camp) (15)
        • SQL (9)
        • Python (5)
        • Machine Learning (1)
      • Python (22)
      • Project (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Juson
1. Data Types
상단으로

티스토리툴바