|
| 1 | +--- |
| 2 | +description: Part 1 of Built-in Functions |
| 3 | +--- |
| 4 | + |
| 5 | +# Built-in Functions Part 01 |
| 6 | + |
| 7 | +## What are built-in functions? |
| 8 | + |
| 9 | +A function is a reusable block of code that performs a specific task. You give it some input |
| 10 | +(sometimes), it does something with it, and it gives you back a result. Python has many built-in |
| 11 | +functions which you can use to make your life easier. In this section, we will take a look at |
| 12 | +`print`, `input` and some type-casting functions in detail for this chapter. |
| 13 | + |
| 14 | +## `print()` function |
| 15 | + |
| 16 | +The `print` function is used to show text on the screen (also known as the terminal or standard |
| 17 | +output). With this, you can write your first program that every programmer writes when they learn a |
| 18 | +new language: |
| 19 | + |
| 20 | +```python |
| 21 | +print("Hello World!") |
| 22 | +``` |
| 23 | + |
| 24 | +When you run this program, it should show `Hello World!` in your terminal. We are not limited to |
| 25 | +only strings, we can even print out other kinds of data types! |
| 26 | + |
| 27 | +```python |
| 28 | +print("My name is Alex") |
| 29 | +print(56) |
| 30 | +print(3.14) |
| 31 | +print(False) |
| 32 | +``` |
| 33 | + |
| 34 | +We can also print variables, this prints out the data present within them. |
| 35 | + |
| 36 | +```python |
| 37 | +age = 43 |
| 38 | +print(age) |
| 39 | + |
| 40 | +language = "python" |
| 41 | +print(language) |
| 42 | +``` |
| 43 | + |
| 44 | +The output of the code above will be: |
| 45 | + |
| 46 | +```text |
| 47 | +43 |
| 48 | +python |
| 49 | +``` |
| 50 | + |
| 51 | +Each `print` statement prints on a new line. You can print multiple things using a single `print` |
| 52 | +statement by separating them using commas (`,`), which will result in all the values being printed |
| 53 | +on the same line separated by spaces. |
| 54 | + |
| 55 | +```python |
| 56 | +print("Value of pi is", 3.14) |
| 57 | +``` |
| 58 | + |
| 59 | +This allows us to also print out variables in the same line too. |
| 60 | + |
| 61 | +```python |
| 62 | +name = "Radagon" |
| 63 | +money = 30 |
| 64 | +print(name, "has", money, "dollars to his name.") |
| 65 | +``` |
| 66 | + |
| 67 | +This will print out `Radagon has 30 dollars to his name.` |
| 68 | + |
| 69 | +!!! note |
| 70 | + |
| 71 | + Keep in mind that the comma separation of values only work in the `print` function. |
| 72 | + |
| 73 | +## `input()` function |
| 74 | + |
| 75 | +The `input` function is used to take input from the user through the terminal. It returns everything |
| 76 | +the user types until the user hits the ++enter++ key. You can also pass in a string to the function |
| 77 | +which will appear as a prompt before asking for input, like so: |
| 78 | + |
| 79 | +```python |
| 80 | +input("Enter your name: ") |
| 81 | +``` |
| 82 | + |
| 83 | +The above code will print `Enter your name: ` followed by a blinking cursor where you can type the |
| 84 | +actual input. Now, to actually use the value entered by the user, you need to assign the value |
| 85 | +returned by the `input` function to a variable. |
| 86 | + |
| 87 | +```python |
| 88 | +name = input("Enter your name: ") |
| 89 | +print("Hello", name, "! Hope you have a wonderful day.") |
| 90 | +``` |
| 91 | + |
| 92 | +The output of this will be: |
| 93 | + |
| 94 | +```text |
| 95 | +Enter your name: Krish |
| 96 | +Hello Krish ! Hope you have a wonderful day. |
| 97 | +``` |
| 98 | + |
| 99 | +!!! note |
| 100 | + |
| 101 | + You must keep in mind that the `input` function will _always_ return the user input as a string. |
| 102 | + |
| 103 | +## Type Casting |
| 104 | + |
| 105 | +Type casting is the process of converting one data type to another. Remember the note from above? |
| 106 | +> You must keep in mind that the `input` function will _always_ return the user input as a string. |
| 107 | +
|
| 108 | +There will be scenarios where you need the input function to return a data type other than a string. |
| 109 | +One such place could be when you want to make a calculator that adds two numbers. Let's take a look |
| 110 | +at how we would do it without any type casting. |
| 111 | + |
| 112 | +```python |
| 113 | +num1 = input("Enter the first number: ") |
| 114 | +num2 = input("Enter the second number: ") |
| 115 | +result = num1 + num2 |
| 116 | + |
| 117 | +print("The sum of", num1, "and", num2, "is", result) |
| 118 | +``` |
| 119 | + |
| 120 | +If we run this program and enter two numbers, this is the result we would get: |
| 121 | + |
| 122 | +```text |
| 123 | +Enter the first number: 23 |
| 124 | +Enter the second number: 12 |
| 125 | +The sum of 23 and 12 is 2312 |
| 126 | +``` |
| 127 | + |
| 128 | +As we can see, `23 + 12` is clearly _not_ supposed to be 2312. So what's happening here? |
| 129 | + |
| 130 | +The variables `num1` and `num2` hold a string data type of the user input. When you perform an |
| 131 | +addition operation on two strings (`result = num1 + num2`) you are simply joining two strings |
| 132 | +together, side-by-side. This is also called string concatenation which we will learn more about in |
| 133 | +the next chapter. |
| 134 | + |
| 135 | +This is where type casting comes to fix this problem. We need to convert the string data type to |
| 136 | +integer so the computer can follow regular arithmetic for our use case. |
| 137 | + |
| 138 | +```python |
| 139 | +num1 = input("Enter the first number: ") |
| 140 | +num2 = input("Enter the second number: ") |
| 141 | + |
| 142 | +int_num1 = int(num1) |
| 143 | +int_num2 = int(num2) |
| 144 | + |
| 145 | +result = int_num1 + int_num2 |
| 146 | + |
| 147 | +print("The sum of", num1, "and", num2, "is", result) |
| 148 | +``` |
| 149 | + |
| 150 | +Running it now gives the correct result: |
| 151 | + |
| 152 | +```text |
| 153 | +Enter the first number: 23 |
| 154 | +Enter the second number: 12 |
| 155 | +The sum of 23 and 12 is 35 |
| 156 | +``` |
| 157 | + |
| 158 | +`int_num1 = int(num1)` and `int_num2 = int(num2)` is what we call type casting. We're casting the |
| 159 | +input string to an integer, then assigning it to a variable. (Assigning the cast value to a separate |
| 160 | +variable is optional and can be omitted.) |
| 161 | + |
| 162 | +Another common way to write the code above is: |
| 163 | + |
| 164 | +```python |
| 165 | +num1 = int(input("Enter the first number: ")) |
| 166 | +num2 = int(input("Enter the second number: ")) |
| 167 | + |
| 168 | +result = num1 + num2 |
| 169 | + |
| 170 | +print("The sum of", num1, "and", num2, "is", result) |
| 171 | +``` |
| 172 | + |
| 173 | +Both the programs perform the same way, with the only difference being that we aren't creating an |
| 174 | +extra variable in the latter. Whenever you see functions being executed within another function: |
| 175 | + |
| 176 | +```python |
| 177 | +num1 = int(input("Enter the first number: ")) |
| 178 | +``` |
| 179 | + |
| 180 | +Remember that the innermost function is the one that always executes first. In this case, the |
| 181 | +`input` function is the one that fires first. It waits for the user to give an input. After an input |
| 182 | +is given, the data transfers to the `int()` function - converting the string data to an integer (if |
| 183 | +the input is valid). Finally, the data gets assigned to the variables. |
| 184 | + |
| 185 | +The example above shows you how to convert user input to an integer. In case you want to use |
| 186 | +decimals instead of integers, you can type cast it to `float`. |
| 187 | + |
| 188 | +```python |
| 189 | +num1 = float(input("Enter the first number: ")) |
| 190 | +num2 = float(input("Enter the second number: ")) |
| 191 | + |
| 192 | +result = num1 + num2 |
| 193 | + |
| 194 | +print("The sum of", num1, "and", num2, "is", result) |
| 195 | +``` |
| 196 | + |
| 197 | +Similarly, we can cast data to other types, such as strings and booleans, by calling their |
| 198 | +respective functions. |
| 199 | + |
| 200 | +| Data Type | Function Call | |
| 201 | +| ---------- | ------------- | |
| 202 | +| String | `str()` | |
| 203 | +| Integers | `int()` | |
| 204 | +| Floats | `float()` | |
| 205 | +| Boolean | `bool()` | |
0 commit comments