|
| 1 | +# Numbers and strings |
| 2 | + |
| 3 | +In this chapter we will work with the most basic datatypes, numbers and strings. Start your Python REPL and write the following: |
| 4 | + |
| 5 | +```Python |
| 6 | +>>> a = 2 |
| 7 | +>>> type(a) |
| 8 | +<class 'int'> |
| 9 | +>>> b = 2.5 |
| 10 | +>>> type(b) |
| 11 | +<class 'float'> |
| 12 | +``` |
| 13 | + |
| 14 | +Basically, you are declaring two variables (named "a" and "b") which will hold some numbers: variable "a" is an integer number while variable "b" is a real number. We can now use our variables or any other numbers to do some calculations: |
| 15 | + |
| 16 | +```Python |
| 17 | +>>> a + b |
| 18 | +4.5 |
| 19 | +>>> (a + b) * 2 |
| 20 | +9.0 |
| 21 | +>>> 2 + 2 + 4 - 2/3 |
| 22 | +7.333333333333333 |
| 23 | +``` |
| 24 | + |
| 25 | +Python also has support for string datatypes. Strings are sequences of characters (like words) and can be defined using single or double quotes: |
| 26 | + |
| 27 | +```Python |
| 28 | +>>> hi = "hello" |
| 29 | +>>> hi |
| 30 | +'hello' |
| 31 | +>>> bye = 'goodbye' |
| 32 | +>>> bye |
| 33 | +'goodbye' |
| 34 | +``` |
| 35 | + |
| 36 | +You can add strings to concatenate them but you can not mix different datatypes, such as strings and integers. |
| 37 | + |
| 38 | +```Python |
| 39 | +>>> hi + "world" |
| 40 | +'helloworld' |
| 41 | +>>> "Hello" + 3 |
| 42 | +Traceback (most recent call last): |
| 43 | + File "<stdin>", line 1, in <module> |
| 44 | +TypeError: must be str, not int |
| 45 | +``` |
| 46 | + |
| 47 | +However, multiplication works as repetition: |
| 48 | + |
| 49 | +```Python |
| 50 | +>>> "Hello" * 3 |
| 51 | +'HelloHelloHello' |
| 52 | +``` |
| 53 | + |
| 54 | +## Exercises with numbers |
| 55 | + |
| 56 | +1. Try the following mathematical calculations and guess what is happening: \((3 / 2)\), \((3 // 2)\), \((3 \% 2)\), \((3**2)\). |
| 57 | + |
| 58 | + Suggestion: check the Python library reference at <https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex>. |
| 59 | + |
| 60 | +2. Calculate the average of the following sequences of numbers: (2, 4), (4, 8, 9), (12, 14/6, 15) |
| 61 | + |
| 62 | +3. The volume of a sphere is given by (4/3 * pi * r^3). Calculate the volume of a sphere of radius 5. Suggestion: create a variable named "pi" with the value of 3.1415. |
| 63 | + |
| 64 | +4. Use the module operator (%) to check which of the following numbers is even or odd: (1, 5, 20, 60/7). |
| 65 | + |
| 66 | + Suggestion: the remainder of \(x/2\) is always zero when \(x\) is even. |
| 67 | + |
| 68 | +5. Find some values for \(x\) and \(y\) such that \(x < 1/3 < y\) returns "True" on the Python REPL. Suggestion: try \(0 < 1/3 < 1\) on the REPL. |
| 69 | + |
| 70 | +## Exercises with strings |
| 71 | + |
| 72 | +Using the Python documentation on strings (<https://docs.python.org/3/library/stdtypes.html?#text-sequence-type-str>), solve the following exercises: |
| 73 | + |
| 74 | +1. Initialize the string "abc" on a variable named "s": |
| 75 | + |
| 76 | + 1. Use a function to get the length of the string. |
| 77 | + |
| 78 | + 2. Write the necessary sequence of operations to transform the string "abc" in "aaabbbccc". Suggestion: Use string concatenation and string indexes. |
| 79 | + |
| 80 | +2. Initialize the string "aaabbbccc" on a variable named "s": |
| 81 | + |
| 82 | + 1. Use a function that allows you to find the first occurence of "b" in the string, and the first occurence of "ccc". |
| 83 | + |
| 84 | + 2. Use a function that allows you to replace all occurences of "a" to "X", and then use the same function to change only the first occurence of "a" to "X". |
| 85 | + |
| 86 | +3. Starting from the string "aaa bbb ccc", what sequences of operations do you need to arrive at the following strings? You can use the "replace" function. |
| 87 | + |
| 88 | + 1. "AAA BBB CCC" |
| 89 | + |
| 90 | + 2. "AAA bbb CCC" |