<jupyter_start><jupyter_text>What we learnt yesterday - downloaded the course material from github- set up our coding environment- got familiar with jupyter-lab   Variables  By the end of the day you'll be able to - Perform basic operations with numerical variables (integers and floats)- Perform basic operations with boolean variables- Perform basic operations with characters and strings- Convert between variable types   Variables   By the end of the day you'll be able to - **Perform basic operations with numerical variables (integers and floats)**- Perform basic operations with boolean variables- Perform basic operations with characters and strings- Convert between variable types   Python as a calculator<jupyter_code>12/5.1<jupyter_output><empty_output><jupyter_text>Variables - Overview- Data is represented as numbers, strings, or boolean variables.    - Variables store a value but it can be changed/updated by the program- Numbers can be integers (whole numbers) or floats (numbers with decimal points).- Boolean variables can have only two values: True or False.- Strings are a sequence of characters within quotation marks.   Numbers<jupyter_code># variables store values that we can work with and modify it later.
# variables can be named arbitrarily but I recommend to use the hungarian notation!
# assign value to variables
a_int = 5
b_int = 8
c_flt = 1.6
d_flt = 5.2

# to check the type of a variable:
print(type(a_int)) # use parentheses for type and print
print(type(b_int))
print(type(c_flt))
print(type(d_flt))<jupyter_output><class 'int'>
<class 'int'>
<class 'float'>
<class 'float'><jupyter_text>HELP() is your friend! Use it on variables, functions, any part of the code really.<jupyter_code>help(type)<jupyter_output>Help on class type in module builtins:

class type(object)
 |  type(object_or_name, bases, dict)
 |  type(object) -> the object's type
 |  type(name, bases, dict) -> a new type
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |  
 |  __dir__(self, /)
 |      Specialized __dir__ implementation for types.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __instancecheck__(self, instance, /)
 |      Check if an object is an instance.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |  
 |  __sizeof__(self, /)
 |      Return memory consumption of the type object.
 |  
 |  __subclasscheck__(self, subclass, /)
 |     [...]<jupyter_text>Let's work with the variables and create new ones<jupyter_code>a_int = 5
b_int = 8
c_flt = 1.6
d_flt = 5.2

# print the sum of two numbers
print(a_int+b_int)
# print the product of two numbers
print(a_int*c_flt)
# what is the type of this new variable?
print(type(a_int*c_flt))
# create a new varible!
e_flt = a_int*c_flt
print(e_flt)
# create a new variable and equate it to the difference of two numbers
print(type(d_flt - b_int))
f_flt = d_flt - b_int
print(f_flt)
# multiply two numbers and store the value in a new variable
print(type(b_int*a_int))
g_int = b_int*a_int
print(g_int)<jupyter_output><empty_output><jupyter_text>Change the value of a variable<jupyter_code># let's add 2 to a_int
print(a_int)
a_int = a_int + 2
print(a_int)
# be careful! if you rerun this cell, a_int will keep increasing by 2.
# let's multiply b_int with 1.5
# what's the type of the new variable? 
# Should we store this value in b_int?
b_int = 8
print(b_int*1.5)
print(type(b_int*1.5))
print(type(b_int))
b_flt = b_int*1.5 # NOT CORRECT!
print(type(b_flt))<jupyter_output><empty_output><jupyter_text>Exercise 1- What's the type of a_int / b_int?  Create a new variable for it and start the name of the variable with ans1_ and complete it with the type of a_int / b_int!<jupyter_code># add your code below this line


# the last line of the exercises show the solution.
# <class 'float'><jupyter_output><empty_output><jupyter_text>Variables   By the end of the day you'll be able to - Perform basic operations with numerical variables (integers and floats)- **Perform basic operations with boolean variables**- Perform basic operations with characters and strings- Convert between variable types   Boolean variables<jupyter_code># create a boolean variable
var1_bool = True
var2_bool = False
print(var1_bool, var2_bool)
print(type(var1_bool))<jupyter_output><empty_output><jupyter_text>Compare variables<jupyter_code>a_int = 5
b_int = 8
c_flt = 1.6
d_flt = 5.0
c1_bool = c_flt < b_int
c2_bool = a_int > b_int
c3_bool = a_int == d_flt
print(c1_bool)
print(c2_bool)
print(c3_bool)
a_int = 5
b_int = 8
c_flt = 1.6
d_flt = 5.0
# is b_int+c_flt larger than a_int+d_flt?
compvars1_bool = b_int+c_flt > a_int+d_flt
# is c_flt-a_int smaller than b_int-d_flt?
compvars2_bool = c_flt-a_int < b_int-d_flt
print(compvars1_bool)
print(compvars2_bool)<jupyter_output><empty_output><jupyter_text>check variable type<jupyter_code># is the type of a_int and c_flt the same?
comptypes1_bool = type(a_int) == type(c_flt)
print(comptypes1_bool)

# is a_int of type int?
type_a_bool = type(a_int) == int
print(type_a_bool)

# is d_flt really a float?
type_d_bool = type(d_flt) == float
print(type_d_bool)<jupyter_output><empty_output><jupyter_text>Exercise 2<jupyter_code>a_int = 3
b_int = 9.1
# Check if a_int and b_int are really of type int! Create a boolean variable with the name 
# compvars_ab_bool and check if the two types are the same!
# Do you notice anything strange here? 
# add your code below this line




# False<jupyter_output><empty_output><jupyter_text>Variables   By the end of the day you'll be able to - Perform basic operations with numerical variables (integers and floats)- Perform basic operations with boolean variables- **Perform basic operations with characters and strings**- Convert between variable types   Characters<jupyter_code>a_lowercase_str = 'a'
print(a_lowercase_str)
another_lowercase_str = "b"
print(another_lowercase_str) 
an_uppercase_str = 'Z'
print(an_uppercase_str)
a_number_str = '9'
print(a_number_str)

# a_number_char + 6
a_symbol_str = '-' 
print(a_symbol_str)<jupyter_output><empty_output><jupyter_text>Strings<jupyter_code>short_str = "string"
longer_str = 'This is my first string!'

print(short_str)
print(longer_str) 
# beware of apostrophe in strings!

# NOT VALID
print('There's my first string!')

# VALID
print("There's my first string!")<jupyter_output><empty_output><jupyter_text>String indexing   string[*starting index* : *stopping index* : *step size*]  Default Values:* start = beginning of string* stop = end of string* step = 1<jupyter_code>my_name_str = '<NAME>'

# indexing one character
print(my_name_str[0])
print(my_name_str[1])
print(my_name_str[-3])
# indexing a range of characters
print(my_name_str[0:3]) # stop index is not included!
print(my_name_str[3:])
print(my_name_str[:6]) 
# indexing a range of characters with a step
print(my_name_str[::2])
print(my_name_str[1::3]) 
print(my_name_str[::]) # default step size = 1
# backwards indexing
print(my_name_str[::-1])<jupyter_output><empty_output><jupyter_text>Exercise: Indexing a string`my_name_str = '<NAME>'`* Assign the initial of the last name to `a_str`* Assign the last four characters to `b_str`* Assign every second character to `c_str`* Assign every third character in reverse order to `d_str`* Assign the characters between the second and the sixth location to `e_str`* Print all variables<jupyter_code>my_name_str = '<NAME>'

a_str = my_name_str[-4]
b_str = my_name_str[-4:]
c_str = my_name_str[::2]
d_str = my_name_str[::-3]
e_str = my_name_str[2:6]

print(a_str)
print(b_str)
print(c_str)
print(d_str)
print(e_str)<jupyter_output><empty_output><jupyter_text>Useful string operations<jupyter_code># combine strings
first_name_str = 'Andras'
second_name_str = 'Zsom'
full_name_str = first_name_str + second_name_str
print(full_name_str)
space_name_str = first_name_str + " " + second_name_str
print(space_name_str)
# repeat strings
laugh_str = 'ha'
print(laugh_str*3)
# split strings
print(space_name_str)
# split on space
print(space_name_str.split())
print(space_name_str.split(' '))
# split on another character
print(space_name_str.split('l'))
# check if characters are in string
full_name_str = '<NAME>'
chars = 'and'
check_bool = chars in full_name_str
print(check_bool)
chars = 'And'
check_bool = chars in full_name_str
print(check_bool)
# find - returns the first occurrence of a character
print(full_name_str)
print(full_name_str.find('a'))<jupyter_output><empty_output><jupyter_text>Exercise: How would you check if `first_str = 'racecar'` is a palindrome? Assign the check to a variable called `check_pal_bool` and print it.<jupyter_code>first_str = 'racecar'
# add your code below


# True<jupyter_output><empty_output><jupyter_text>Variables   By the end of the day you'll be able to - Perform basic operations with numerical variables (integers and floats)- Perform basic operations with boolean variables- Perform basic operations with characters and strings- **Convert between variable types**  Casting between types<jupyter_code>print(9 + 5)
print('9' + '5')
print('9' + 5)
print(str(9))
print(type(str(9)))

print(int('9'))
print(type(int('9')))
print(int('9') + 5)
print(float('9.5'))
print(type(float('9.5')))
int('a')
# boolean casting
print(int(True),int(False)) # True is 1, False is 0
print(float(True),float(False)) # True is 1.0, False is 0.0
print(str(True),str(False)) # True is 'True', False is 'False'
print(bool('True'),bool('False')) # both strings were cast to True!!!
print(bool(0.1),bool(-100)) # both number are cast to True
print(bool(0),bool(0.0)) # everything but the number 0 is cast to True<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10395491597061488, 'fraction_numerical': 0.011673543322934488, 'mean_word_length': 5.419896640826873, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 80, 'https://': 0, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '451854', 'license': 'MIT', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3455, 'n_words': 1063, 'path': 'week_1/day_2_variables.ipynb', 'repo_name': 'tbw005/workshop-python-2021', 'size': '9957'}