<jupyter_start><jupyter_text>COURSE: Master math by coding in Python SECTION: Data types https://www.udemy.com/course/math-with-python/?couponCode=MXC-DISC4ALL INSTRUCTOR: sincxpress.comNote about this code: Each video in this section of the course corresponds to a section of code below. Please note that this code roughly matches the code shown in the live recording, but is not exactly the same -- the variable names, order of lines, and parameters may be slightly different.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>VIDEO: Numbers and strings<jupyter_code># ints vs. floats

c = 7
d = 7.0

print(c,d)
print( type(c) )
print( type(d) )
# implication of datatype for functions loops

for i in range(c): # vs d
    print(i)
# we can add numbers; can we "add" strings??
firstName = 'Mike'
lastName  = 'Cohen'

print( firstName + lastName )
print( firstName + ' ' + lastName )
firstName * 3
%whos<jupyter_output><empty_output><jupyter_text>Exercise<jupyter_code>### converting across datatypes

s1 = '4'
s2 = '4.7'
n1 = 5
n2 = 5.8

## convert strings to integers
print( int(s1) )
# print( int(s2) )


## string to floating-point
print( float(s1) )
print( float(s2) )


## number to string
print( str(n1) )
print( str(n2) )<jupyter_output><empty_output><jupyter_text>VIDEO: Lists and numpy arrays<jupyter_code>aList = [0,1,2,3,4,5]
aList
type(aList)
listList = [ 3,['3','4','5'],5,[4,5,6] ]
listList
listList = [ 
            3,              # a number
            ['3','h','5'],  # a list of strings
            5,              # another number
            [4,5,6]         # a list of numbers
           ]

listList
# can we add lists?

aList = [0,1,2,3,4,5]
strList = ['0','1','2','3','4','5']

aList + strList
# aList + listList
# multiply a list?
aList*3

# the principal here is 'overloading': One operation has multiple behaviors depending on the context
# lists vs. numpy arrays

aList_np = np.array(aList)

print(aList)
print(aList_np)
# now let's try multiplication again
print(aList*3)
print(aList_np*3)
# scalar addition?
print(aList+3)
print(aList_np+3)<jupyter_output><empty_output><jupyter_text>Exercise<jupyter_code># regular method
for idx,item in enumerate(listList):
    print(f'List element {idx} is {item} and is of type {type(item)}.')
# advanced method to print out only the type
for idx,item in enumerate(listList):
    t = str(type(item))[8:] 
    apost = t.find("'")
    print(f'List element {idx} is {item} and is of type {t[:apost]}.')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13153724247226625, 'fraction_numerical': 0.02377179080824089, 'mean_word_length': 4.227743271221532, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '82519', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 908, 'n_words': 268, 'path': 'MXC_pymath_dataTypes.ipynb', 'repo_name': 'stefan-cross/mathematics-with-python', 'size': '2524'}