<jupyter_start><jupyter_text>3. Data Types
> Just a quick introduction to some basic data types in Python.

- toc: true
- badges: true
- comments: true
- categories: [python]
- author: <NAME>
- sticky_rank: 3  The Python Conceptual Hierarchy

Before we get to the code, let’s first establish a clear picture of how this chapter fits into the overall Python picture. From a more concrete perspective, Python programs can be decomposed into **modules**, **statements**, **expressions**, and **objects**, as follows:

1. Programs are composed of modules.
2. Modules contain statements.
3. Statements contain expressions.
4. Expressions create and process objects

![](nb_images/types.png)


 Numbers:

**Intergers**: no fractional part

**Floating-point numbers**: with fractional part.<jupyter_code>3.14e-2 # Scientific notation
123 + 222 # Integer addition
1.5 * 4 # Floating-point multiplication
2 ** 100 # 2 to the power 100
len(str(2 ** 1000000)) # How many digits in a really BIG number?
import math
print(math.pi)
print(math.sqrt(85))<jupyter_output><empty_output><jupyter_text>The math module contains more advanced numeric tools as functions, while the random module performs random-number generation and random selections (here, from a Python list coded in square brackets — an ordered collection of other objects to be introduced later):<jupyter_code>import random
print(random.random())
random.choice([1, 2, 3, 4])<jupyter_output><empty_output><jupyter_text>String:`"``'`**Question**: What if there's quotation mark in the string?<jupyter_code>print('I\'m \"ok\".')  # Use escape character \
print('I\'m learning\nPython.')
print('\\\n\\')<jupyter_output><empty_output><jupyter_text>We could use r'' to avoid escape character:<jupyter_code>print('\\\t\\')
print(r'\\\t\\')<jupyter_output><empty_output><jupyter_text>Instead of \n, we could use '''...''' to display multiple lines.<jupyter_code>print('''line1
... line2
... line3''')<jupyter_output><empty_output><jupyter_text>Booleaneither `True` or `False`.<jupyter_code>True
False
3 > 2
3 > 5<jupyter_output><empty_output><jupyter_text>Logical operators: and, or, not<jupyter_code>True and True
True and False
False and False
5 > 3 and 3 > 1
True or True
True or False
False or False
5 > 3 or 1 > 3
not True
not False
not 1 > 2<jupyter_output><empty_output><jupyter_text>We often use Boolean type in conditional statements:<jupyter_code>if age >= 21:
    print('Yes, you can.')
else:
    print('Sorry.')<jupyter_output><empty_output><jupyter_text>Precedence matters**General rule: arithmetic > comparison > `not` > `and`/`or`**Much more detailed operator precedence: https://docs.python.org/3/reference/expressions.htmloperator-precedence<jupyter_code># example:
5 * 7 >= 3 + 5 * (7 - 1) and not False
35 >= 3 + 5 * 6 and not False
35 >= 3 + 30 and not False
35 >= 33 and not False
True and not False
True and True
True<jupyter_output><empty_output><jupyter_text>Nonetype`None`The sole value of the type **NoneType**. `None` is frequently used to represent the absence of a value, as when default arguments are not passed to a function.  ***Exercises***1\. Below is a transcript of a session with the Python shell. For each expression being evaluated, provide the type and the value the expression returns. I encourage you to answer them **directly** since this will help reinforce your understanding of basic Python expressions.<jupyter_code># 1.1
a = 3
a + 2
# 1.2
a = a + 1.0
a
# 1.3
a = 3
b
# 1.4
a = 3
a == 5.0
a 
# 1.5
b = 10
c = b > 9
c 
# 1.6
5/2 == 5/2.0<jupyter_output><empty_output><jupyter_text>2\. For each of the following expressions, indicate the value returned. I encourage you to answer them **directly** since this will help reinforce your understanding of basic Python expressions.<jupyter_code>3.0 - 1.0 != 5.0 - 3.0
3 > 4 or (2 < 3 and 9 > 10)
4 > 5 or 3 < 4 and 9 > 8
not(4 > 3 and 100 > 6)<jupyter_output><empty_output><jupyter_text>3\. The time module provides a function, also named time, that returns the current Greenwich Mean Time in “the epoch”, which is an arbitrary time used as a reference point. On UNIX systems, the epoch is 1 January 1970.<jupyter_code>import time

print(time.time())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11433286185761433, 'fraction_numerical': 0.033946251768033946, 'mean_word_length': 4.695302013422819, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 73, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '16582', 'license': 'Apache-2.0', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1357, 'n_words': 511, 'path': '_notebooks/2021-09-20-03-types.ipynb', 'repo_name': 'OIM3640/blog', 'size': '4242'}