<jupyter_start><jupyter_text>This notebook serves as a tutorial of basic Python programming. You will become familiar with Python data structures including both the basic and advanced data types. You will also start using Python if statement and loops. At the end of this tutorial, you should be feeling comfortable writing basic Python codes. You will also obtain most of the Python coding knowlege that you need to know in order to finish the lab exercises.But keep in mind that the best way to get better at Python coding (or any other  programming language) is to practice it every day!Many of the example codes in this manual come with comments, which in Python always start with the hash character , and extend to the end of the physical line. Comments appear only for the purpose of the clarity of codes, and are not interpreted by Python.Please run the Python codes in the following cells, and have fun!Updated by: Toul @toul.io, 4/2/2021Author: <NAME> @ University of Houston, 01/23/2018  Section 1: Integers<jupyter_code># The first basic Python data type is integers
a = 3
print(a)
# All the algebraic operations such as addition, subtraction, division, etc, apply to integers
b = 5
c = 13
print('b + c =', b + c)
print('b - c =', b - c)
print('b * c =', b*c)
print('b/c =', b/c)

# Note the % operator returns the remainder of the division
d = 17
e = 3
print('17 % 3 =', 17%3, 'This is the remainder!')
print('17 / 3 =', 17/3, 'This is division!')<jupyter_output>17 % 3 = 2 This is the remainder!
17 / 3 = 5.666666666666667 This is division!<jupyter_text>Section 2: Floats<jupyter_code># Floats are simply numbers with decimals
f = 5.678
print(f, 'is a floating point number.')
# Similar to integers, for floats, you can also perform any of the algebraic operations
g = 82.132
print('f * g =', f*g)<jupyter_output>f * g = 466.345496<jupyter_text>Section 3: Strings<jupyter_code># A string is a collection of characters. In Python, you create a string by enclosing a sequence of characters within a pair of single or double quotes
h = 'John'
i = 'lives'
j = "at"
k = "Houston"
print(h + i + j + k)
# Note that the output from previous code cell looks odd because of the lack of spaces between adjacent words.
# A simple way to fix it is to add one additional space in each string (except for the last one)
h = "John "
i = "lives "
j = "at "
k = "Houston"
print(h + i + j + k)<jupyter_output>John lives at Houston<jupyter_text>Note that you can concatenate strings by using the + sign. Also, note that this line you are reading now is NOT comments, because it does not start with the hash sign .<jupyter_code># If you want to start a new line, you need to use \n.
line = 'First line. \nSecond line.'
print(line)
# Strings can be indexed (subscripted), with the fist character having index of 0. 
# Note that Python does not have a character data type. A single character can be considered as a string of size one.
mychar = 'Python'
mychar[0]  # The character with index 0
mychar[1]  # The character with index 1
mychar[2:] # The sequence of characters beginning with the one with index 2 until the last character in the string
print(mychar * 2)<jupyter_output>PythonPython<jupyter_text>Section 4: Boolean<jupyter_code># A Boolean variable in Python can only take two values: True, False
bvar = 'True'
print(bvar)<jupyter_output>True<jupyter_text>Now we are moving on to the more advanced data types in Python  Section 5: Array<jupyter_code># An array is a compact way of collecting values of the same basic data types (i.e., homogeneous items)
# Since we will use Numpy array for our class, we will only look at Numpy array for now.
# To creat a Numpy array, we first need to import Numpy library
import numpy as np<jupyter_output><empty_output><jupyter_text>Note: Python is a great language largely because of all the available packages. A package is a collection of code that someone else or a team has written that you can use in your code if they've open sourced it. Python has an incredible library of open source packages and to use them we do 'import'. In the above we've shortened the package name, numpy, to np to make it easier to code with.<jupyter_code># Now, let us create a Numpy array and multiply it by 3.2
narr1 = np.array([2,3,5])
narr2 = narr1 * 3.2
print(narr2)
# You can slice a numpy array using indices
narr2[0]  # The element or item associated with index 0
# If you want to add a value, say, 21.3, to every elements, you can simply do the following:
narr3 = narr2 + 12.3
print(narr3)<jupyter_output>[18.7 21.9 28.3]<jupyter_text>Section 6: Lists<jupyter_code># A list is a collection of heterogenous items (i.e., items can be of different data types). The values for these items are mutable.
# The elements in a list are enclosed within [ ], and separated by comma , 
list1 = [2.8, 9.2, 23.1]
list1[0]  # The first element in the list<jupyter_output><empty_output><jupyter_text>Do not worry! That is just to show you why we prefer to work with Numpy array. Simple operations like in the above example are not supported by this data type.<jupyter_code># Note that lists can take heterogenous items, while Numpy array can only take homogenous items.
list2 = [2.89, True, 'Houston', 14]
print(list2)
array2 = np.array([2.89, True, 'Houston', 14])
print(array2)<jupyter_output>[2.89, True, 'Houston', 14]
['2.89' 'True' 'Houston' '14']<jupyter_text>Pay close attention to the output of print(array2). Have you noticed that when you put heterogenous items into a Numpy array, all the items will be converted into the same data type (in this case, strings)?  Section 7: Tuples<jupyter_code># A tuple is a collection of heterogeneous items that are enclosed by parentheses (optional) and separated by comma ,
# The values of the items in a tuple are immutable
tup1 = 1, 2, 3, 4, 5
tup1[1]   # the item with index of 1 in the tuple
tup2 = (4, 21, 6, 9, 76)
tup2[2]   # the item with index of 2 in the tuple
tup3 = 'dog', 'cat', 29, 13
tup3[1]<jupyter_output><empty_output><jupyter_text>Section 8: Dictionary<jupyter_code># A dictionary consists of a sequence of key-value pairs, enclosed by curly brackets { }, and separate by comma ,
stockprice = {"ExxonMobile":88.32, "BP":43.86, "Chevron":131.70, "Anadarko":61.32}
stockprice["BP"]   # retrieve the stock price for BP, by using the key
# you can check if a key exists in a dictionary
"BP" in stockprice
# You can change the value associated with any key in the dictionary
del(stockprice["BP"])
stockprice<jupyter_output><empty_output><jupyter_text>Section 9: If statement The syntax for if statement:**if** *expression*: &nbsp;&nbsp; statement(s)An if statement consists of a boolean expression (or equivalently, logical expression) followed by one or more statements.  If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s) is executed.<jupyter_code># an example of if statement
val = 10
if (val%2 == 0):   # The expression in the brackets is true; note that the brackets are optional
    print('val is divisible by 2')    # Therefore, this statement is executed<jupyter_output>val is divisible by 2<jupyter_text>We can also have if-else statement:    **if** *expression*: &nbsp;&nbsp; statement(s) **else:**&nbsp;&nbsp; statement(s)<jupyter_code># an example of the if-else statement
val = 15
if (val%2 == 0):   # The expression in the brackets is false
    print("val is even!")  # Therefore, this statement is not executed
else: 
    print("val is odd!")  # This statement is executed instead.<jupyter_output>val is odd!<jupyter_text>A more general form of the if statement has the following form:**if** *expression1*: &nbsp;&nbsp;&nbsp; statement(s) **elif** *expression2*:&nbsp;&nbsp;&nbsp; statement(s)**elif** *expression3*:&nbsp;&nbsp;&nbsp; statement(s)**else:**&nbsp;&nbsp;&nbsp; statement(s)<jupyter_code># An example of the if-elif-else statement
var = 1500
if var == 1000:   # the expression is false
    print("1 - Got a true expression value")  # Thus, this statement is not executed
    print(var)
elif var == 1500:  # the expression is true
    print("2 - Got a true expression value")  # Thus, this statement is executed
    print(var)
elif var == 2000:
    print("3 - Got a true expression value")
    print(var)
else:
    print("4 - Got a false expression value")
    print(var)<jupyter_output>2 - Got a true expression value
1500<jupyter_text>Section 10: While loop The syntax for while loop:    **while** *expression*: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statement(s)    A while loop repeatedly executes the statement(s) if the expression evaluates to True.If the expression evaluates to False, the statement(s) will be skipped, and the first statement after the while loop will be executed.<jupyter_code># example of a while loop 
val = 0
while (val < 6):
    print("val =", val)
    val = val + 1
    
print('done!')<jupyter_output>val = 0
val = 1
val = 2
val = 3
val = 4
val = 5
done!<jupyter_text>Section 11: For loop The for loop has the following form:    **for** *var* **in** *seq*: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statement(s)  which can be interpreted as:for each *var* in the sequence *seq* (could be a list or a string), execute statements<jupyter_code># an example of for loop
for letter in "Python":   # note that the sequence here is actually a string
    print("Current letter is:", letter)
# another example of for loop
cities = ["Houston", "LA", "Denver", "New York"]
for city in cities:  # Note that the sequence, cities, here is a list
   print("The current city is:", city)<jupyter_output>The current city is: Houston
The current city is: LA
The current city is: Denver
The current city is: New York
{'fraction_non_alphanumeric': 0.09121552604698673, 'fraction_numerical': 0.025842696629213482, 'mean_word_length': 4.607674684994272, 'pattern_counts': {'":': 5, '<': 55, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '203318', 'license': 'MIT', 'n_tokens_mistral': 3125, 'n_tokens_neox': 2882, 'n_words': 1413, 'path': '2_Introduction_to_basic_Python_programming.ipynb', 'repo_name': 'T-O-U-L/data-analytics-nbs', 'size': '9794'}