<a target="_blank" href="https://drive.google.com/file/d/11BkaBWM0BOq9LOVQnrunmV32ZjHrincp/view?usp=sharing"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a> <br /> # Intro to programming ## Getting started Programming is how to get your computers to solve problem. Computers are tools to help you to be more productive. ![](https://i.imgur.com/kKft6fA.png) And this is problem solving in computer science. You have some input, which is data, and you want the output which is the result of the solution to your problem. In between that input and output is the code that you write to define the solution which called **algorithm**. Unfortunately, computers don’t understand languages like English or Spanish, so we have to use a **programming language** they understand to give them instructions. ![](https://i.imgur.com/ajRaY4i.png) In this course you will be programming using a language called **Python**. This is a small Python program which asks the user to enter their name and says “Hi” to them: ```python print("Hello and welcome.") name = input("Whats your name?") if name == "Minh": print("Thats my name too!") print("Hi " + name) ``` The Python language is one of the most accessible programming languages available because it has simplified syntax and not complicated, which gives more emphasis on natural language. ## Computational Thinking One of the core skills to solve problem in Computer Science is Computational Thinking. Simply put, Computational Thinking is a technique used to solve problems, logically. Computational Thinking consists of four key components: - **Decomposition** - Breaking down a big problem into smaller problems - **Pattern Recognition** - Looking for similarities and trends - **Pattern Abstraction** - Identification of key components of the solution - **Algorithm Design** - Creation of step-by-step instructions to solve a problem - **Debugging** - Fixing errors(bugs) within your algorithm > **What is debugging?** > Programming is error-prone. For whimsical reasons, programming errors are called **bugs** and the process of tracking them down is called **debugging**. ### Example - Drawing Turtle `turtle` is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called the turtle and this is what gives the library its name. In short, the Python `turtle` library helps new programmers get a feel for what programming with Python is like in a fun and interactive way. **Installation** ```python !pip3 install ColabTurtle ``` ```python # Import the library from ColabTurtle.Turtle import * # Initialize the canvas initializeTurtle() ``` ![](https://i.imgur.com/lHLhgSQ.png) ```python print('Size of the Canvas:', window_width(), 'x', window_height(), 'pixels') ``` Size of the Canvas: 800 x 500 pixels The main functions that this library implements are explained below: | Function | Description | | ----------------- | ------------------------------------------------------------------------------------------------------------------------ | | `forward(units)` | Moves the turtle in the direction it is facing, by units pixels | | `backward(units)` | Moves the turtle in the opposite of the direction it is facing, by units pixels | | `right(degrees)` | Turns the turtle to right by the given degrees many degrees | | `left(degrees)` | Turns the turtle to left by the given degrees many degrees | | `goto(x, y)` | Moves the turtle to the point defined by `x`, `y` | | `penup()` | Lifts the pen, turtles movement will not draw anything after this function is called | | `pendown()` | Puts the pen down, causing the turtle movements to start drawing again | | `pensize(w)` | Changes the width of the pen | | `speed(s)` | Sets the speed of turtle's movements. s can be a value in interval `[1,13]` where 1 is the slowest and 13 is the fastest | | `bgcolor(c)` | Change the background color | | `color(c)` | Change the pen color | | `clear()` | Clear any drawing on the screen | The following formats are accepted for this color string: - HTML standard color names: 140 color names defined as standard ( <https://www.w3schools.com/colors/colors_names.asp> ) . Examples: `"red"`, `"black"`, `"magenta"`, `"cyan"` etc. - Hex string with 3 or 6 digits, like `"#fff"`, `"FFF"`, `"#dfdfdf"`, `"#DFDFDF"` - RGB string, like `"rgb(10 20 30)"`, `"rgb(10, 20, 30)"` **Example 1 - Draw a square** <img src="https://i.imgur.com/zI2uXIl.png" width="200px"/> ```python initializeTurtle() speed(5) forward(100) left(90) forward(100) left(90) forward(100) left(90) forward(100) left(90) forward(100) ``` ![](https://i.imgur.com/Jt3swNy.png) **Example 2 - Draw a star** <img src="https://i.imgur.com/7gD75yU.png" width="200px" /> - **Decomposition**: Drawing a star is basically drawing five lines with the same length. - **Pattern recognition**: First of all, we analyze the geometry of the star. The key insight here is the angle of **36 degrees** inside the tip of each point. <img src="https://i.imgur.com/q164z6t.png" width="400px"/> - **Abstraction**: The key component of the solution is move the turtle to draw a line then rotate it 144 degrees to draw the next line, repeat the process 5 times - **Algorithm design**: - Turn right 90 degrees - Move forward 100 pixels - Turn right 144 degrees - Move forward 100 pixels - Turn right 144 degrees - Move forward 100 pixels - Turn right 144 degrees - Move forward 100 pixels - Turn right 144 degrees - Move forward 100 pixels - Turn right 144 degrees ```python initializeTurtle() speed(5) right(90) forward(100) right(144) forward(100) right(144) forward(100) right(144) forward(100) right(144) forward(100) right(144) ``` ![](https://i.imgur.com/9ruqlty.png) **Refactoring** In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of the software (its non-functional attributes), while preserving its functionality. Here we put the repeated code in a `for` loop: ```python initializeTurtle() speed(5) right(90) for i in range(5): forward(100) right(144) ``` ![](https://i.imgur.com/9ruqlty.png) **Bonus: Rainbow Benzene** ```python initializeTurtle() speed(13) colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow'] for x in range(300): color(colors[x%6]) forward(x) left(59) ``` ![](https://i.imgur.com/rb3QFBy.png) ## Exercises ### Exercise 1 - Hexagon (easy) <img src="https://i.imgur.com/D9Lamop.png" width="200px" /> ```python # YOUR_CODE HERE ``` ### Exercise 2 - Plus sign (easy) <img src="https://i.imgur.com/os9uAds.png" width="200px" /> ```python # YOUR_CODE HERE ``` ### Exercise 3 - The Red Cross (medium) <img src="https://i.imgur.com/mdxaBKX.png" width="200px" /> ```python # YOUR_CODE HERE ``` ### Exercise 4 - The circle (medium) <img src="https://i.imgur.com/Oh1CDV2.png" width="200px" /> ```python # YOUR_CODE HERE ``` ### Exercise 5 - The square of squares (hard) <img src="https://i.imgur.com/2NFZZ12.png" width="200px" /> ```python # YOUR_CODE HERE ``` ## References - [This is CS50](https://www.youtube.com/watch?v=YoXxevp1WRQ) - [Computational Thinking](https://en.wikibooks.org/wiki/A-level_Computing/AQA/Problem_Solving,_Programming,_Data_Representation_and_Practical_Exercise/Problem_Solving/Introduction_to_principles_of_computation) - [ColabTurtle](https://github.com/tolgaatam/ColabTurtle)