<jupyter_start><jupyter_text>Learn Python Variables and Data Types **April 2021**Instruction, media content, examples and links to resources that will help you build a foundation for Python competency. Jupyter Notebooks are available on [Google Colab](https://colab.research.google.com/drive/1tdQkjNdHGJ7wTMvO_bhp3aM5k3ZR3eEx?usp=sharing) and [Github](https://github.com/AlphaWaveData/Jupyter-Notebooks/blob/master/Learn%20Python%20Variables%20and%20Data%20Types.ipynb).  SINGLE DATA TYPES  Prerequisite knowledge - variable assignment using a single equals `=`- variables must use letters first and not numbers e.g. `var_1` and not `1_var`- there are reserved words that cannot be used such as `print`, `str`, `int`....Web Resources Docs.python.org - Numbers Data Types Docs.python.org - Strings Data Types Docs.python.org - Lists Data Types Docs.python.org - Dictionary Data Types STRINGS What is a String?A string is a data type that is used to represent text rather than numbers and is composed of a set of characters and can also contain spaces and numbers. Why do we use Strings?Strings are useful when storing small or large bodies from single words to full sentences.  How do we use Strings?Strings are assigned to variables using a single equals and are surrounded by triple quotes `""" """`, double quotes `" "` or single quotes `' '`, although triple quotes are generally used for function doc strings so please use the single or double quotes.  Creating a String<jupyter_code>string_1 = """This is a string"""    # triple quote
string_2 = "this is another string"  # double quote
string_3 = 'this is also a string'   # single quote

print(string_3)
type(string_1)<jupyter_output>this is also a string<jupyter_text>Indexing a String It is possible to index a string by referencing the position (or index) of a single character, or by referencing a range with a start and end position. Note: indexes start at where the first element is 0.<jupyter_code>print(string_1)
print(string_1[0])
print(string_1[5:-3])<jupyter_output>This is a string
T
is a str<jupyter_text>INTEGERS or INTs What is an Integer?An integer is a whole number (not a fraction) that can be positive, negative or zero. Why do we use Integers?Integers are a commonly used data type in programming as they are efficient to store and are generally used in loops and indexing strings or lists.  How do we use Integers?Integers are assigned to variables using a single equals and are standalone numbers without a decimal point or an operation that does not return a whole number.<jupyter_code>int_1 = 1
int_2 = -2
int_3 = 10 / 2

print(int_1, int_2)
type(int_1)<jupyter_output>1 -2<jupyter_text>FLOATING-POINT NUMBERS or FLOATS What is a Float?A float is a floating-point number that contains a decimal point. Compared to Integers, they require more space in memory as you will have two integers separated by a decimal point. Why do we use Floats?Computers recognize real numbers that contain fractions as floating point numbers. Floats are useful for numbers that require a great deal of precision, such as calculations in engineering or microchip manufacturing. How do we use Floats?Floats are assigned to variables using a single equals and are standalone numbers that contain a decimal point or the result of an operation that returns a decimal number.<jupyter_code>float_1 = 1.0
float_2 = 3 / 1
print(float_1, float_2)
type(float_1)<jupyter_output>1.0 3.0<jupyter_text>BASIC DATA STRUCTURES Now that we have covered single variables being strings, ints and floats, there is another class of variables which store one or more single variables.These are Lists, Dictionaries and Tuples. More information can be found in the python documentation: https://docs.python.org/3/tutorial/datastructures.html LISTS What is a List?Lists are data structures that contain an ordered collection of elements. These elements are generally of the same type and can contain elements such as single data types through to more complex data structures such as other lists or dictionaries. Why do we use Lists?Lists are commonly used in programming to store and organize data so it can be easily sorted, indexed or searched. How do we use Lists?Lists are assigned to variables using a single equals, are denoted by square brackets, and use a comma to separate elements `[a,b]`. Creating a List There are two main ways of creating a list. First is to use square brackets,`a=[]` and the second is to use the list type cast `b=list()`.<jupyter_code>list_1 = ['string item',float_1,100]
list_a = list()
print(list_1)
type(list_1)<jupyter_output>['string item', 1.0, 100]<jupyter_text>Adding an item to a List Now that we have our list, you can add another element using the python's inbuilt `.append()` method.<jupyter_code>list_1.append('new element')
print(list_1)
type(list_1)<jupyter_output>['string item', 1.0, 100, 'new element']<jupyter_text>Changing an item in a List When changing an item in a list, you need to know the position of the item denoted by the index.<jupyter_code>list_1[1]='Changed Item'
print(list_1)<jupyter_output>['string item', 'Changed Item', 100, 'new element']<jupyter_text>Removing an item from a List To remove an item from a list, use the `.pop()` method.<jupyter_code>list_1.pop(0)
print(list_1)<jupyter_output>['Changed Item', 100, 'new element']<jupyter_text>Merging two Lists There are two ways you can merge two lists. First is to append using `.append(element)` which will add the new elements to the end of the old list.<jupyter_code>list_2 = ['this', 'is', 'a', 'list']
list_1.append(list_2)
print(list_1)<jupyter_output>['Changed Item', 100, 'new element', ['this', 'is', 'a', 'list']]<jupyter_text>The other way is to insert a list as an element within the list (note: not append but inject) using the `.insert(index, element)` method.<jupyter_code>list_3 = [1,2,3,4]
list_4 = [5,6,7,8]
list_3.insert(2,list_4)
print(list_3)<jupyter_output>[1, 2, [5, 6, 7, 8], 3, 4]<jupyter_text>Accessing items in a List You can access list items by referring to the index number of the item. Note: Lists start with an index of 0. You can also use negative indexing to access items from back to front. E.g. -1 is the last item in the list and -2 is second last.<jupyter_code>print(list_1[0])
print(list_1[-1])<jupyter_output>Changed Item
['this', 'is', 'a', 'list']<jupyter_text>You can also reference index ranges by using the colon between indexes which refer to the start and end of the range e.g. `[start:end]` or if you omit a number on either start (or end), it will include everything from the start (or end) up until the end (or start) e.g. `[:end]` will include the first item until the end index item.<jupyter_code>print(list_1[0:3])
print(list_1[2:])<jupyter_output>['Changed Item', 100, 'new element']
['new element', ['this', 'is', 'a', 'list']]<jupyter_text>List of Dictionaries The below example illustrates how to create a list of dictionaries (a more complex data structure that will be covered below).<jupyter_code>dict_a = {'key_1':'value_1',
          'key_2':'value_2'}
dict_b = {'key_3':'value_3',
          'key_4':'value_4'}
list_a = [dict_a, dict_b, dict_a]
print(list_a)
type(list_a)<jupyter_output>[{'key_1': 'value_1', 'key_2': 'value_2'}, {'key_3': 'value_3', 'key_4': 'value_4'}, {'key_1': 'value_1', 'key_2': 'value_2'}]<jupyter_text>We can also index this list to get the dictionary.<jupyter_code>print(list_a[0])
type(list_a[0])<jupyter_output>{'key_1': 'value_1', 'key_2': 'value_2'}<jupyter_text>List Comprehension List comprehensions provide a concise way to create lists where each new element is the result of some operation applied to each member of another sequence or iterable.<jupyter_code>squares = [x**2 for x in range(10)]
print(squares)
type(squares)<jupyter_output>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<jupyter_text>TUPLES What is a Tuple?A tuple is a data structure that contains an ordered collection of elements that are immutable (unchangeable). These elements are generally of the same type and can contain elements such as single data types through to more complex data structures such as other lists or dictionaries. Why do we use Tuples?Tuples are commonly used in programming to store and organize data so it can be easily sorted, indexed or searched. Since tuples are immutable, we cannot add, change or remove elements. How do we use Tuples?Tuples are assigned to variables using a single equals and are denoted by round brackets and use a comma to separate elements `(a,b,c)`. Creating a Tuple<jupyter_code>tuple_1 = (1,2,3,4)
print(tuple_1)
type(tuple_1)<jupyter_output>(1, 2, 3, 4)<jupyter_text>Dictionaries What is a Dictionary?Dictionaries are data structures that contain a collection of "key" and "value" pairs where each key needs to be a unique string and the value can range from a single variable, to other data structures (such as another dictionary, or a list). If you attempt to add a new key1:value pair when the key1 already exists in the Dictionary, you should get a KeyExists error or risk overwriting the existing data. Since dictionaries can store single or complex data types as values, you can store two identical types in a list or dictionary structure. For example, a key with three identical values in a list key1:[value1,value1,value1] can be stored in a Dictionary. Why do we use a Dictionary?Dictionaries are commonly used in programming to store and organize data so it can be easily sorted, indexed or searched. Since dictionaries are mutable, we can add, change, or remove elements. How do we use a Dictionary?Dictionaries are assigned to variables using a single equals and are denoted by curly brackets and use a colon to separate key and value, and a comma to separate elements `{key1: value1, key2: value2}`. Creating a Dictionary There are two ways to create an empty dictionary - `my_dict = dict()` or `my_dict = {}`. The following example illustrates how to create a dictionary with keys and values.<jupyter_code>dict_1 = {'key_1':'value_1',
          'key_2':'value_2'}

dict_2 = dict(key_100='item_100',key_101='item_101')
print(dict_1)
print(dict_2)
type(dict_1)<jupyter_output>{'key_1': 'value_1', 'key_2': 'value_2'}
{'key_100': 'item_100', 'key_101': 'item_101'}<jupyter_text>Adding items to a Dictionary Once we have created the dictionary data structure object, the below example shows how to add a new key, value pair where the value is a list from the above example.<jupyter_code>dict_1['key_3'] = 'value_3'
print(dict_1)<jupyter_output>{'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}<jupyter_text>Removing items from a Dictionary To remove items from a dictionary you can use the `.pop()` method.<jupyter_code>dict_1.pop('key_1')
print(dict_1)<jupyter_output>{'key_2': 'value_2', 'key_3': 'value_3'}<jupyter_text>Accessing items in a Dictionary To access a value in a dictionary you can use the `.get()` method, which is preferred because if it cannot find the key it will return a `None`.The second method does work but is not considered best practice since python throws `KeyError` and will block a program from continuing.<jupyter_code># to access the item of a dictionary by its key use the .get() 
print(dict_1.get('key_1'))
print(dict_1.get('key_10'))

# or another way
print(dict_1['key_2'])
print(dict_1['key_10'])<jupyter_output>None
None
value_2<jupyter_text>Accessing all keys or items of a Dictionary It is also possible to output all keys or items to a list.<jupyter_code># to get all keys or values
print(list(dict_1.keys()))

# or get all values
print(list(dict_1.values()))<jupyter_output>['key_2', 'key_3']
['value_2', 'value_3']<jupyter_text>Check if key exists in a Dictionary It is always helpful to see if the dictionary contains a specific key.<jupyter_code>if 'key_2' in dict_1: 
    print("key_1 exists!")
if 'random_key' in dict_1: 
    print("the key exists!")
else:
    print('the key does not exist')<jupyter_output>the key does not exist<jupyter_text>Dictionary Comprehension<jupyter_code>squares_dict = {x:x**2 for x in range(10)}
print(squares_dict)
type(squares_dict)<jupyter_output>{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
{'fraction_non_alphanumeric': 0.08916256157635467, 'fraction_numerical': 0.025205254515599344, 'mean_word_length': 5.48615548455804, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '161548', 'license': 'MIT', 'n_tokens_mistral': 3901, 'n_tokens_neox': 3665, 'n_words': 1634, 'path': 'Learn Python Variables and Data Types.ipynb', 'repo_name': 'HDVI/AlphaWaveData', 'size': '12180'}