<jupyter_start><jupyter_text>Type Conversions  Every value has a type. * Every value in a program has a specific type.* Integer (`int`): represents positive or negative whole numbers like 3 or -512.* Floating point number (`float`): represents real numbers like 3.14159 or -2.5.* Character string (usually called "string", `str`): text.    * Written in either single quotes or double quotes (as long as they match).    * The quote marks aren't printed when the string is displayed.  Use the built-in function `type` to find the type of a value. * Use the built-in function `type` to find out what type a value has.* Works on variables as well.    * But remember: the *value* has the type --- the *variable* is just a label.<jupyter_code>print(type(52))
fitness = 'average'
print(type(fitness))<jupyter_output><class 'str'><jupyter_text>Types control what operations (or methods) can be performed on a given value. * A value's type determines what the program can do to it.<jupyter_code>print(5 - 3)
print('hello' - 'h')<jupyter_output><empty_output><jupyter_text>You can use the `+` and `*` operators on strings. * "Adding" character strings concatenates them.<jupyter_code>full_name = 'Ahmed' + ' ' + 'Walsh'
print(full_name)<jupyter_output><NAME><jupyter_text>* Multiplying a character string by an integer *N* creates a new string that consists of that character string repeated *N* times.    * Since multiplication is repeated addition.<jupyter_code>separator = '=' * 10
print(separator)<jupyter_output>==========<jupyter_text>Strings have a length (but numbers don't). * The built-in function `len` counts the number of characters in a string.<jupyter_code>print(len(full_name))<jupyter_output>11<jupyter_text>* But numbers don't have a length (not even zero).<jupyter_code>print(len(52))<jupyter_output><empty_output><jupyter_text>Must convert numbers to strings or vice versa when operating on them. * Cannot add numbers and strings.<jupyter_code>print(1 + '2')<jupyter_output><empty_output><jupyter_text>* Not allowed because it's ambiguous: should `1 + '2'` be `3` or `'12'`?* Some types can be converted to other types by using the type name as a function.<jupyter_code>print(1 + int('2'))
print(str(1) + '2')<jupyter_output>3
12<jupyter_text>Can mix integers and floats freely in operations. * Integers and floating-point numbers can be mixed in arithmetic.    * Python 3 automatically converts integers to floats as needed. (Integer division in Python 2 will return an integer, the *floor* of the division.)<jupyter_code>print('half is', 1 / 2.0)
print('three squared is', 3.0 ** 2)<jupyter_output>half is 0.5
three squared is 9.0<jupyter_text>Variables only change value when something is assigned to them. * If we make one cell in a spreadsheet depend on another, and update the latter, the former updates automatically.* This does **not** happen in programming languages.<jupyter_code>first = 1
second = 5 * first
first = 2
print('first is', first, 'and second is', second)<jupyter_output>first is 2 and second is 5<jupyter_text>* The computer reads the value of `first` when doing the multiplication, creates a new value, and assigns it to `second`.* After that, `second` does not remember where it came from.  Questions  Q1: Fractions What type of value is 3.4? How can you find it? **Solution**Click on the '...' below to show the solution.<jupyter_code># It is a floating-point number (often abbreviated "float").

print(type(3.4))<jupyter_output><empty_output><jupyter_text>Q2: Automatic Type Conversion What type of value is 3.25 + 4? **Solution**Click on the '...' below to show the solution.<jupyter_code># It is a float. Integers are automatically converted to floats as necessary.

result = 3.25 + 4
print(result, 'is', type(result))<jupyter_output><empty_output><jupyter_text>Q3: Choose a type What type of (integer, floating point number, or character string) would you use to represent each of the following? Try to come up with more than one good answer for each problem. For example, in 1, when would counting days wioth a floating point variable make more senes than using an integer? 1. Number of days since the start of the year2. Time ealpsed form the start of the year until now in days3. Serial number of a piece of lab equipment4. A lab specimen's age5. Current population of a city6. Average Population of a city over time **Solution**Click on the '...' below to show the solution.<jupyter_code># 1. Integer, since the numebr of days would lie between 1 and 365.
# 2. Floating point, since the fractional dats are required.
# 3. Character string if serial number contains letters and numbers, otherwise 
#    integer if the serial number consists of only numerals
# 4. This will vary! How do you define a specimen's age? Whole days since 
#    collection (integer) data and time (string)
# 5. Choose floating point to represent population as large aggregates 
#    (eg. millions), or integer to preresent population in unties of individuals.
# 6. Floating point number, since an average is likely to have a fractional part.<jupyter_output><empty_output><jupyter_text>Q4: Division Types In Pyhton 3, the `//` operator performs integer(whole-number) floor division, the `/` operator performs floating point division, and the `%` or (modulo) operator calulates and returns the remainder from integer division.<jupyter_code>print('5 // 3:', 5//3)
print('5 / 3:', 5/3)
print('5 % 3:', 5&3)<jupyter_output>5 // 3: 1
5 / 3: 1.6666666666666667
5 % 3: 1<jupyter_text>However in Python2 (and other langauges), the `/` operator between two integer  types froms a floor (`//`) division. To perform a float division, we have to convert one of the inetgers to float.<jupyter_code>print('5 // 3:', 1)
print('5 / 3:', 1 )
print('5 / float(3):', 1.6666667 )
print('float(5) / 3:', 1.6666667 )
print('float(5 / 3):', 1.0 )
print('5 % 3:', 2)<jupyter_output><empty_output><jupyter_text>if `num_subjects` is the number of subjects taking part in a study, and `num_per_survey` is the number that can take part in a single survey, write an expression that calulcates the number of surveys needed to reach everyone once. **Solution**Click on the '...' below to show the solution.<jupyter_code># We want the minimum number of surveys that reaches everyone once, whoch is the 
# rounded up value of num_nubjects / num_per_survey. This is equivalent to 
# performing an integer division with // and adding 1.

num_subjects = 600
num_per_survey = 42
num_surveys = num_subjects // num_per_survey + 1

print(num_subjects, 'subjects,', num_per_survey, 'per survey:', num_surveys)<jupyter_output><empty_output><jupyter_text>Q5: Strings to Numbers Where reasonable, `float()` will convert a string to a floating poeint number, and `int()` will convert a floating point number to an integer.<jupyter_code>print("string to float:", float("3.4"))
print("float to int:", int(3.4))<jupyter_output>string to float: 3.4
float to int: 3<jupyter_text>If the conversion does not make sense, however, an error message will occur<jupyter_code>print("string to float:", float("Hello World!"))<jupyter_output><empty_output><jupyter_text>Given this information, what do you expect the following program to do?What does it actually do?Why do you think it does that? `print("fractional string to int:", int("3.4"))` **Solution**Click on the '...' below to show the solution.<jupyter_code># It would be unreasonable to expect the Python in the command to convert the 
# string "3.4" to 3.4 and an additional type conversion to 3. After all, Python  
# performs a lot of other magic - isn't that part of its charm?
#
# However, Python throws an error. Why? To be consistent, possibly. If you ask 
# Python to perform two consecutive typecasts, you must convert it explicitly 
# in code, as in line 2 below.

int("3.4")
int(float("3.4"))<jupyter_output><empty_output><jupyter_text>Q6: Arithmetic with Different Types Which of the following will print 2.0? Note: there may be more than one right answer.<jupyter_code>first = 1.0
second = "1"
third = "1.1"<jupyter_output><empty_output><jupyter_text>1. `first + float(second)`2. `float(second) + float(third)`3. `first + int(third)`4. `first + int(float(third))`5. `int(first) + int(float(third))`6. `2.0 * second` **Solution**Click on the '...' below to show the solution.<jupyter_code># Answer 1 and 4.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10492351816443594, 'fraction_numerical': 0.02425908221797323, 'mean_word_length': 5.086545454545455, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 78, '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': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '284095', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2505, 'n_words': 1069, 'path': '_episodes_rapids/030_types_conversions.ipynb', 'repo_name': 'UFResearchComputing/py4ai', 'size': '8373'}