<jupyter_start><jupyter_text>Variables and types<jupyter_code>a = 2
b = 3.142
print(a)
c
print(type(a))
print(type(b))
s = "Hello World"
print(type(s))
print(a)
print(b)<jupyter_output>2
3.142<jupyter_text>Arithmetic<jupyter_code>print(a + b)
print(a * b)
print(b ** a)
print(a % b)
print(2 * a + b)
d = 8
e = 3
print(d / e)
print(d % e)
print(2 * (a + b))
print("a is", a, "b is", b, sep="_")
print("a is", a, "b is", b, end="    ")
print("end")
help(print)<jupyter_output>Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.<jupyter_text>Changing data types<jupyter_code>a = "3.142"
print(type(a))
print(type(float(a)))
int(a)
float(2)
int(3.142)
int(3.9)
a = float(a)
print(type(a))
b = "Hello World"
print(type(b))
int(b)<jupyter_output><empty_output><jupyter_text>Strings<jupyter_code>name = "Leon"
print(name)
my_sentence = "My name is " + name + "."
print(my_sentence)
print(len(my_sentence))
print(my_sentence.upper())
print(my_sentence)
print(dir(my_sentence))
my_string = "The quick brown fox"
my_string.startswith("The")
my_string.startswith("the")
my_string.find("quick")
my_string.count("fox")
my_string.replace(" ", "_")<jupyter_output><empty_output><jupyter_text>Boolean values<jupyter_code>print(True)
print(False)
print(type(True))
true
print("hello" == "HELLO")
print(3 == 4)
print(3 != 4)
print(3 < 4)
bool_value = "TRUE"
bool(bool_value)
bool_val1 = 'TRUE'
print('read as type ',type(bool_val1))
print('value when cast to bool',bool(bool_val1))

bool_val2 = 'FALSE'
print('read as type ',type(bool_val2))
print('value when cast to bool',bool(bool_val2))

bool_val3 = 1
print('read as type ',type(bool_val3))
print('value when cast to bool',bool(bool_val3))

bool_val4 = 0
print('read as type ',type(bool_val4))
print('value when cast to bool',bool(bool_val4))

bool_val5 = -1
print('read as type ',type(bool_val5))
print('value when cast to bool',bool(bool_val5))
print(bool(bool_val5))
bool("")<jupyter_output><empty_output><jupyter_text>Structured data types<jupyter_code>list1 = [6, 54, 89]
print(list1)
print(type(list1))
list2 = [6, "hello", 3.1]
list1[0]
list1[0:2]
list3 = [list1[0], list1[2]]
print(list3)
num_list = [4,5,6,11]

last_num_in_list = num_list[-1]
print(last_num_in_list)

odd_from_list = [num_list[1], num_list[-1]]
print(odd_from_list)
import numpy as np
num_list_np_array = np.array(num_list)
num_list_np_array[num_list_np_array%2 == 1]
list(range(5))
list(range(1, 9))
my_list = list(range(2, 11, 2))
my_list = list(range(2, 11, -2))
print(my_list)
my_list = list(range(10, 1, -2))
print(my_list)<jupyter_output>[10, 8, 6, 4, 2]<jupyter_text>The `if` statement<jupyter_code>if statement:
    statement 1
    statement 2
    
statement always executed
value = 5
threshold = 4
print("value is", value, "threshold is", threshold)

if value > threshold:
    print(value, "is bigger than", threshold)
value = 3
threshold = 4
print("value is", value, "threshold is", threshold)

if value > threshold:
    print(value, "is bigger than", threshold)
    
print("end")
value = 4
threshold = 4
print("value is", value, "threshold is", threshold)

if value > threshold:
    print(value, "is bigger than", threshold)
elif value == threshold:
    print(value, "is equal to", threshold)
else:
    print(value, "is smaller than", threshold)
    
print("end")<jupyter_output>value is 4 threshold is 4
4 is equal to 4
end<jupyter_text>The `for` loop<jupyter_code>my_list = [1, 2, 3, True, "hello"]
print(my_list)
for item in my_list:
    print(item)
long_string = "The quick brown fox jumped over the lazy sleeping dog"
for word in long_string.split():
    print(word)
    print(word.upper())
variablelist = "01/01/2010,34.5,Yellow,True"
for word in variablelist.split(","):
    print(word)<jupyter_output>01/01/2010
34.5
Yellow
True<jupyter_text>Defining your own functions<jupyter_code>items_owned = "bicycle;television;solar_panel;table"
def get_item_count(items_str, sep):
    '''
    This function counts the number of items in a string
    '''
    items_list = items_str.split(sep)
    num_items = len(items_list)
    return num_items
number_of_items = get_item_count(items_owned, ";")
print(number_of_items)
# comment<jupyter_output>4<jupyter_text>![](https://datacarpentry.org/python-socialsci/fig/functionAnatomy.png) to run a cell: shift + enter<jupyter_code>def calculate_vol_cuboid(h, w, l):
    """
    Calculates the volume of a cuboid. h, w, and l are 
    the height, width, and length
    """
    volume = h * w * l
    return volume

calculate_vol_cuboid(1, 2, 3)
help(calculate_vol_cuboid)<jupyter_output>Help on function calculate_vol_cuboid in module __main__:

calculate_vol_cuboid(h, w, l)
    Calculates the volume of a cuboid. h, w, and l are 
    the height, width, and length
{'fraction_non_alphanumeric': 0.1452081316553727, 'fraction_numerical': 0.028073572120038724, 'mean_word_length': 5.417391304347826, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '323919', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1968, 'n_words': 501, 'path': 'files/2-introduction-to-python.ipynb', 'repo_name': 'esciencecenter-digital-skills/2021-12-06-dc-socsci-python-nlesc', 'size': '5165'}