What I Learned From Stanford’s CS101

Sachin Bhutekar
The Startup
Published in
18 min readNov 9, 2020

--

Source: CS101 on Edx

CS101 is Stanford University’s Introduction to Computer Science for a zero-prior-experience audience, taught by Professor Nick Parlante and available for anyone to take online.

I had left the job to make my career in the IT Industry and I took this course to begin the same after a recommendation from my mentor Aishwarya. I am aspiring to be a Data Scientist and thought it's better to start with basics. I had so many questions regarding the fundamental working of computers and CS101 lived up to all of my expectations in answering those questions in a very simple manner.

Overview

Split over 6 weeks, it's a self-paced online course that teaches the essential ideas of Computer Science for a zero-prior-experience audience. Using short bits of “computer code” written in a variant of ‘Javascript’, it teaches the basic concepts of strength and limitations of computers. It covers all fundamentals topics related to the computer like

  • Computer: How it works?
  • RGB Scheme & Image Codes
  • Tables & Table Codes
  • Spreadsheets & Digital Media
  • Memory, Network & Signals
  • Computer Hardware, Software & Security.

They are pretty basic, that's what I was looking for.

What did I learn?

WEEK 1 :

Fundamental Equation: Computer = Powerful + Stupid

Credit: www.themindquotes.com

The computer is not a magic box but an understandable machine. It's powerful because it can perform billions of operations per second and look through masses of data but it is also stupid since it lacks something which we called ‘insight’ or ‘understanding’ and need humans to tell them how to work. The computer is driven by instructions which are very simple and mechanical, e.g. add 2 numbers. The computer executes or runs the instructions, one by one, from top to bottom in a purely mechanical way.

Although computer executes such simple instructions mechanically, it offers a wonderful feature like audio, video because the computer programmers design algorithms to achieve these features. The algorithm means steps to accomplish a certain task. Further breaking it down into instructions, they write code for the computer.

Code: Language of Computer

Code is the language that a computer can understand. Code consists of instructions to the computer which are like bricks, individually simple but when combined together bring up great combinations. Also, the computer knows the meaning of very few words and that only can be used in code.

Syntax: Expression of Computer’s Stupidity

Code can’t be written in free form and we need to follow some grammatical rules while writing the code, called syntax. It is code text structured for the computer. Syntax of the code is very limited and strict and its violation leads to error or computer just don't understand the instructions. The strict syntax of code is the reflection of the inner, mechanical/dumb nature of the computer.

Pixel & Megapixel

Credit: https://www.figma.com

If we zoom digital image by 10 times, we see the image on the computer screen is made up of small squares called pixels. Pixel is a small square that shows a single colour. Quality of image is measured in Megapixels which means 1 million pixels. If you have a picture having a length of 800 pixels and width 600 pixels, then its quality is 800*600=480000 pixels which are approximately 0.5 MP. There is an x-y coordinate system to identify every pixel uniquely. For eg: the pixel in the top left corner has x=0 and y=0, so it is pixel (0,0) or just (0,0)

Red-Green-Blue (RGB Scheme)

We use a Red-Blue-Green (RGB) scheme where we mix light of these three colours in different proportion to make any colour represented by the pixel (remember Newtons’ prism experiment from school days). The intensity of each colour is represented by the number between 0 to 255 with 0 meaning zero light and 255 meaning maximum light. So if we want to make pixel (2,0) dark yellow, then we will set Red to 255, Green to 255 and blue to 0 or (255,255,0) for that pixel (2,0).

Credit: http://smilebasic.com

Week 2

CS101 uses a variant of Javascript to write small code snippets. Javascript code can be run on any browser.

Hello World! : The First Code

We use a function called print() to display something on the computer screen. Anything written in parenthesis is displayed on the screen. We can display a number or string on the computer screen. The string is a sequence of characters or letters and must be written in quotes. Every statement in the code must be ended by a semicolon (;). If we have two or more numbers or strings in parenthesis they must be separated by comma (,).

Following image shows code on the left while its output displayed on the right whenever we run or execute the code. Run executes each line of code once, running from top to bottom

Variable

A variable is a name given to the memory location in computer memory. A “variable” is like a box that holds a value. For eg: x = 7; Using = in this way is called “variable assignment”. This stores the value 7 into the variable (i.e. box) x. Now wherever we use x in the code, the computer will use the value stored in it. A variable can hold any type of value like a number, string or image or any other file.

In the following example, we created variable x and assigned different values to it which were printed by print() function.

Image Codes

As we know that each image has pixels and we can change the colour of pixels by changing the RGB values (R, G, B) for specific pixel (x,y). Image codes are the code written to manipulate or perform a different operation on images.

In the following code, we use function new SimpleImage() to load image “flower.jpg” into a variable image. Then we print the image using the print() function.

Image for() Loop

In Programming, there is the concept of the loop which we use to repeat some piece of code several times. The For () loop takes a few lines of our code and runs those lines again and again, once for each pixel in the image. It is called bulk operation. Also, we use functions pixel.setRed(), pixel.setGreen() and pixel.setBlue() to set value or intensity of Red, Green and Blue light for the given pixel.

In the following code, For loop visits, each pixel of the image and sets the value for Red, Green to 255 & Blue colour light to 0 and thus the picture turns to yellow since every pixel in the image has become yellow.

Greyscale Image.

Whenever the intensity of all three colour lights is equal, the pixel colour is grey. In the following code, we have used pixel.getRed() function for taking the present values of red colour and respective function for other two colours for the given pixel and then we calculate the average light intensity of that pixel and set all three colours value to average intensity in order to make the image look grey. This is called Greyscale Image.

Image Logic

The if () structure provides the ability to write a true/false test to control if a bit of code runs or not. Combined with the loop, the if-statement will greatly expand what we can do with code. The condition to be checked is written in parenthesis and code is written after test condition in curly ybraces{}. The computer checks the condition in parenthesis, if it's true then executes the code in curly braces otherwise skips the code. Using if statement we can perform an operation on specific pixels of the image.

The following code where we have set value for Red & Green light to 0 and blue to 255 only for those pixels whose Green value is less than 140. You can observe that the green pages of leaves have turned to blue.

Week 3

Credit: Basics Explained

Bits, Bytes & Memory

Human beings use the decimal system where we have 10 digits from 0 to 9, each representing a specific number of things. The Computer knows only two states which are represented by 0 & 1(Why? Watch this.) Computer breaks down everything to 0s & 1s and this system where the base of the system is 2 called Binay System. The smallest measuring unit of the computer memory is called Bit and it can store only 1 digit i.e. 2¹=2 values, either 0 or 1. Group of 8 bits is called Byte and it can store 2⁸=256 values from 0 to 255 (The reason why we have 0 to 255 value to colours in RGB Scheme). Following are the larger units of computer memory.

Credit: Aone Computer Education Center Youtube

Computer Hardware

“Hardware” refers to the physical parts of the computer, and “software” refers to the code that runs on the computer. For eg. piano is like hardware, whereas the music on the piano is the software.

  • Transistor
    It is an electronic device that controls the flow of an electric current, most often used as an amplifier or switch. It is a basic building block used to construct more complex electronic components. In particular, a “bit” can be built with an arrangement of 5 transistors. (How? Click here). Transistors replaced vacuum tubes. Microscopic transistors are etched onto fingernail-sized silicon chips. Chips can contain billions of transistors. e.g. CPU chips, memory chips, flash chips
Credit: www.quora.com
  • Moore’s Law
    Moore’s law states that the density of transistors on a chip doubles about every 2 years or so (sometimes listed as every 18 months)
    . It is not a scientific law, just a broad prediction that seems to keep working. More broadly it tells that for the same cost, the computer is becoming more and more capable or efficient or It could mean that for the same capability, computers get cheaper and cheaper .. showing up in more and more places as it becomes cost-effective (e.g. in your car, your thermostat, ..).
  • Central Processing Unit (CPU)
    CPU is called the “brain” of the computers. The CPU does the active “running” of code, manipulating data, while the other components have a more passive role, such as storing data. When we say that a computer can “add two numbers, a billion times a second” .. that’s the CPU. When you hit the Run button, the CPU ultimately “runs” your code.
Credit: https://web.stanford.edu
  • RAM (Temporary Memory)
    RAM stands for Random Access Memory. RAM is the working memory the computer uses to store code and data that are being actively used. RAM is effectively a storage area of bytes under the control of the CPU. RAM is relatively fast, able to retrieve the value of any particular byte in a few nanoseconds (1 nanosecond is 1 billionth of a second). The other main feature of RAM is that it only keeps its state so long as it is supplied with power and loses all data as soon as the power supply is stopped i.e it is volatile memory.
  • Persistent Storage (Hard Disk)
    Persistent storage — long term storage for bytes as files and folders
    . Persistent meaning that the bytes are stored, even when power is removed. A laptop might use a spinning Hard Disk Drive (HDD) also known as “hard disk” for persistent storage of files. Or it could use a “flash drive”, also known as a Solid State Disk — SSD, to store bytes on flash chips. The hard drive reads and writes magnetic patterns on a spinning metal disk to store the bytes, while the flash is “solid-state” has no moving parts, just silicon chips with tiny groups of electrons to store the bytes. In either case, the storage is persistent, in that it maintains its state even when the power is off.
Credit: https://www.backblaze.com/blog/ssd-vs-hdd-future-of-storage/

Week 4

Computer Software

“Software” is the code which runs on the hardware. If the hardware is a piano, then the software is the music which is being played on it. The code is also referred to as “program”. You run software on your computer to solve a particular problem or to manage information. We know that computer only understands the language of 0s & 1s which is also called as Machine Language. The code written in machine language is called Machine code and it has a name ending with extension .exe (“.exe” is a windows convention to mark a file as a program). So when we say software, it’s actually .exe file which runs on the computer.

The machine code defines a set of individual instructions. Each machine code instruction is extremely primitive, such as adding two numbers or testing if a number is equal to zero. When stored, each instruction takes up just a few bytes. When we said earlier that a CPU can execute 2 billion operations per second, “operations” there refers to these simple machine code instructions.

Credit: https://web.stanford.edu

In our Persistent storage, the program is stored as .exe file. When we double click on the program, Operating system “launches” the program, allocates an area of memory within RAM for the program, loading the first section of the program’s machine code into that memory, and finally directing the CPU to start running that code. RAM is under CPU control.

CPU runs a “fetch/execute cycle”

  • fetch one instruction in sequence
  • execute (run) that instruction, e.g. do the addition
  • fetch the next instruction, and so on till the last instruction.

Operating System (OS)

The “operating system” of a computer is like a first, supervisory program that begins running when the computer first starts up (“boots up”). Operating System is like the manager of Computer which manages all resources and supervises all the programs running on the computer. The operating system keeps things organized in the background so that multiple programs can run at the same time, which is known as “multitasking”. The operating system gives each program its own area of memory, so each program only accesses its own resources.

Programming Languages.

We know that CPU can run only machine code instructions written in 0s & 1s, but it's very difficult for programmers to write machine codes. Hence programmers write code in “High Level” computer language which is easy to understand for them and has features like for loop if statement etc. Such a program written in High-Level Programming language is called Source code which is then subsequently converted to machine code.

  • Compiled Languages
    There is a group of programming languages including C, C++ which use the compiler to convert source code to machine code. A compiler is a language-specific software which reads the source code and translates and expands it to a larger sequence of the machine code instructions to implement the sequence of actions specified by the source code. Here memory management is partially manual i.e some input is required from the programmer side but compiled code runs faster.

    A compiler translates all the source code into equivalent machine code
    to be run later, it is a bulk translation. For eg: an if-statement, can be implemented by a sequence of 5 machine code instructions, then compiler if statement into 5 machine code instructions. The compiler produces .exe file after compilation, which can be distributed and run independently without source code or compiler, after any amount of time from the compilation. It’s not generally possible to generate source code from the .exe file.
Credit: https://www.guru99.com/
  • Interpreted (Dynamic) Languages
    There is another group of languages like Python, Javascript which uses Interpreter instead of Compiler. An interpreter looks at each line of code, and translates and runs it at the moment, and then proceeds to the next line of source code. The interpreter does not produce a .exe file, instead, it performs the actions specified in the source code directly.
    In interpreted languages, memory management is automatic and it offers more features than compiled languages but interpreted code runs slower than compiled code.

Computer Network

Computers are connected to each other via Computer network. A computer network is like a phone system for computers, sharing data is like making a call. LAN (local area network) is technology connecting 2–50 computers in a house or on one floor of a building. A wired LAN is called ethernet whereas wireless LAN is called Wi-Fi.

  • Ethernet
    Ethernet uses a co-axial cable to connect Computers in a LAN to each other. The wire is often of Blue or Yellow colour and its length is maximum 100 m. Ethernet cables’ one end is connected to the computer while another end is connected to a device called Router which connects Ethernet to a larger network called the Internet. Ethernet cable used RJ45 connector to connect to computer or Router.
    Wifi is the same as ethernet except connecting medium is air and not cable.
Credit: https://www.edrawsoft.com/
  • Internet
    Ethernet or LAN is confined to a house or building but the Internet is a worldwide network. Each computer and router has a unique address called IP Address which internet used to send data. Internet is a network of Routers all over the world and each computer is connected to some router.

    The most common way for a computer to be “on the internet” is to establish a connection with a “router” which is already on the internet. The computer establishes a connection via, say, ethernet to communicate data with the router. The router is “upstream” of the computer, connecting the computer to the whole internet. The Data is shared in hops (jumps)from one router to other router and finally to the destination. Each router does not need to know the whole path but just the correct neighbour whom data is needed to pass.
Credit: https://web.stanford.edu

Week 5

Table Data

Credit: https://web.stanford.edu

Table data is a common way to organize strings, numbers, dates in rectangular table structure. The table is made of Rows & Fields. A number of fields will always be small (categories) but the number of rows can be millions or billions. In the above figure, file baby-2010.csv contains names of top 1000 boy and girl names, 2000 names total organized as a “table”. Here name, rank, gender and year are fields while rows named Jacob, isabella etc contain data of babies born in 2010. The rectangular table format is very common and “Databases” are an extension of this basic table idea.

Table Codes

Code that works on tables is similar to image codes which we saw earlier. Recall we had for (pixel: image) for images, similarly, we have for (row: table) which will visit every row in the table and perform operation written in the curly braces {}. print(row) will print the rows of the table. Also, we have used function new SimpleTable() to load table file “baby-2010.csv” into a variable table.

Following code prints all the rows of the table file.

We use row.getField(field_name) to pick field out of the row. In our file we have field names name, rank, gender, year. We use (==) operator to check whether two expressions on its both side are equal or not, its different than assignment operator which assigns the value on its RHS to variable or expression its LHS. Following code, we will print all the rows having “Alice” from the “Name‘“ field.

Credit: www.stanford.edu
  • startsWith()

The function startsWith() when used with getField() function, checks whether data in given field starts with letter written in its parenthesis(). Following code will check and print all the names from the “name” field starting with “Ab”.

  • endsWith()

Similarly, the function endsWith() will check all the data in the given field ending with letters written in its parenthesis. Following code will check and print all the names from the “name” field ending with “ly”.

Boolean Logic

Boolean logic constitutes the tests which are used to combine two statements. We can use AND (&&) operator represented by two ampersand symbols when we want to execute code only when both given conditions are satisfied.

In Following code, We used AND (&&) operator to print names starting with “B” and ending with “a”.

We can use OR (||) operator represented by two bar symbols when we want want to execute code even if any one of the two or both two given conditions are satisfied.

In Following code, We used OR (||) operator to print names staring with either “X” or “Y”.

Week 6

Spreadsheets

A spreadsheet is a file that made of cells in rows and columns and can help arrange, calculate and sort data. Data in a spreadsheet can be numeric values, as well as text, formulas, references and functions. One of the most popular examples of spreadsheets is Microsoft’s Excel. Columns in spreadsheets are named as A, B, C, D… and rows are named as 1, 2, 3, 4…. Thus every cell in the spreadsheet has a unique address.

Credit: https://web.stanford.edu

Also, there are functions like SUM, AVERAGE etc in spreadsheets which makes our mathematical calculation easy and fast. Also, we have options to represent our data in the forms of graphs, charts, tree diagrams etc.

Computer Security

Our computer is like a castle and our data is stored inside it. The bad guy cannot just access files (bytes really) stored inside the computer at will Bad guy will and he will need to work at it. Generally, the bad guy is not crafting attack especially for you. They send in the millions rather crude attacks, just snaring the most clumsy victims. Following are the three common types of attacks on our computer security.

  1. Password Attacks
    The bad guy could try to guess your password on a site and will try to log in again and again. This works if the password is common, e.g. “password” “password123”. It is also known as “dictionary attack”, where he tries all the words in a dictionary. This fails mostly, but success here and there with an account with a poor password is good enough for the bad guys.

    So in order to safeguard from such attack,
    ★ one should not choose such a password which a thousand others have chosen.
    ★ A good password is a combination of lowercase & uppercase letters, specials symbols, numbers and misspellings.
    ★ In case, the site itself is compromised, so the bad-guy possibly gets your password that way? Therefore, do not re-use your passwords across important sites like banks.
    ★ Also one should consider writing down important passwords on a slip of paper at home. Otherwise, it’s hard to keep it all straight in your head.
  2. Phishing Attacks
    In a Phishing attack, bad guy tricks you into exposing your password or whatever info he needs. We receive so many emails or links on the name of e-commerce sites or banks and email might say something provocative — “fraud alert, click immediately” and if we click on it then we will get directed to an alternate phishing web site that imitates the real site. When we type in a password to either of these, it goes to the bad guy. Bad guys email out or in some other way distributes the URL to a phishing site. Bad guys want passwords for sites that have something to do with money.

    In Order to safeguard our computer from malware attacks,
    ★ Don’t trust URLs in emails or random sites, especially when leading to a login page
    ★ Scrutinize the URL as shown in your browser or email program. It will be not exactly similar to the original site.
    ★ Best is to type the URL in yourself — if it claims to be from eBay, type in www.ebay.com yourself in your browser.
    ★ Look for https in the URL, where ‘s’ stands for secure.
  3. Malware Attacks
    This is a big category, where the bad guy tricks us into running bad software (malware including worms, viruses, trojans etc) on our computer. If the bad guy gets you to run bad guy authored code on your computer, the computer is compromised, the bad guy wins. The code can take actions, control your computer, erase or transfer data etc.

    In Order to safeguard our computer from malware attacks,
    ★ Don’t run programs from random sources (google it first, see what people say)
    ★ Your web-facing software like browsers are vulnerable to such attacks, keep them updated with the latest versions.

Conclusion

This course was a wonderful journey where I got to learn so many things which I never knew before. I will advise everyone who wants to make their career in Computer Science to take this course. Thank You very much for your patient reading. Leave a comment below if you have any queries, I will be glad to answer them. You can connect with me on LinkedIn or by visiting my website on Medium. Happy Learning.

--

--