Hook
More breakout videos from this creator.
The best way to make a computer do something useful is with a programming language which uses abstraction so that instead of this you can write code that looks like this which is then converted into machine code for you. Some languages like Python use an interpreter which directly tries to execute source code line by line. Other languages like C or Go use a compiler which converts the entire program into machine code before putting it in a file the CPU can execute. Now every programming language has different syntax, but there's some basic tools almost all of them have. The most basic way to use data is with variables which assigns a value to a name which can then be reused and modified depending on the value. Variables can have different data types. For text they're single characters, or strings of multiple characters. For numbers, there's integers which can also be signed and floating point for decimal values. They're called floating point because the decimal point can float around to trade off precision with range. This is possible because they use scientific notation. It's some number times 10 to the power of something, telling you where to put the decimal point. Which is exactly what they look like under the hood. The actual number is stored with binary fractions. Some fractions like 1/3 can only be approximated in binary with an infinite sum. But since memory is not infinite you have to cut it off at some point which leads to rounding errors, causing pretty weird calculations sometimes. If these are not enough, long/double use twice the amount of memory to double the range of int/float. Python automatically figures out which type a variable is, but in a language like C you have to explicitly declare the type of a variable. The value of a variable is stored at an address in memory. Pointers are variables whose value is the memory address of another variable, denoted by this ampersand. So a pointer is just a chunk of memory pointing to another chunk of memory. A memory address is just a number. You can add and subtract from it to navigate through individual bytes of memory. This is called pointer arithmetic. In low-level languages like C you have to manually allocate and free memory once it's no longer used. This all happens in the "heap" which is a part of memory that can dynamically grow and shrink as the program demands, which allows for more control but makes it incredibly easy to completely break your code. You could touch memory you're not supposed to or memory that simply doesn't exist, as a segmentation fault. But also if there's some chunk of memory that's no longer used and you forget to free it, you have no way to access it anymore. That memory is no longer usable and can cause the program to slow down and eventually crash. This is called a memory leak. To avoid this mess, high-level languages like Python have built-in garbage collectors that manage memory for you. Different data types take up a different amount of memory. Integers are most often 4 bytes of memory. A single character is just 1 byte of memory. A string is just multiple character bytes with a null character to signal the end of the string. Storing multiple items in a contiguous chunk of memory is the idea of an array. More generally, it's a list of items with the same data type, with each item having a numerical index, most often starting at 0. Since the items are next to each other in memory, by knowing the address of the first item, you can quickly index to any item in the array by using pointer arithmetic. An array is what's known as a data structure, a way to organize data to make it easier to work with. Retrieving values from an array is blazing fast, but the size of an array is often fixed when creating it. So when it's full you can't add anything, and if you don't use it all, it's just wasted memory. So a more flexible option is a linked list. It uses nodes containing a value and a pointer to the next node, which allows them to be spread apart in memory. Also it can grow and shrink dynamically as you add or remove any node. You can reorder it by simply rearranging the pointers. But they can be impractical as you have no way to access the last node except to traverse every single one before it. But still, both arrays and linked lists are useful as they allow you to create queues and stacks.