|
| 1 | +# Constants and their types |
| 2 | + |
| 3 | +## Constants |
| 4 | + |
| 5 | +Constant values such as numbers, letters, and strings (strings) are called "constants" because their value does not change. In Python, constants can be of the following types: |
| 6 | + |
| 7 | +### Numeric types |
| 8 | + |
| 9 | +* integer numbers: `1`, `100`; |
| 10 | +* integers in non-decimal notation systems beginning with the prefix `0b` (binary notation), `0x` (hexadecimal notation), `0o` (octal notation): `0b101` (5), `0x1a` (26), `0o12` (10); |
| 11 | +* real numbers (the decimal part is separated by a dot and no digits before or after the dot means 0): `2.27`, `3.14`, `-1.2`, `1.0`, `2.` (2.0), `.5` (0.5); |
| 12 | +* real numbers written in exponential (base 10) notation <em style="color: #0000ff;">mantissa</em>e<em style="color: #ff00ff;">exponent</em>, oznaczające <em style="color: #0000ff;">mantissa</em> × 10<sup><em style="color: #ff00ff;">exponent</em></sup>: `-5e3` (-5000), `2.5e-2`</span></tt>` (0.025); |
| 13 | +* complex numbers in which we write both components as real numbers, and denote the imaginary part by adding `j` at the end: `1.5-0.2j`, `-2+1e-1j` (-2+0.1j); |
| 14 | + |
| 15 | +### Text types |
| 16 | + |
| 17 | +Strings between one of the following sets of quotation marks: |
| 18 | + |
| 19 | +* single quotation marks: `'...'`, |
| 20 | +* double quotation marks: `"..."`, |
| 21 | +* single or double quotation marks repeated three times: `'''...'''`, or `"""..."""`. |
| 22 | + |
| 23 | +In the latter case, the text may span several lines. In the first two it is not allowed, however newlines can be inserted using the special symbol `\n`. |
| 24 | + |
| 25 | +You can use any set of the above symbols to denote text strings, but it is very important that each string begins as it ends. The valid examples are (red indicates the beginning and ending quotation marks): |
| 26 | + |
| 27 | +```python |
| 28 | +"Ala ma kota" |
| 29 | + |
| 30 | +'This chapter is named "Constants"' |
| 31 | + |
| 32 | +"""First line |
| 33 | +Second line""" |
| 34 | + |
| 35 | +"Here are\ntwo lines too" |
| 36 | +``` |
| 37 | + |
| 38 | +### Tuples |
| 39 | + |
| 40 | +Tuples are an ordered set of values constant. Successive values are separated by commas, and the whole tuples should be enclosed in parentheses. More about using tuples will follows in this course later. Examples of tuples are: |
| 41 | + |
| 42 | +```python |
| 43 | +(1, 2, 5) |
| 44 | + |
| 45 | +(2.5, 3, 1j, 'tekst') # tuples' elements don't have to be of the same type |
| 46 | + |
| 47 | +("tekst", (1, 2)) # tuples can contain other tuples (and other types, with which we haven't mentioned yet) |
| 48 | + |
| 49 | +(1,) # a one-element tuple must have an extra comma (to distinguish it from the number in parentheses) |
| 50 | + |
| 51 | +() # empty tuple |
| 52 | +``` |
| 53 | + |
| 54 | +More on the use of tuples and how to extract their individual elements will be presented later in this lecture. |
| 55 | + |
| 56 | +## Operations on values |
| 57 | + |
| 58 | +You can act on the values supported by Python using mathematical operators. The basic operators are `+`, `-`, `*` (multiplication), `/` (division), `**` (power), `//` (integer division), `%` (division remainder). Python can perform operations according to the applicable mathematical rules (the use of parentheses is allowed). To read them, start the Spyder program and enter the following expressions in the interactive console (it is not necessary to use the `print` function - the interactive shell will automatically print the result of the calculated expression): |
| 59 | + |
| 60 | +```python |
| 61 | +1 + 2 |
| 62 | + |
| 63 | +2 + 2 * 2 |
| 64 | + |
| 65 | +(2 * (1 + 1))**2 |
| 66 | + |
| 67 | +2.5 * 2 |
| 68 | + |
| 69 | +7.5 / 3 |
| 70 | + |
| 71 | +5 / 2 |
| 72 | + |
| 73 | +4 / 2 |
| 74 | + |
| 75 | +8 // 3 |
| 76 | + |
| 77 | +8 % 3 |
| 78 | + |
| 79 | +2**10 |
| 80 | + |
| 81 | +8**(1/3) |
| 82 | + |
| 83 | +(-1)**0.5 |
| 84 | + |
| 85 | +'1' + '2' |
| 86 | + |
| 87 | +(1, 2) + (3,) # notice comma in (3,) |
| 88 | + |
| 89 | +3 * "a" |
| 90 | + |
| 91 | +(1, 2) * 2 |
| 92 | +``` |
| 93 | + |
| 94 | +I also suggest trying this interactive Python shell yourself as a calculator. |
| 95 | + |
| 96 | +A few of the above examples should be discussed in detail. The division operator (`/`) on real or integer numbers will always return the real result (even if the result is an integer). In turn, the integer division operator (`//`) will remove the fractional part - for this reason, it has limited use in engineering and scientific applications. |
| 97 | + |
| 98 | +The exponentiation operator (`**`) can also raise to a non-integer power. Therefore `(-1)**0.5` is treated as the principal root of `-1`, i.e. the imaginary number `1j`. It is shown as `(0 + 1j)`, or something like `(6.123233995736766e-17+1j)`. Do not be afraid of this: according to the previously described exponential notation, the real part of this number is in the order of 10<sup>-17</sup>, which is practically 0. Floating point computer calculations are not 100% accurate, hence the result (accuracy of calculations is a separate issue on which you need to pay attention to when writing calculation programs, but at this stage we will not deal with it). |
| 99 | + |
| 100 | +It is a good practice to write spaces around the mathematical operators, as it significantly increases the readability of the code (of course, this is not a formal requirement, but omitting spaces frequently makes it very difficult to analyze the program, especially when it is written by someone else). |
| 101 | + |
| 102 | +Python can also work on non-numeric types as long as they make sense. Adding text strings or tuples together combines them, and multiplication by a whole number repeats their contents a given number of times. |
| 103 | + |
| 104 | +For actions that don't make sense, Python will throw an error. Please try the following in the console and read and understand the error message each time: |
| 105 | + |
| 106 | +```python |
| 107 | +1 / 0 |
| 108 | + |
| 109 | +1 + "2" |
| 110 | + |
| 111 | +2.5 * (1, 2) |
| 112 | + |
| 113 | +"Alala" - "la" |
| 114 | + |
| 115 | +50.0 ** 1000000 |
| 116 | +``` |
| 117 | + |
| 118 | +The last operation is mathematically and logically correct, however, it results in too large number that cannot be correctly stored in the computer's memory as a real number. |
| 119 | + |
| 120 | +## Converting types |
| 121 | + |
| 122 | +All the constants of a specific type described above (as well as other types, not yet shown) have names. For the types already described, they are as follows: |
| 123 | + |
| 124 | +* integer numbers: `int` |
| 125 | +* real numbers: `float` |
| 126 | +* complex numbers: `complex` |
| 127 | +* text strings: `str` |
| 128 | +* tuples: `tuple` |
| 129 | + |
| 130 | + |
| 131 | +For any value you can check its type using the type function, for example: |
| 132 | + |
| 133 | +```python |
| 134 | +type(2) |
| 135 | + |
| 136 | +type(2.5) |
| 137 | + |
| 138 | +type(2j) |
| 139 | + |
| 140 | +type("2") |
| 141 | + |
| 142 | +type(1 + 2) |
| 143 | + |
| 144 | +type(1 + 2.0) |
| 145 | +``` |
| 146 | + |
| 147 | +As you can see above, Python is able to convert numeric types by itself so that the result of a given action makes sense. However, it is possible to force type conversions manually by directly using their names as functions. This is especially useful when you want to convert a string to a number. For example: |
| 148 | + |
| 149 | +```python |
| 150 | +float("2.5") |
| 151 | +``` |
| 152 | + |
| 153 | +Please see what is the difference between: |
| 154 | + |
| 155 | +```python |
| 156 | +"1" + "2" |
| 157 | +``` |
| 158 | + |
| 159 | +and |
| 160 | + |
| 161 | +```python |
| 162 | +int("1") + int("2") |
| 163 | +``` |
| 164 | + |
| 165 | +and |
| 166 | + |
| 167 | +```python |
| 168 | +str(1) + str(2) |
| 169 | +``` |
| 170 | + |
| 171 | +If the conversion doesn't make sense, Python will throw an error. Please try |
| 172 | + |
| 173 | +```python |
| 174 | +int(10+2j) |
| 175 | + |
| 176 | +float("dwa") |
| 177 | + |
| 178 | +float("two and half") |
| 179 | + |
| 180 | +int("one") |
| 181 | + |
| 182 | +int("4O") # can you notice a problem here? |
| 183 | +``` |
| 184 | + |
| 185 | +The exception is the conversion of a real number to an integer. The expression |
| 186 | + |
| 187 | +``` |
| 188 | +int(2.7) |
| 189 | +``` |
| 190 | + |
| 191 | +will simply discard the fractional part. |
| 192 | + |
| 193 | + |
| 194 | +<hr /> |
| 195 | + |
| 196 | +Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license. |
0 commit comments